UNPKG

umbrella-storage

Version:

umbrella of localStorage & sessionStorage

80 lines (74 loc) 2.77 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = global || self, global.umbrella = factory()); }(this, (function () { 'use strict'; function isJsonString(str) { try { JSON.parse(str); return true; } catch (error) { return false; } } //# sourceMappingURL=utils.js.map var storage = (function () { function storage() { } storage.setStorage = function (storageType, key, value) { key = this.transformKey(key); if (typeof value !== 'string') { value = JSON.stringify(value); } window[storageType].setItem(key, value); }; storage.getStorage = function (storageType, key, withoutParse) { var value = window[storageType].getItem(this.transformKey(key)); if (!value) return null; if (withoutParse) return value; if (isJsonString(value)) return JSON.parse(value); return value; }; storage.removeStorage = function (storageType, key) { window[storageType].removeItem(this.transformKey(key)); }; storage.transformKey = function (key) { return this.prefix + "@" + key; }; storage.prefix = location.origin + location.pathname; return storage; }()); //# sourceMappingURL=storage.js.map var umbrella = (function () { function umbrella() { } umbrella.config = function (prefix) { storage.prefix = prefix; }; umbrella.setLocalStorage = function (key, value) { storage.setStorage('localStorage', key, value); }; umbrella.getLocalStorage = function (key, withoutParse) { return storage.getStorage('localStorage', key, withoutParse); }; umbrella.removeLocalStorage = function (key) { storage.removeStorage('localStorage', key); }; umbrella.setSessionStorage = function (key, value) { storage.setStorage('sessionStorage', key, value); }; umbrella.getSessionStorage = function (key, withoutParse) { return storage.getStorage('sessionStorage', key, withoutParse); }; umbrella.removeSessionStorage = function (key) { storage.removeStorage('sessionStorage', key); }; return umbrella; }()); return umbrella; }))); //# sourceMappingURL=index.umd.js.map