Permalink
Please sign in to comment.
Browse files
Created a basic localStorage ORM + refactored app to use it
- Loading branch information...
Showing
with
204 additions
and 42 deletions.
- +1 โ1 README.md
- +3 โ1 package.json
- +6 โ13 src/Article.js
- +3 โ8 src/Editor.js
- +16 โ15 src/Saved.js
- +7 โ0 src/configure-localStore.js
- +22 โ4 src/index.js
- +109 โ0 src/localStore.js
- +37 โ0 src/localStore.test.js
31
src/Saved.js
| @@ -0,0 +1,7 @@ | ||
| +import LocalStore from './localStore'; | ||
| + | ||
| +let store = (name, existing) => { | ||
| + return new LocalStore('savedWriting', existing); | ||
| +}; | ||
| + | ||
| +export default store; |
26
src/index.js
| @@ -0,0 +1,109 @@ | ||
| +export default class LocalStore { | ||
| + constructor(storeName, existingStore) { | ||
| + this.storeName = storeName; | ||
| + if (existingStore) { | ||
| + localStorage[storeName] = existingStore; | ||
| + } else { | ||
| + localStorage[storeName] = JSON.stringify([]); | ||
| + } | ||
| + } | ||
| + | ||
| + getAll() { | ||
| + return JSON.parse( | ||
| + localStorage[this.storeName] | ||
| + ); | ||
| + } | ||
| + | ||
| + setAll(mutatedStore) { | ||
| + localStorage[this.storeName] = JSON.stringify(mutatedStore); | ||
| + } | ||
| + | ||
| + addItem(item) { | ||
| + let storeObject = this.getAll(); | ||
| + storeObject.push(item); | ||
| + this.setAll(storeObject); | ||
| + } | ||
| + | ||
| + removeItem(item) { | ||
| + let storeObject = this.getAll(); | ||
| + let index; | ||
| + if (typeof item === 'object') { | ||
| + index = indexOfWithObject(storeObject, item); | ||
| + } else { | ||
| + index = storeObject.indexOf(item); | ||
| + } | ||
| + storeObject.splice( | ||
| + index, | ||
| + 1 | ||
| + ); | ||
| + this.setAll(storeObject); | ||
| + } | ||
| + | ||
| + getItemByIndex(index) { | ||
| + let storeObject = this.getAll(); | ||
| + return storeObject[index]; | ||
| + } | ||
| + | ||
| + setItemByIndex(index, mutatedItem) { | ||
| + let storeObject = this.getAll(); | ||
| + let mutatedStoreObject = storeObject; | ||
| + mutatedStoreObject[index] = mutatedItem; | ||
| + this.setAll(mutatedStoreObject); | ||
| + } | ||
| + | ||
| + getItemsByProperty(propertyKey, matchingValue) { | ||
| + let storeObject = this.getAll(); | ||
| + let results = storeObject.filter(item => { | ||
| + return item[propertyKey] === matchingValue; | ||
| + }); | ||
| + return results; | ||
| + } | ||
| + | ||
| + setItemByProperty(propertyKey, matchingValue, modifiedItem) { | ||
| + let storeObject = this.getAll(); | ||
| + let indexOfitemToModify = storeObject.findIndex(item => { | ||
| + return item[propertyKey] == matchingValue; | ||
| + }); | ||
| + storeObject[indexOfitemToModify] = modifiedItem; | ||
| + this.setAll(storeObject); | ||
| + } | ||
| + | ||
| + delete() { | ||
| + localStorage.removeItem(this.storeName); | ||
| + } | ||
| + | ||
| + reset() { | ||
| + localStorage[this.storeName] = JSON.stringify([]); | ||
| + } | ||
| +} | ||
| + | ||
| +// http://stackoverflow.com/questions/12604062/javascript-array-indexof-doesnt-search-objects | ||
| +function indexOfWithObject(arr, value) { | ||
| + var a; | ||
| + for (var i=0, iLen=arr.length; i<iLen; i++) { | ||
| + a = arr[i]; | ||
| + if (a === value) return i; | ||
| + if (typeof a == 'object') { | ||
| + if (compareObj(arr[i], value)) { | ||
| + return i; | ||
| + } | ||
| + } else { | ||
| + // deal with other types | ||
| + } | ||
| + } | ||
| + return -1; | ||
| + // Extremely simple function, expects the values of all | ||
| + // enumerable properties of both objects to be primitives. | ||
| + function compareObj(o1, o2, cease) { | ||
| + var p; | ||
| + if (typeof o1 == 'object' && typeof o2 == 'object') { | ||
| + for (p in o1) { | ||
| + if (o1[p] != o2[p]) return false; | ||
| + } | ||
| + if (cease !== true) { | ||
| + compareObj(o2, o1, true); | ||
| + } | ||
| + return true; | ||
| + } | ||
| + } | ||
| +} |
| @@ -0,0 +1,37 @@ | ||
| +/* | ||
| + Not functional yet due to reliance upon | ||
| + browser-specific localStorage implementation. | ||
| +*/ | ||
| + | ||
| +'use strict'; | ||
| + | ||
| +let test = require('tape'); | ||
| +let LocalStore = require('./localStore'); | ||
| + | ||
| +test('getAll() test', t => { | ||
| + t.plan(1); | ||
| + let store = new LocalStore('test'); | ||
| + t.equal(store.getAll(), []); | ||
| +}); | ||
| + | ||
| +test('setAll() test', t => { | ||
| + t.plan(1); | ||
| + let store = new LocalStore('test'); | ||
| + store.setAll([1,2,3]); | ||
| + t.equal(store.getAll(), [1,2,3]); | ||
| +}); | ||
| + | ||
| +test('addItem(item) test', t => { | ||
| + t.plan(1); | ||
| + let store = new LocalStore('test'); | ||
| + store.addItem({}); | ||
| + t.equal(store.getAll(), [{}]); | ||
| +}); | ||
| + | ||
| +test('removeItem(item) test', t => { | ||
| + t.plan(1); | ||
| + let store = new LocalStore('test'); | ||
| + store.addItem({}); | ||
| + store.removeItem({}); | ||
| + t.equal(store.removeItem({}), []); | ||
| +}); |
0 comments on commit
69e5b0d