record.js/dist/record.min.js.map

8 lines
8.1 KiB
Plaintext

{
"version": 3,
"sources": ["../src/record.js"],
"sourcesContent": ["/*! Record.js v0.8.0 | MIT | https://github.com/n2geoff/record.js */\n/*\n * Record.js\n *\n * A dead-simple object collection library\n */\nclass Record {\n /**\n * @constructor\n *\n * @example\n * // create a new record (in-memory)\n * let pets = new Record();\n *\n * @example\n * // create a new record (localStorage)\n * let pets = new Record({\"store\": \"pets\"});\n *\n * @param {object} opts\n * @param {object} opts.store - localStorage ID to use\n * @param {object} opts.debug - show console logs\n */\n constructor(opts) {\n // define options\n this.store = (opts || {}).store;\n this.debug = (opts || {}).debug || false;\n\n // initialize the collection\n this.records = [];\n\n // get stored records, merge as needed\n if (this.store && localStorage) {\n this._log(\"Initializing localStorage for \" + this.store);\n\n let existing = this._load() || [];\n this.records = [...existing];\n }\n }\n\n /**\n * Supresses logs based on this.debug value\n *\n * @private\n */\n _log() {\n if (this.debug) {\n console.log(...arguments);\n }\n }\n\n /**\n * Add record to collection creating an sudo unique id if\n * one not provided\n *\n * @example\n * // add pet to collection\n * let dog = pets.add({\"name\": \"Yonkers\", \"age\": 5});\n * // > [{\"id\": \"14rj345k9\", \"name\": \"Yonkers\", \"age\": 5}]\n *\n * @param {Object} entry - object(s) you want to store\n * @returns {object} entry added\n */\n add(entry) {\n\n // for array of entries\n if (Array.isArray(entry)) {\n // temp array\n let entries = [];\n\n entry.forEach(() => {\n // add id if not provided\n if (!entry.id) {\n entry.id = Math.random().toString(36).substr(2, 9);\n }\n\n // add entry to records collection\n this.records.push(entry);\n\n // used to return all added\n this.entries.push(entry);\n });\n\n // save to storage\n this._save();\n\n // return added entry(s)\n return entries;\n\n // if single entry\n } else {\n // add id if not provided\n if (!entry.id) {\n entry.id = Math.random().toString(36).substr(2, 9);\n }\n\n // add entry to records collection\n this.records.push(entry);\n\n // save to storage\n this._save();\n\n // return added entry\n return entry;\n }\n }\n\n /**\n * Updates a Record\n *\n * @param {Object} entry\n */\n update(entry) {\n\n // find entry index\n var idx = this.records.indexOf(entry);\n\n // sanity check\n if (this.records[idx].id === entry.id) {\n\n // update record\n this.records.splice(idx, 1, entry);\n\n // save to storage\n this._save();\n\n return entry;\n\n } else {\n return false;\n }\n\n }\n\n /**\n * Finds records in collection by id or object filter.\n *\n * @example\n * // find all\n * let all = collection.find();\n *\n * @example\n * // find by id\n * let record = collection.find(1);\n *\n * @example\n * // filter by object\n * let dogs = collection.find({\"type\": \"dog\"});\n *\n * @param {string|number} key - (optional) by id, or object\n * @returns {array} matching records\n */\n find(key) {\n\n // return all records\n if (!key) {\n return this.records;\n }\n\n // find by id\n if (typeof key === \"string\" || typeof key === \"number\") {\n return this.records.filter((record) => {\n return record.id === key;\n });\n }\n\n // filter by\n let value = Object.keys(key);\n\n // filter records for value\n return this.records.filter((record) => {\n\n // id trumps all\n if (value.indexOf(\"id\") !== -1) {\n return record.id === key.id;\n }\n\n return value.every((val) => {\n return record[val] === key[val];\n });\n });\n\n }\n\n /**\n * Remove record(s) from collection. Leverages same functionality as `find`\n *\n * @example\n * // remove all records by type\n * let removed = collection.remove({\"type\": \"cat\"});\n * // > []\n *\n * @param {any} entry - (optional)\n *\n * @returns {array} records removed\n */\n remove(entry) {\n\n // use clear() to remove all\n if (!entry || Array.isArray(entry)) {\n this._log(console.error(\"remove() accepts a single object\"));\n return [];\n }\n\n // find matching records\n let entries = this.find(entry);\n\n // remove all matching records\n entries.forEach((item) => {\n this.records.splice(this.records.indexOf(item), 1);\n });\n\n // save to storage\n this._save();\n\n // return records removed\n return entries;\n }\n\n clear() {\n // erase records to empty array\n this.records = [];\n\n // save to storage\n this._save();\n }\n\n /**\n * save a record to storage if available\n *\n * @private\n *\n * @memberof Record\n */\n _save() {\n if (this.store && localStorage) {\n localStorage.setItem(this.store, JSON.stringify(this.records));\n }\n }\n\n /**\n * load records from storage if exists\n *\n * @private\n *\n * @returns {array} of loaded records\n * @memberof Record\n */\n _load() {\n if (this.store && localStorage) {\n return JSON.parse(localStorage.getItem(this.store)) || [];\n }\n }\n\n /**\n * Dumps data to JSON file\n *\n * Uses 'store' as file name with a '.json' extension\n *\n * @return {object} JSON Object of records\n */\n dump() {\n function download(filename, content) {\n let a = document.createElement(\"a\");\n let file = new Blob([content], { type: \"text/plain\" });\n a.href = URL.createObjectURL(file);\n a.download = filename;\n a.click();\n }\n\n download(`${this.store || \"data\"}.json`, JSON.stringify(this._load(), null, 4));\n }\n}\n\nexport default Record;\n"],
"mappings": "AAAA,4EAsBI,YAAY,GASR,GAPA,KAAK,MAAS,IAAQ,IAAI,MAC1B,KAAK,MAAS,IAAQ,IAAI,OAAS,GAGnC,KAAK,QAAU,GAGX,KAAK,OAAS,cACd,KAAK,KAAK,iCAAmC,KAAK,OAElD,MAAe,KAAK,SAAW,GAC/B,KAAK,QAAU,CAAC,GAAG,IAS3B,OACI,AAAI,KAAK,OACL,QAAQ,IAAI,GAAG,WAgBvB,OAGI,GAAI,MAAM,QAAQ,IAEd,MAAc,GAEd,SAAM,QAAQ,KAEV,AAAK,EAAM,IACP,GAAM,GAAK,KAAK,SAAS,SAAS,IAAI,OAAO,EAAG,IAIpD,KAAK,QAAQ,KAAK,GAGlB,KAAK,QAAQ,KAAK,KAItB,KAAK,QAGE,MAKP,OAAK,GAAM,IACP,GAAM,GAAK,KAAK,SAAS,SAAS,IAAI,OAAO,EAAG,IAIpD,KAAK,QAAQ,KAAK,GAGlB,KAAK,QAGE,EASf,UAGI,MAAU,KAAK,QAAQ,QAAQ,GAG/B,MAAI,MAAK,QAAQ,GAAK,KAAO,EAAM,GAG/B,MAAK,QAAQ,OAAO,EAAK,EAAG,GAG5B,KAAK,QAEE,GAGA,GAuBf,QAGI,GAAI,CAAC,EACD,MAAO,MAAK,QAIhB,GAAI,MAAO,IAAQ,UAAY,MAAO,IAAQ,SAC1C,MAAO,MAAK,QAAQ,OAAO,GAChB,EAAO,KAAO,GAK7B,MAAY,OAAO,KAAK,GAGxB,MAAO,MAAK,QAAQ,OAAO,GAGnB,EAA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