switching to esbuild

This commit is contained in:
Geoff Doty 2021-01-02 04:48:43 -05:00
parent b9b45d919c
commit 4e65f51b14
9 changed files with 714 additions and 3967 deletions

View File

@ -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.

1
dist/record.cjs.js vendored Normal file
View File

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

161
dist/record.js vendored
View File

@ -1,160 +1 @@
/*! Record.js v0.7.2 | 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;
}));

4
dist/record.min.js vendored
View File

@ -1,2 +1,2 @@
/*! Record.js v0.7.2 | 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

7
dist/record.min.js.map vendored Normal file

File diff suppressed because one or more lines are too long

3959
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "record.js", "name": "record.js",
"version": "0.7.2", "version": "0.8.0",
"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": {
"eslint": "^7.1.0", "eslint": "^7.16.0",
"gulp": "^4.0.2", "tape": "^5.1.0"
"gulp-minify": "^3.1.0",
"gulp-strip-comments": "^2.5.2",
"tape": "^5.0.1"
} }
} }

View File

@ -1,14 +1,4 @@
/*! Record.js v0.7.2 | 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
* *
@ -281,5 +271,4 @@
} }
} }
return Record; export default Record;
}));

View File

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