From 7601b132aafacfa2745f43e8c403c3d3ff2f82e1 Mon Sep 17 00:00:00 2001 From: Geoff Doty Date: Sat, 7 Apr 2018 00:19:31 -0400 Subject: [PATCH] latest build --- dist/record.js | 136 +++++++++++++++++++++++++++++++++++++++++++++ dist/record.min.js | 4 +- src/record.js | 2 +- 3 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 dist/record.js diff --git a/dist/record.js b/dist/record.js new file mode 100644 index 0000000..a3485b6 --- /dev/null +++ b/dist/record.js @@ -0,0 +1,136 @@ +/*! Record.js | MIT | https://github.com/n2geoff/record.js */ +(function (root, factory) { + "use strict"; + if (typeof module === "object" && module.exports) { + module.exports = factory(root.Record); + } else { + root.Record = factory(root.Record); + } + +}(this, function () { + "use strict"; + class Record { + constructor(opts) { + this.store = (opts || {}).store; + this.debug = (opts || {}).debug || false; + + this.records = []; + + if(this.store && localStorage) { + this._log("Initializing localStorage for " + this.store); + + let existing = this._load() || []; + this.records = [...existing]; + } + } + + _log() { + if(this.debug) { + console.log(...arguments); + } + } + + add(entry) { + + if (Array.isArray(entry)) { + let entries = []; + + entry.forEach(() => { + if (!entry.id) { + entry.id = Math.random().toString(36).substr(2, 9); + } + + this.records.push(entry); + + this.entries.push(entry); + }); + + this._save(); + + return entries; + + } else { + if (!entry.id) { + entry.id = Math.random().toString(36).substr(2, 9); + } + + this.records.push(entry); + + this._save(); + + return entry; + } + } + + find(key) { + + if (!key) { + return this.records; + } + + if (typeof key === "string" || typeof key === "number") { + return this.records.filter((record) => { + return record.id === key; + }); + } + + let value = Object.keys(key); + + return this.records.filter((record) => { + + if(value.indexOf("id") !== -1) { + return record.id === key.id; + } + + return value.every((val) => { + return record[val] === key[val]; + }); + }); + + } + + remove(entry) { + + if (!entry || Array.isArray(entry)) { + this._log(console.error("remove() accepts a single object")); + return []; + } + + let entries = this.find(entry); + + entries.forEach((item) => { + this.records.splice(this.records.indexOf(item), 1); + }); + + this._save(); + + return entries; + } + + clear() { + this.records = []; + + this._save(); + } + + count() { + return this.records.length; + } + + _save() { + if (this.store && localStorage) { + localStorage.setItem(this.store, JSON.stringify(this.records)); + } + } + + _load() { + if (this.store && localStorage) { + return JSON.parse(localStorage.getItem(this.store)) || []; + } + } + + dump() {} + } + + return Record; +})); diff --git a/dist/record.min.js b/dist/record.min.js index 0d01ce4..71eef70 100644 --- a/dist/record.min.js +++ b/dist/record.min.js @@ -1,2 +1,2 @@ -/* Record.js | MIT | https://github.com/n2geoff/record.js */ -(function(root,factory){"use strict";if(typeof module==="object"&&module.exports){module.exports=factory(root.Record)}else{root.Record=factory(root.Record)}})(this,function(){"use strict";class Record{constructor(init,opts){this.store=(opts||{}).store;this.debug=(opts||{}).debug;this.records=Array.isArray(init)?init:[]}_log(){if(!this.debug){console.log(...arguments)}}add(entry){if(Array.isArray(entry)){entry.forEach(()=>{if(!entry.id){entry.id=Math.random().toString(36).substr(2,9)}this.records.push(entry);return entry})}else{if(!entry.id){entry.id=Math.random().toString(36).substr(2,9)}this.records.push(entry);return entry}}find(key){if(!key){return this.records}if(typeof key==="string"||typeof key==="number"){return this.records.filter(record=>{return record.id===key})}let value=Object.keys(key);return this.records.filter(record=>{if(value.indexOf("id")!==-1){return record.id===key.id}return value.every(val=>{return record[val]===key[val]})})}remove(entry){if(!entry||Array.isArray(entry)){this._log(console.error("remove() accepts a single object"));return[]}let entries=this.find(entry);entries.forEach(item=>{this.records.splice(this.records.indexOf(item),1)});return entries}clear(){this.records=[]}count(){return this.records.length}save(){if(this.storage){}}dump(){}}return Record}); +/*! Record.js | MIT | https://github.com/n2geoff/record.js */ +(function(r,t){"use strict";"object"==typeof module&&module.exports?module.exports=t(r.Record):r.Record=t(r.Record)}(this,function(){"use strict";return class{constructor(r){if(this.store=(r||{}).store,this.debug=(r||{}).debug||!1,this.records=[],this.store&&localStorage){this._log("Initializing localStorage for "+this.store);let r=this._load()||[];this.records=[...r]}}_log(){this.debug&&console.log(...arguments)}add(r){if(Array.isArray(r)){let t=[];return r.forEach(()=>{r.id||(r.id=Math.random().toString(36).substr(2,9)),this.records.push(r),this.entries.push(r)}),this._save(),t}return r.id||(r.id=Math.random().toString(36).substr(2,9)),this.records.push(r),this._save(),r}find(r){if(!r)return this.records;if("string"==typeof r||"number"==typeof r)return this.records.filter(t=>t.id===r);let t=Object.keys(r);return this.records.filter(e=>-1!==t.indexOf("id")?e.id===r.id:t.every(t=>e[t]===r[t]))}remove(r){if(!r||Array.isArray(r))return this._log(console.error("remove() accepts a single object")),[];let t=this.find(r);return t.forEach(r=>{this.records.splice(this.records.indexOf(r),1)}),this._save(),t}clear(){this.records=[],this._save()}count(){return this.records.length}_save(){this.store&&localStorage&&localStorage.setItem(this.store,JSON.stringify(this.records))}_load(){if(this.store&&localStorage)return JSON.parse(localStorage.getItem(this.store))||[]}dump(){}}})); \ No newline at end of file diff --git a/src/record.js b/src/record.js index 6da86e0..a7e002e 100644 --- a/src/record.js +++ b/src/record.js @@ -1,4 +1,4 @@ -/* Record.js | MIT | https://github.com/n2geoff/record.js */ +/*! Record.js | MIT | https://github.com/n2geoff/record.js */ (function (root, factory) { "use strict"; if (typeof module === "object" && module.exports) {