Compare commits
No commits in common. "master" and "0.7.1" have entirely different histories.
10
README.md
10
README.md
|
@ -8,11 +8,11 @@ Records can be exported to JSON, with no internal metadata[*], so you can import
|
|||
|
||||
### Features
|
||||
|
||||
- Tiny, only >2kb
|
||||
- Tiny, only 2kb
|
||||
- Zero-Dependencies
|
||||
- Saves to LocalStorage (if available)
|
||||
- Data saved, and exported as simple JSON
|
||||
- Browser ESM or Nodejs (use `require('record.cjs.js').default`)
|
||||
- Browser or Nodejs compatible
|
||||
|
||||
### Why
|
||||
|
||||
|
@ -95,12 +95,6 @@ Records.js constructor supports a few options passed in as an `object`
|
|||
|
||||
npm test
|
||||
|
||||
## Build
|
||||
|
||||
npm install esbuild -g
|
||||
|
||||
npm run build
|
||||
|
||||
## Support
|
||||
|
||||
Please open [an issue](https://github.com/n2geoff/record.js/issues/new) for support.
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
var __defProp=Object.defineProperty;var __markAsModule=target=>__defProp(target,"__esModule",{value:true});var __export=(target,all)=>{__markAsModule(target);for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};__export(exports,{default:()=>record_default});/*! Record.js v0.8.0 | MIT | https://github.com/n2geoff/record.js */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}}update(entry){var idx=this.records.indexOf(entry);if(this.records[idx].id===entry.id){this.records.splice(idx,1,entry);this._save();return entry}else{return false}}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()}_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(){function download(filename,content){let a=document.createElement("a");let file=new Blob([content],{type:"text/plain"});a.href=URL.createObjectURL(file);a.download=filename;a.click()}download(`${this.store||"data"}.json`,JSON.stringify(this._load(),null,4))}}var record_default=Record;
|
|
@ -1 +1,160 @@
|
|||
/*! Record.js v0.8.0 | MIT | https://github.com/n2geoff/record.js */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}}update(entry){var idx=this.records.indexOf(entry);if(this.records[idx].id===entry.id){this.records.splice(idx,1,entry);this._save();return entry}else{return false}}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()}_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(){function download(filename,content){let a=document.createElement("a");let file=new Blob([content],{type:"text/plain"});a.href=URL.createObjectURL(file);a.download=filename;a.click()}download(`${this.store||"data"}.json`,JSON.stringify(this._load(),null,4))}}var record_default=Record;export{record_default as default};
|
||||
/*! Record.js v0.7.1 | 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;
|
||||
}
|
||||
}
|
||||
|
||||
update(entry) {
|
||||
|
||||
var idx = this.records.indexOf(entry);
|
||||
|
||||
if(this.records[idx].id === entry.id) {
|
||||
|
||||
this.records.splice(idx, 1, entry);
|
||||
|
||||
this._save();
|
||||
|
||||
return entry;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
_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() {
|
||||
function download(filename, content) {
|
||||
let a = document.createElement("a");
|
||||
let file = new Blob([content], {type: "text/plain"});
|
||||
a.href = URL.createObjectURL(file);
|
||||
a.download = filename;
|
||||
a.click();
|
||||
}
|
||||
|
||||
download(`${this.store || "data"}.json`, JSON.stringify(this._load(), null, 4));
|
||||
}
|
||||
}
|
||||
|
||||
return Record;
|
||||
}));
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
/*! 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
|
||||
/*! Record.js v0.7.1 | MIT | https://github.com/n2geoff/record.js */
|
||||
!function(t,e){"use strict";"object"==typeof module&&module.exports?module.exports=e(t.Record):t.Record=e(t.Record)}(this,function(){"use strict";return class{constructor(t){if(this.store=(t||{}).store,this.debug=(t||{}).debug||!1,this.records=[],this.store&&localStorage){this._log("Initializing localStorage for "+this.store);let t=this._load()||[];this.records=[...t]}}_log(){this.debug&&console.log(...arguments)}add(t){if(Array.isArray(t)){let e=[];return t.forEach(()=>{t.id||(t.id=Math.random().toString(36).substr(2,9)),this.records.push(t),this.entries.push(t)}),this._save(),e}return t.id||(t.id=Math.random().toString(36).substr(2,9)),this.records.push(t),this._save(),t}update(t){var e=this.records.indexOf(t);return this.records[e].id===t.id&&(this.records.splice(e,1,t),this._save(),t)}find(t){if(!t)return this.records;if("string"==typeof t||"number"==typeof t)return this.records.filter(e=>e.id===t);let e=Object.keys(t);return this.records.filter(r=>-1!==e.indexOf("id")?r.id===t.id:e.every(e=>r[e]===t[e]))}remove(t){if(!t||Array.isArray(t))return this._log(console.error("remove() accepts a single object")),[];let e=this.find(t);return e.forEach(t=>{this.records.splice(this.records.indexOf(t),1)}),this._save(),e}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(t,e){let r=document.createElement("a"),s=new Blob([e],{type:"text/plain"});r.href=URL.createObjectURL(s),r.download=t,r.click()}(`${this.store||"data"}.json`,JSON.stringify(this._load(),null,4))}}});
|
File diff suppressed because one or more lines are too long
|
@ -1,5 +1,5 @@
|
|||
const gulp = require("gulp");
|
||||
const minify = require("gulp-minify");
|
||||
const minify = require('gulp-minify');
|
||||
const strip = require("gulp-strip-comments");
|
||||
|
||||
gulp.task("default", function() {
|
||||
|
|
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "record.js",
|
||||
"version": "0.8.1",
|
||||
"version": "0.7.1",
|
||||
"description": "dead-simple storage-collection library",
|
||||
"main": "src/record.js",
|
||||
"directories": {
|
||||
|
@ -9,12 +9,8 @@
|
|||
"test": "test"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "npm run build:es && npm run build:es:min && npm run build:cjs",
|
||||
"build:es": "esbuild src/record.js --minify-whitespace --format=esm --outfile=dist/record.js",
|
||||
"build:es:min": "esbuild src/record.js --minify --sourcemap --format=esm --outfile=dist/record.min.js",
|
||||
"build:cjs": "esbuild src/record.js --minify-whitespace --format=cjs --platform=node --outfile=dist/record.cjs.js",
|
||||
"test": "npm run lint && npm run build && npx tape test/*.spec.js",
|
||||
"lint": "npx eslint src/record.js"
|
||||
"test": "node_modules/.bin/tape test/*.spec.js",
|
||||
"build": "node_modules/.bin/gulp"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -30,9 +26,11 @@
|
|||
],
|
||||
"author": "Geoff Doty",
|
||||
"license": "MIT",
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"eslint": "^8.21.0",
|
||||
"tape": "^5.5.3"
|
||||
"eslint": "^6.8.0",
|
||||
"gulp": "^4.0.2",
|
||||
"gulp-minify": "^3.1.0",
|
||||
"gulp-strip-comments": "^2.5.2",
|
||||
"tape": "^4.13.2"
|
||||
}
|
||||
}
|
||||
|
|
471
src/record.js
471
src/record.js
|
@ -1,73 +1,103 @@
|
|||
/*! Record.js v0.8.0 | MIT | https://github.com/n2geoff/record.js */
|
||||
/*
|
||||
* Record.js
|
||||
*
|
||||
* A dead-simple object collection library
|
||||
*/
|
||||
class Record {
|
||||
/**
|
||||
* @constructor
|
||||
*
|
||||
* @example
|
||||
* // create a new record (in-memory)
|
||||
* let pets = new Record();
|
||||
*
|
||||
* @example
|
||||
* // create a new record (localStorage)
|
||||
* let pets = new Record({"store": "pets"});
|
||||
*
|
||||
* @param {object} opts
|
||||
* @param {object} opts.store - localStorage ID to use
|
||||
* @param {object} opts.debug - show console logs
|
||||
*/
|
||||
constructor(opts) {
|
||||
// define options
|
||||
this.store = (opts || {}).store;
|
||||
this.debug = (opts || {}).debug || false;
|
||||
|
||||
// initialize the collection
|
||||
this.records = [];
|
||||
|
||||
// get stored records, merge as needed
|
||||
if (this.store && localStorage) {
|
||||
this._log("Initializing localStorage for " + this.store);
|
||||
|
||||
let existing = this._load() || [];
|
||||
this.records = [...existing];
|
||||
}
|
||||
/*! Record.js v0.7.1 | 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Supresses logs based on this.debug value
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_log() {
|
||||
if (this.debug) {
|
||||
console.log(...arguments);
|
||||
}(this, function () {
|
||||
"use strict";
|
||||
/*
|
||||
* Record.js
|
||||
*
|
||||
* A dead-simple object collection library
|
||||
*/
|
||||
class Record {
|
||||
/**
|
||||
* @constructor
|
||||
*
|
||||
* @example
|
||||
* // create a new record (in-memory)
|
||||
* let pets = new Record();
|
||||
*
|
||||
* @example
|
||||
* // create a new record (localStorage)
|
||||
* let pets = new Record({"store": "pets"});
|
||||
*
|
||||
* @param {object} opts
|
||||
* @param {object} opts.store - localStorage ID to use
|
||||
* @param {object} opts.debug - show console logs
|
||||
*/
|
||||
constructor(opts) {
|
||||
// define options
|
||||
this.store = (opts || {}).store;
|
||||
this.debug = (opts || {}).debug || false;
|
||||
|
||||
// initialize the collection
|
||||
this.records = [];
|
||||
|
||||
// get stored records, merge as needed
|
||||
if(this.store && localStorage) {
|
||||
this._log("Initializing localStorage for " + this.store);
|
||||
|
||||
let existing = this._load() || [];
|
||||
this.records = [...existing];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add record to collection creating an sudo unique id if
|
||||
* one not provided
|
||||
*
|
||||
* @example
|
||||
* // add pet to collection
|
||||
* let dog = pets.add({"name": "Yonkers", "age": 5});
|
||||
* // > [{"id": "14rj345k9", "name": "Yonkers", "age": 5}]
|
||||
*
|
||||
* @param {Object} entry - object(s) you want to store
|
||||
* @returns {object} entry added
|
||||
*/
|
||||
add(entry) {
|
||||
/**
|
||||
* Supresses logs based on this.debug value
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_log() {
|
||||
if(this.debug) {
|
||||
console.log(...arguments);
|
||||
}
|
||||
}
|
||||
|
||||
// for array of entries
|
||||
if (Array.isArray(entry)) {
|
||||
// temp array
|
||||
let entries = [];
|
||||
/**
|
||||
* Add record to collection creating an sudo unique id if
|
||||
* one not provided
|
||||
*
|
||||
* @example
|
||||
* // add pet to collection
|
||||
* let dog = pets.add({"name": "Yonkers", "age": 5});
|
||||
* // > [{"id": "14rj345k9", "name": "Yonkers", "age": 5}]
|
||||
*
|
||||
* @param {Object} entry - object(s) you want to store
|
||||
* @returns {object} entry added
|
||||
*/
|
||||
add(entry) {
|
||||
|
||||
entry.forEach(() => {
|
||||
// for array of entries
|
||||
if (Array.isArray(entry)) {
|
||||
// temp array
|
||||
let entries = [];
|
||||
|
||||
entry.forEach(() => {
|
||||
// add id if not provided
|
||||
if (!entry.id) {
|
||||
entry.id = Math.random().toString(36).substr(2, 9);
|
||||
}
|
||||
|
||||
// add entry to records collection
|
||||
this.records.push(entry);
|
||||
|
||||
// used to return all added
|
||||
this.entries.push(entry);
|
||||
});
|
||||
|
||||
// save to storage
|
||||
this._save();
|
||||
|
||||
// return added entry(s)
|
||||
return entries;
|
||||
|
||||
// if single entry
|
||||
} else {
|
||||
// add id if not provided
|
||||
if (!entry.id) {
|
||||
entry.id = Math.random().toString(36).substr(2, 9);
|
||||
|
@ -76,199 +106,180 @@ class Record {
|
|||
// add entry to records collection
|
||||
this.records.push(entry);
|
||||
|
||||
// used to return all added
|
||||
this.entries.push(entry);
|
||||
});
|
||||
// save to storage
|
||||
this._save();
|
||||
|
||||
// save to storage
|
||||
this._save();
|
||||
// return added entry
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
// return added entry(s)
|
||||
return entries;
|
||||
/**
|
||||
* Updates a Record
|
||||
*
|
||||
* @param {Object} entry
|
||||
*/
|
||||
update(entry) {
|
||||
|
||||
// if single entry
|
||||
} else {
|
||||
// add id if not provided
|
||||
if (!entry.id) {
|
||||
entry.id = Math.random().toString(36).substr(2, 9);
|
||||
// find entry index
|
||||
var idx = this.records.indexOf(entry);
|
||||
|
||||
// sanity check
|
||||
if(this.records[idx].id === entry.id) {
|
||||
|
||||
// update record
|
||||
this.records.splice(idx, 1, entry);
|
||||
|
||||
// save to storage
|
||||
this._save();
|
||||
|
||||
return entry;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
// add entry to records collection
|
||||
this.records.push(entry);
|
||||
|
||||
// save to storage
|
||||
this._save();
|
||||
|
||||
// return added entry
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a Record
|
||||
*
|
||||
* @param {Object} entry
|
||||
*/
|
||||
update(entry) {
|
||||
|
||||
// find entry index
|
||||
var idx = this.records.indexOf(entry);
|
||||
|
||||
// sanity check
|
||||
if (this.records[idx].id === entry.id) {
|
||||
|
||||
// update record
|
||||
this.records.splice(idx, 1, entry);
|
||||
|
||||
// save to storage
|
||||
this._save();
|
||||
|
||||
return entry;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Finds records in collection by id or object filter.
|
||||
*
|
||||
* @example
|
||||
* // find all
|
||||
* let all = collection.find();
|
||||
*
|
||||
* @example
|
||||
* // find by id
|
||||
* let record = collection.find(1);
|
||||
*
|
||||
* @example
|
||||
* // filter by object
|
||||
* let dogs = collection.find({"type": "dog"});
|
||||
*
|
||||
* @param {string|number} key - (optional) by id, or object
|
||||
* @returns {array} matching records
|
||||
*/
|
||||
find(key) {
|
||||
|
||||
/**
|
||||
* Finds records in collection by id or object filter.
|
||||
*
|
||||
* @example
|
||||
* // find all
|
||||
* let all = collection.find();
|
||||
*
|
||||
* @example
|
||||
* // find by id
|
||||
* let record = collection.find(1);
|
||||
*
|
||||
* @example
|
||||
* // filter by object
|
||||
* let dogs = collection.find({"type": "dog"});
|
||||
*
|
||||
* @param {string|number} key - (optional) by id, or object
|
||||
* @returns {array} matching records
|
||||
*/
|
||||
find(key) {
|
||||
// return all records
|
||||
if (!key) {
|
||||
return this.records;
|
||||
}
|
||||
|
||||
// return all records
|
||||
if (!key) {
|
||||
return this.records;
|
||||
}
|
||||
// find by id
|
||||
if (typeof key === "string" || typeof key === "number") {
|
||||
return this.records.filter((record) => {
|
||||
return record.id === key;
|
||||
});
|
||||
}
|
||||
|
||||
// find by id
|
||||
if (typeof key === "string" || typeof key === "number") {
|
||||
// filter by
|
||||
let value = Object.keys(key);
|
||||
|
||||
// filter records for value
|
||||
return this.records.filter((record) => {
|
||||
return record.id === key;
|
||||
|
||||
// id trumps all
|
||||
if(value.indexOf("id") !== -1) {
|
||||
return record.id === key.id;
|
||||
}
|
||||
|
||||
return value.every((val) => {
|
||||
return record[val] === key[val];
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// filter by
|
||||
let value = Object.keys(key);
|
||||
/**
|
||||
* Remove record(s) from collection. Leverages same functionality as `find`
|
||||
*
|
||||
* @example
|
||||
* // remove all records by type
|
||||
* let removed = collection.remove({"type": "cat"});
|
||||
* // > []
|
||||
*
|
||||
* @param {any} entry - (optional)
|
||||
*
|
||||
* @returns {array} records removed
|
||||
*/
|
||||
remove(entry) {
|
||||
|
||||
// filter records for value
|
||||
return this.records.filter((record) => {
|
||||
|
||||
// id trumps all
|
||||
if (value.indexOf("id") !== -1) {
|
||||
return record.id === key.id;
|
||||
// use clear() to remove all
|
||||
if (!entry || Array.isArray(entry)) {
|
||||
this._log(console.error("remove() accepts a single object"));
|
||||
return [];
|
||||
}
|
||||
|
||||
return value.every((val) => {
|
||||
return record[val] === key[val];
|
||||
// find matching records
|
||||
let entries = this.find(entry);
|
||||
|
||||
// remove all matching records
|
||||
entries.forEach((item) => {
|
||||
this.records.splice(this.records.indexOf(item), 1);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
// save to storage
|
||||
this._save();
|
||||
|
||||
/**
|
||||
* Remove record(s) from collection. Leverages same functionality as `find`
|
||||
*
|
||||
* @example
|
||||
* // remove all records by type
|
||||
* let removed = collection.remove({"type": "cat"});
|
||||
* // > []
|
||||
*
|
||||
* @param {any} entry - (optional)
|
||||
*
|
||||
* @returns {array} records removed
|
||||
*/
|
||||
remove(entry) {
|
||||
|
||||
// use clear() to remove all
|
||||
if (!entry || Array.isArray(entry)) {
|
||||
this._log(console.error("remove() accepts a single object"));
|
||||
return [];
|
||||
// return records removed
|
||||
return entries;
|
||||
}
|
||||
|
||||
// find matching records
|
||||
let entries = this.find(entry);
|
||||
clear() {
|
||||
// erase records to empty array
|
||||
this.records = [];
|
||||
|
||||
// remove all matching records
|
||||
entries.forEach((item) => {
|
||||
this.records.splice(this.records.indexOf(item), 1);
|
||||
});
|
||||
// save to storage
|
||||
this._save();
|
||||
}
|
||||
|
||||
// save to storage
|
||||
this._save();
|
||||
/**
|
||||
* save a record to storage if available
|
||||
*
|
||||
* @private
|
||||
*
|
||||
* @memberof Record
|
||||
*/
|
||||
_save() {
|
||||
if (this.store && localStorage) {
|
||||
localStorage.setItem(this.store, JSON.stringify(this.records));
|
||||
}
|
||||
}
|
||||
|
||||
// return records removed
|
||||
return entries;
|
||||
}
|
||||
/**
|
||||
* load records from storage if exists
|
||||
*
|
||||
* @private
|
||||
*
|
||||
* @returns {array} of loaded records
|
||||
* @memberof Record
|
||||
*/
|
||||
_load() {
|
||||
if (this.store && localStorage) {
|
||||
return JSON.parse(localStorage.getItem(this.store)) || [];
|
||||
}
|
||||
}
|
||||
|
||||
clear() {
|
||||
// erase records to empty array
|
||||
this.records = [];
|
||||
/**
|
||||
* Dumps data to JSON file
|
||||
*
|
||||
* Uses 'store' as file name with a '.json' extension
|
||||
*
|
||||
* @return {object} JSON Object of records
|
||||
*/
|
||||
dump() {
|
||||
function download(filename, content) {
|
||||
let a = document.createElement("a");
|
||||
let file = new Blob([content], {type: "text/plain"});
|
||||
a.href = URL.createObjectURL(file);
|
||||
a.download = filename;
|
||||
a.click();
|
||||
}
|
||||
|
||||
// save to storage
|
||||
this._save();
|
||||
}
|
||||
|
||||
/**
|
||||
* save a record to storage if available
|
||||
*
|
||||
* @private
|
||||
*
|
||||
* @memberof Record
|
||||
*/
|
||||
_save() {
|
||||
if (this.store && localStorage) {
|
||||
localStorage.setItem(this.store, JSON.stringify(this.records));
|
||||
download(`${this.store || "data"}.json`, JSON.stringify(this._load(), null, 4));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* load records from storage if exists
|
||||
*
|
||||
* @private
|
||||
*
|
||||
* @returns {array} of loaded records
|
||||
* @memberof Record
|
||||
*/
|
||||
_load() {
|
||||
if (this.store && localStorage) {
|
||||
return JSON.parse(localStorage.getItem(this.store)) || [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dumps data to JSON file
|
||||
*
|
||||
* Uses 'store' as file name with a '.json' extension
|
||||
*
|
||||
* @return {object} JSON Object of records
|
||||
*/
|
||||
dump() {
|
||||
function download(filename, content) {
|
||||
let a = document.createElement("a");
|
||||
let file = new Blob([content], { type: "text/plain" });
|
||||
a.href = URL.createObjectURL(file);
|
||||
a.download = filename;
|
||||
a.click();
|
||||
}
|
||||
|
||||
download(`${this.store || "data"}.json`, JSON.stringify(this._load(), null, 4));
|
||||
}
|
||||
}
|
||||
|
||||
export default Record;
|
||||
return Record;
|
||||
}));
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const test = require("tape");
|
||||
const Record = require("../dist/record.cjs.js").default;
|
||||
const Record = require("../src/record.js");
|
||||
|
||||
test("Record.js", function(t) {
|
||||
let pets;
|
||||
|
|
Loading…
Reference in New Issue