yylib-quick-mobile
Version:
yylib-quick-mobile
74 lines (69 loc) • 2.62 kB
JavaScript
var _ = require('lodash');
var Cookies = require('js-cookie');
window._yylib_global_cache_ = {};
var DataUtil = {
getCache: function getCache(key) {
return window._yylib_global_cache_[key];
},
setCache: function setCache(key, value) {
window._yylib_global_cache_[key] = value;
},
removeCache: function removeCache(key) {
delete window._yylib_global_cache_[key];
},
localSave: function localSave(key, data) {
if (!window.localStorage) return;
window.localStorage.setItem(key, _.isPlainObject(data) || _.isArray(data) ? JSON.stringify(data) : data);
},
localRemove: function localRemove(keys) {
if (!window.localStorage) return;
if (_.isArray(keys)) {
for (var idx in keys) {
window.localStorage.removeItem(keys[idx]);
}
} else {
window.localStorage.removeItem(keys);
}
},
localRead: function localRead(key) {
if (!window.localStorage) return;
return window.localStorage[key] ? window.localStorage[key] : null;
},
localReadObject: function localReadObject(key) {
if (!window.localStorage) return;
return window.localStorage[key] ? JSON.parse(window.localStorage[key]) : null;
},
getCookie: function getCookie(key) {
return Cookies.get(key);
},
setCookie: function setCookie(key, value, options) {
return Cookies.set(key, value, options);
},
removeCookie: function removeCookie(key, options) {
return Cookies.remove(key, options);
},
localSessionSave: function localSave(key, data) {
if (!window.sessionStorage) return;
window.sessionStorage.setItem(key, _.isPlainObject(data) ? JSON.stringify(data) : data);
},
localSessionRemove: function localRemove(keys) {
if (!window.sessionStorage) return;
if (_.isArray(keys)) {
for (var idx in keys) {
window.sessionStorage.removeItem(keys[idx]);
}
} else {
window.sessionStorage.removeItem(keys);
}
},
localSessionRead: function localRead(key) {
if (!window.sessionStorage) return;
return window.sessionStorage[key] ? window.sessionStorage[key] : null;
},
localSessionReadObject: function localReadObject(key) {
if (!window.sessionStorage) return;
return window.sessionStorage[key] ? JSON.parse(window.sessionStorage[key]) : null;
}
};
module.exports = DataUtil;
;