frontend/src/tools/ls.js

45 lines
1.0 KiB
JavaScript

"use strict";
exports.__esModule = true;
var Store = /** @class */ (function () {
function Store() {
this.store = window.localStorage;
this.prefix = "bk_";
}
Store.prototype.set = function (key, value, callback) {
if (callback === void 0) {
callback = function () {};
}
var val;
try {
val = JSON.stringify(value);
} catch (e) {
val = value;
}
this.store.setItem(this.prefix + key, val);
callback();
};
Store.prototype.get = function (key) {
if (!key) {
throw new Error("没有找到key。");
}
if (typeof key === "object") {
throw new Error("key不能是一个对象。");
}
var value = this.store.getItem(this.prefix + key);
var val;
if (value !== null) {
try {
val = JSON.parse(value);
} catch (e) {
val = value;
}
}
return val;
};
Store.prototype.remove = function (key) {
this.store.removeItem(this.prefix + key);
};
return Store;
})();
exports["default"] = new Store();