record.js/dist/record.min.js

3 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2021-01-02 09:48:43 +00:00
/*! Record.js v0.8.0 | MIT | https://github.com/n2geoff/record.js */class o{constructor(e){if(this.store=(e||{}).store,this.debug=(e||{}).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(e){if(Array.isArray(e)){let r=[];return e.forEach(()=>{e.id||(e.id=Math.random().toString(36).substr(2,9)),this.records.push(e),this.entries.push(e)}),this._save(),r}else return e.id||(e.id=Math.random().toString(36).substr(2,9)),this.records.push(e),this._save(),e}update(e){var r=this.records.indexOf(e);return this.records[r].id===e.id?(this.records.splice(r,1,e),this._save(),e):!1}find(e){if(!e)return this.records;if(typeof e=="string"||typeof e=="number")return this.records.filter(t=>t.id===e);let r=Object.keys(e);return this.records.filter(t=>r.indexOf("id")!==-1?t.id===e.id:r.every(s=>t[s]===e[s]))}remove(e){if(!e||Array.isArray(e))return this._log(console.error("remove() accepts a single object")),[];let r=this.find(e);return r.forEach(t=>{this.records.splice(this.records.indexOf(t),1)}),this._save(),r}clear(){this.records=[],this._save()}_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(){function e(r,t){let s=document.createElement("a"),i=new Blob([t],{type:"text/plain"});s.href=URL.createObjectURL(i),s.download=r,s.click()}e(`${this.store||"data"}.json`,JSON.stringify(this._load(),null,4))}}var a=o;export{a as default};
//# sourceMappingURL=record.min.js.map