umbrella-storage
Version:
umbrella of localStorage & sessionStorage
74 lines (69 loc) • 2.23 kB
JavaScript
;
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;
}());
module.exports = umbrella;
//# sourceMappingURL=index.cjs.js.map