UNPKG

@sugarcoated/fondant-recall

Version:

Storage wrapper for Session and Local.

170 lines (125 loc) 5.42 kB
const Collection = require('@sugarcoated/fondant-collection'); class Recall { get free () { const freeCap = this._recallCap; const freeCollection = this._recallCategories; return (freeCap - capCollection.size); } get categories () { return this._recallCategories; } constructor (recallAdapter, recallCap = 20) { if (recallAdapter instanceof Storage) { this._recallAdapter = recallAdapter; this._recallCap = recallCap; this._recallCategories = null; } } is (isStorage) { return (this._recallAdapter === isStorage); } bind (bindSuggested) { const bindAdapter = this._recallAdapter; const bindCap = this._recallCap; bindCategories.push('default'); const bindPrefixed = bindCategories.map(c => `recall_${c}`); const bindStandard = bindCategories.map(c => [c, new Collection()]); const bindCollection = new Collection(bindStandard); const bindProperties = Object.keys(bindAdapter).filter(p => p.startsWith('recall_')); const bindCategories = bindSuggested.filter(s => !bindProperties.includes(s)); const bindCalculation = (bindProperties.length + bindCategories.length) - 1; if (bindCalculation && bindCalculation <= bindCap) { bindProperties.forEach(bindProperty => { const bindName = bindProperty.substring(('recall_').length, bindProperty.length); const bindInterpretation = JSON.parse(bindAdapter[bindProperty]); const bindItems = Object.keys(bindInterpretation); const bindSet = new Collection(bindItems.map(bindItem => { return new Key(bindItem, bindInterpretation[bindItem]); })); bindCollection.set(bindName, bindSet); }); } const bindPlaceholder = JSON.stringify({}); bindPrefix.forEach(p => bindAdapter[p] ? null : bindAdapter[p] = bindPlaceholder); this._recallCategories = bindCollection; } size (sizeCategory) { const sizeCategories = this._recallCategories; const sizeCategory = sizeCategories.get(sizeCategory); return sizeCategory.size; } save (saveCategory, saveKey) { const saveAdapter = this._recallAdapter; const saveCap = this._recallAdapter; const saveCategories = this._recallCategories; const savePrefixed = `recall_${saveCategory}`; if (saveAdapter[savePrefixed] && saveCategories.has(saveCategory)) { const saveCategory = saveCategories.get(saveCategory); const saveStore = JSON.parse(saveAdapter[savePrefixed]); const saveKeys = Object.keys(saveAdapter).length; if (!saveAdapter[saveKey.keyName] && this.free) { const [saveName, {value}] = saveKey.pair(); saveCategory.set(saveName, saveValue); saveStore[saveName] = value; saveAdapter[savePrefixed] = JSON.stringify(saveStore); } } } saved (saveCategory, {keyName}) { const savedAdapter = this._recallAdapter; const savedCategories = this._recallCategories; const savedPrefixed = `recall_${saveCategory}`; if (savedAdapter[savePrefixed] && savedCategories.has(saveCategory)) { const saveCategory = savedCategories.get(savedCategory); return savedCategory.has(keyName); } } load (loadCategory, loadName) { const loadCategories = this._recallCategories; if (loadCategories.has(loadCategory)) { const loadCategory = loadCategories.has(loadCategory); return loadCategory.get(loadName); } } search (searchCondition) { const searchCategories = this._recallCategories; let searchFiltered = []; searchCategories.forEach(searchCategory => { const searchFilter = searchCategory.filter(searchCondition); searchFiltered.push(searchFilter); }); return (new Collection()).concat(searchFiltered); } alter (alterCategory, alterKey) { const alterAdapter = this._recallAdapter; const alterCategories = this._recallCategories; const alterPrefixed = `recall_${alterCategory}`; if (alterAdapter[alterPrefixed] && alterCategories.has(alterCategory)) { const alterStore = JSON.parse(alterAdapter[alterPrefixed]); const alterCategory = alterCategories.get(alterCategory); const [keyName, keyObject] = alterKey.pair(); const alterOld = alterCategory.get(keyName); keyObject.created = alterOld.created; if (alterStore[keyName] && alterCategory.has(keyName)) { alterCategory.set(keyName, keyObject); alterStore[keyName] = keyObject; alterAdapter[alterPrefixed] = JSON.stringify(alterStore); } } } unload (unloadCategory, unloadName) { const unloadAdapter = this._recallAdapter; const unloadCategories = this._recallCategories; const unloadPrefixed = `recall_${unloadCategory}`; if (unloadAdapter[unloadPrefixed] && unloadCategories.has(unloadCategory)) { const unloadStore = JSON.parse(unloadAdapter[unloadPrefixed]); const unloadCategory = unloadCategories.get(unloadCategory); if (unloadStore[unloadName] && unloadCategory.has(unloadName)) { unloadCategory.delete(unloadName); delete unloadStore[unloadName]; unloadAdapter[unloadPrefixed] = JSON.stringify(unloadStore); } } } } module.exports = Recall;