UNPKG

@belloai/ls

Version:
138 lines (121 loc) 3.21 kB
import _classCallCheck from '@babel/runtime/helpers/classCallCheck'; import _createClass from '@babel/runtime/helpers/createClass'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import store from 'store'; import { decode, encode } from 'js-base64'; var Base64Store = /*#__PURE__*/function () { function Base64Store(props) { _classCallCheck(this, Base64Store); _defineProperty(this, "notClearKeys", [// 退出登录时候需要保留的 ]); _defineProperty(this, "passLocalStorageB64Key", 'passLocalStorageB64'); _defineProperty(this, "isBase64", true); var _ref = props || {}, notClearKeys = _ref.notClearKeys, _ref$isBase = _ref.isBase64, isBase64 = _ref$isBase === void 0 ? true : _ref$isBase; this.notClearKeys = notClearKeys || []; this.isBase64 = isBase64; } _createClass(Base64Store, [{ key: "get", value: function get(key) { return this.b64ToUtf8(store.get(key)); } }, { key: "remove", value: function remove(key) { store.remove(key); } }, { key: "set", value: function set(key, val) { return store.set(key, this.utf8ToB64(val)); } }, { key: "clear", value: function clear() { store.clearAll(); } }, { key: "getKeys", value: function getKeys() { var keys = []; store.each(function (item, key) { keys.push(key); }); return keys; } }, { key: "clearAllExcept", value: function clearAllExcept(item) { var temp = []; if (typeof item === 'string') { temp.push({ key: item, val: store.get(item) }); } else if (Array.isArray(item)) { item.forEach(function (key) { temp.push({ key: key, val: store.get(key) }); }); } else { this.notClearKeys.forEach(function (key) { temp.push({ key: key, val: store.get(key) }); }); } store.clearAll(); temp.forEach(function (obj) { if (obj.val) { store.set(obj.key, obj.val); } }); } }, { key: "b64ToUtf8", value: function b64ToUtf8(str) { var utfStr = str; try { utfStr = decodeURIComponent(escape(decode(str))); } catch (error) {// console.warn(error) } try { return JSON.parse(utfStr); } catch (error) {// console.warn(error) } return utfStr; } }, { key: "utf8ToB64", value: function utf8ToB64(str) { if (!this.isPass()) { try { return encode(unescape(encodeURIComponent(JSON.stringify(str)))); } catch (error) {// console.warn(error) } } return str; } }, { key: "isPass", value: function isPass() { return !!store.get(this.passLocalStorageB64Key) || !this.isBase64; } }]); return Base64Store; }(); export { Base64Store as default }; if(typeof window !== 'undefined') { window.pgk__belloai_ls_version = '0.0.1' } /*! * @belloai/ls v0.0.1 * (c) 2021 thomas-ballo <liurongliang@balloai.com> * @license MIT */