UNPKG

browser-storage-utilities

Version:

A front-end library that provides utility methods to facilitate CRUD operations to data stored in the browser, and more.

207 lines 4.62 kB
Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); const storage_types_1 = require("./enums/storage-types"); const storage_base_1 = require("./base/storage-base"); const storage_item_1 = require("./models/storage-item"); /** * Provides base functionality for StorageUtilities. */ class StorageUtilitiesBase extends storage_base_1.StorageBase { constructor(settings) { super(settings); this.defaultSettings = settings; } /** * * @param settings */ setCustomSettings(settings) { this.setSettings(settings); } /** * */ resetSettings() { this.setSettings(exports.defaultSettings); } /** * * @param key * @param item * @param expiry * @param type */ addItem(key, item, expiry, type) { this.add(key, item, expiry, type); } /** * * @param key * @param type */ getItem(key, type) { return this.get(key, type); } /** * * @param key * @param type */ removeItem(key, type) { this.remove(key, type); } /** * * @param keys * @param type */ removeItems(keys, type) { this.removeAll(keys, type); } /** * * @param key * @param propName * @param newValue * @param type */ updateItemProp(key, propName, newValue, type) { return this.updateProp(key, propName, newValue, type); } /** * * @param key * @param propName * @param type */ removeItemProp(key, propName, type) { return this.removeProp(key, propName, type); } /** * * @param type */ clearAll(type) { this.clear(type); } /** * */ getStateObservable() { return this.getNotifierObservable(); } /** * * @param type */ getItems(type) { const items = new Array(); const storage = this.getStorage(type); for (let i = 0; i < storage.length; i++) { const key = storage.key(i); const json = storage.getItem(key); const obj = this._parsedJSON(json); const item = obj ? obj : json; items.push(new storage_item_1.StorageItem({ key, value: item.value || item, expiry: item.expiry })); } return items; } /** * * @param type */ getValues(type) { const values = new Array(); const storage = this.getStorage(type); for (let i = 0; i < storage.length; i++) { const key = storage.key(i); const json = storage.getItem(key); const obj = this._parsedJSON(json); const item = obj ? obj : json; values.push(item.value || item); } return values; } /** * * @param type */ getKeys(type) { const keys = new Array(); const storage = this.getStorage(type); for (let i = 0; i < storage.length; i++) { keys.push(storage.key(i)); } return keys; } /** * * @param key * @param id */ findOne(key, id) { // TODO: add implementation to return a single item from an Array return {}; } /** * * @param key */ findAll(key) { // TODO: add implementation to return all Array items by key return []; } /** * * @param key * @param item */ addOne(key, item) { // TODO: add implementation to add an item to Array } /** * * @param key * @param items */ addAll(key, items) { // TODO: add implementation to add all items to Array } /** * * @param key * @param id */ removeOne(key, id) { // TODO: add implementation to remove item from Array } /** * * @param key */ reset(key) { // TODO: add implementation to reset Array } _parsedJSON(json) { try { const obj = JSON.parse(json); if (_.isObject(obj)) { return obj; } } catch (e) { } return false; } } // default settings exports.defaultSettings = { keyPrefix: '', type: storage_types_1.StorageTypes.LOCAL // set to localStorage by default }; // create a singleton exports.default = new StorageUtilitiesBase(exports.defaultSettings); //# sourceMappingURL=storage-utilities-base.js.map