diff --git a/dist/record.min.js b/dist/record.min.js index a088ea5..0d01ce4 100644 --- a/dist/record.min.js +++ b/dist/record.min.js @@ -1,2 +1,2 @@ /* Record.js | MIT | https://github.com/n2geoff/record.js */ -;(function(root,factory){if(typeof module==="object"&&module.exports){module.exports=factory(root.Record)}else{root.Record=factory(root.Record)}})(this,function(me){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(i=>{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(obj=>{return record[value]===key[value]})})}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}); +(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}); diff --git a/src/record.js b/src/record.js index 0bea53c..64a982c 100644 --- a/src/record.js +++ b/src/record.js @@ -1,29 +1,29 @@ -'use strict'; - -(function(root, factory) { - if(typeof module === "object" && module.exports) { +/* 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(me) { - +}(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(); - * + * * @param {array} init - collection to start with - * @param {object} opts + * @param {object} opts * @param {object} opts.store - localStorage ID to use * @param {object} opts.debug - show console logs */ @@ -38,7 +38,7 @@ /** * Supresses logs based on this.debug value - * + * * @private */ _log() { @@ -48,22 +48,22 @@ } /** - * Add record to collection creating an sudo unique id if + * 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 + * + * @param {Object} entry - object(s) you want to store * @returns {object} entry added */ add(entry) { - if(Array.isArray(entry)) { - entry.forEach((i) => { - if(!entry.id) { + if (Array.isArray(entry)) { + entry.forEach(() => { + if (!entry.id) { entry.id = Math.random().toString(36).substr(2, 9); } @@ -72,10 +72,10 @@ return entry; }); } else { - if(!entry.id) { + if (!entry.id) { entry.id = Math.random().toString(36).substr(2, 9); } - + this.records.push(entry); return entry; @@ -83,50 +83,50 @@ } /** - * Finds records in collection by id or object filter. - * + * 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) { + if (!key) { return this.records; } // find by id - if(typeof key === 'string' || typeof key === 'number') { + if (typeof key === "string" || typeof key === "number") { return this.records.filter((record) => { return record.id === key; }); } - // filter by + // filter by let value = Object.keys(key); // filter records for value return this.records.filter((record) => { - + // id trumps all - if(value.indexOf('id') !== -1) { + if(value.indexOf("id") !== -1) { return record.id === key.id; } - return value.every((obj) => { - return record[value] === key[value]; + return value.every((val) => { + return record[val] === key[val]; }); }); @@ -134,24 +134,24 @@ /** * 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) - * + * + * @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')); + if (!entry || Array.isArray(entry)) { + this._log(console.error("remove() accepts a single object")); return []; } - + // find matching records let entries = this.find(entry); @@ -177,15 +177,13 @@ // save to localstorage save() { - if(this.storage) { - + if (this.storage) { + // check for localStorage } } dump() {} - } return Record; })); - \ No newline at end of file