latest build

This commit is contained in:
Geoff Doty 2018-04-07 00:19:31 -04:00
parent 21449685f1
commit 7601b132aa
3 changed files with 139 additions and 3 deletions

136
dist/record.js vendored Normal file
View File

@ -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;
}));

4
dist/record.min.js vendored
View File

@ -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(){}}}));

View File

@ -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) {