Compare commits
6 Commits
Author | SHA1 | Date |
---|---|---|
|
fc5f7e0621 | |
|
9262b28795 | |
|
4e65f51b14 | |
|
b9b45d919c | |
|
3ea896f94f | |
|
c39dba5335 |
|
@ -0,0 +1,34 @@
|
||||||
|
{
|
||||||
|
"env": {
|
||||||
|
"browser": true,
|
||||||
|
"es6": true,
|
||||||
|
"node": true
|
||||||
|
},
|
||||||
|
"extends": "eslint:recommended",
|
||||||
|
"parserOptions": {
|
||||||
|
"ecmaVersion": 2017,
|
||||||
|
"ecmaFeatures": {
|
||||||
|
"experimentalObjectRestSpread": true
|
||||||
|
},
|
||||||
|
"sourceType": "module"
|
||||||
|
},
|
||||||
|
"rules": {
|
||||||
|
"indent": [
|
||||||
|
"error",
|
||||||
|
4,
|
||||||
|
{"SwitchCase": 1}
|
||||||
|
],
|
||||||
|
"quotes": [
|
||||||
|
"error",
|
||||||
|
"double"
|
||||||
|
],
|
||||||
|
"semi": [
|
||||||
|
"error",
|
||||||
|
"always"
|
||||||
|
],
|
||||||
|
"no-extra-semi": [
|
||||||
|
"off",
|
||||||
|
1
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
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
|
### Features
|
||||||
|
|
||||||
- Tiny, only 2kb
|
- Tiny, only >2kb
|
||||||
- Zero-Dependencies
|
- Zero-Dependencies
|
||||||
- Saves to LocalStorage (if available)
|
- Saves to LocalStorage (if available)
|
||||||
- Data saved, and exported as simple JSON
|
- Data saved, and exported as simple JSON
|
||||||
- Browser or Nodejs compatible
|
- Browser ESM or Nodejs (use `require('record.cjs.js').default`)
|
||||||
|
|
||||||
### Why
|
### Why
|
||||||
|
|
||||||
|
@ -95,6 +95,12 @@ Records.js constructor supports a few options passed in as an `object`
|
||||||
|
|
||||||
npm test
|
npm test
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
npm install esbuild -g
|
||||||
|
|
||||||
|
npm run build
|
||||||
|
|
||||||
## Support
|
## Support
|
||||||
|
|
||||||
Please open [an issue](https://github.com/n2geoff/record.js/issues/new) for support.
|
Please open [an issue](https://github.com/n2geoff/record.js/issues/new) for support.
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
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,160 +1 @@
|
||||||
/*! Record.js v0.7.0 | MIT | https://github.com/n2geoff/record.js */
|
/*! 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};
|
||||||
;(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.7.0 | MIT | https://github.com/n2geoff/record.js */
|
/*! 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};
|
||||||
!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))}}});
|
//# sourceMappingURL=record.min.js.map
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,5 +1,5 @@
|
||||||
const gulp = require("gulp");
|
const gulp = require("gulp");
|
||||||
const minify = require('gulp-minify');
|
const minify = require("gulp-minify");
|
||||||
const strip = require("gulp-strip-comments");
|
const strip = require("gulp-strip-comments");
|
||||||
|
|
||||||
gulp.task("default", function() {
|
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",
|
"name": "record.js",
|
||||||
"version": "0.7.0",
|
"version": "0.8.1",
|
||||||
"description": "dead-simple storage-collection library",
|
"description": "dead-simple storage-collection library",
|
||||||
"main": "src/record.js",
|
"main": "src/record.js",
|
||||||
"directories": {
|
"directories": {
|
||||||
|
@ -9,8 +9,12 @@
|
||||||
"test": "test"
|
"test": "test"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node_modules/.bin/tape test/*.spec.js",
|
"build": "npm run build:es && npm run build:es:min && npm run build:cjs",
|
||||||
"build": "node_modules/.bin/gulp"
|
"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"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -26,11 +30,9 @@
|
||||||
],
|
],
|
||||||
"author": "Geoff Doty",
|
"author": "Geoff Doty",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"dependencies": {},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"gulp": "^4.0.2",
|
"eslint": "^8.21.0",
|
||||||
"gulp-minify": "^3.1.0",
|
"tape": "^5.5.3"
|
||||||
"gulp-strip-comments": "^2.5.2",
|
|
||||||
"jshint": "^2.10.2",
|
|
||||||
"tape": "^4.10.2"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,10 @@
|
||||||
/*! Record.js v0.6.1 | MIT | https://github.com/n2geoff/record.js */
|
/*! Record.js v0.8.0 | 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";
|
|
||||||
/*
|
|
||||||
* Record.js
|
* Record.js
|
||||||
*
|
*
|
||||||
* A dead-simple object collection library
|
* A dead-simple object collection library
|
||||||
*/
|
*/
|
||||||
class Record {
|
class Record {
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
*
|
*
|
||||||
|
@ -39,7 +29,7 @@
|
||||||
this.records = [];
|
this.records = [];
|
||||||
|
|
||||||
// get stored records, merge as needed
|
// get stored records, merge as needed
|
||||||
if(this.store && localStorage) {
|
if (this.store && localStorage) {
|
||||||
this._log("Initializing localStorage for " + this.store);
|
this._log("Initializing localStorage for " + this.store);
|
||||||
|
|
||||||
let existing = this._load() || [];
|
let existing = this._load() || [];
|
||||||
|
@ -53,7 +43,7 @@
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_log() {
|
_log() {
|
||||||
if(this.debug) {
|
if (this.debug) {
|
||||||
console.log(...arguments);
|
console.log(...arguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,7 +115,7 @@
|
||||||
var idx = this.records.indexOf(entry);
|
var idx = this.records.indexOf(entry);
|
||||||
|
|
||||||
// sanity check
|
// sanity check
|
||||||
if(this.records[idx].id === entry.id) {
|
if (this.records[idx].id === entry.id) {
|
||||||
|
|
||||||
// update record
|
// update record
|
||||||
this.records.splice(idx, 1, entry);
|
this.records.splice(idx, 1, entry);
|
||||||
|
@ -180,7 +170,7 @@
|
||||||
return this.records.filter((record) => {
|
return this.records.filter((record) => {
|
||||||
|
|
||||||
// id trumps all
|
// id trumps all
|
||||||
if(value.indexOf("id") !== -1) {
|
if (value.indexOf("id") !== -1) {
|
||||||
return record.id === key.id;
|
return record.id === key.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,7 +261,7 @@
|
||||||
dump() {
|
dump() {
|
||||||
function download(filename, content) {
|
function download(filename, content) {
|
||||||
let a = document.createElement("a");
|
let a = document.createElement("a");
|
||||||
let file = new Blob([content], {type: "text/plain"});
|
let file = new Blob([content], { type: "text/plain" });
|
||||||
a.href = URL.createObjectURL(file);
|
a.href = URL.createObjectURL(file);
|
||||||
a.download = filename;
|
a.download = filename;
|
||||||
a.click();
|
a.click();
|
||||||
|
@ -279,7 +269,6 @@
|
||||||
|
|
||||||
download(`${this.store || "data"}.json`, JSON.stringify(this._load(), null, 4));
|
download(`${this.store || "data"}.json`, JSON.stringify(this._load(), null, 4));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Record;
|
export default Record;
|
||||||
}));
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const test = require("tape");
|
const test = require("tape");
|
||||||
const Record = require("../src/record.js");
|
const Record = require("../dist/record.cjs.js").default;
|
||||||
|
|
||||||
test("Record.js", function(t) {
|
test("Record.js", function(t) {
|
||||||
let pets;
|
let pets;
|
||||||
|
|
Loading…
Reference in New Issue