@flexi-ui/storage
Version:
3 lines (2 loc) • 1.58 kB
JavaScript
(function(i,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(i=typeof globalThis<"u"?globalThis:i||self,r(i["@flexi-ui/storage"]={}))})(this,function(i){"use strict";class r{constructor(e="./theme-storage.json"){this.filePath=e,(void 0)(this.filePath)||(void 0)(this.filePath,JSON.stringify({}))}readFile(){return JSON.parse((void 0)(this.filePath,"utf-8"))}writeFile(e){(void 0)(this.filePath,JSON.stringify(e,null,2))}get(e){return this.readFile()[e]||null}set(e,t){const s=this.readFile();s[e]=t,this.writeFile(s)}remove(e){const t=this.readFile();delete t[e],this.writeFile(t)}clear(){this.writeFile({})}}const l=60*60*24*7;class h{constructor(e="",t=localStorage){this.storage=t,this.prefixKey=e}getKey(e){return`${this.prefixKey}${e}`.toUpperCase()}set(e,t,s=l){const n=JSON.stringify({value:t,expire:s!==null?new Date().getTime()+s*1e3:null});this.storage.setItem(this.getKey(e),n)}get(e,t=null){const s=this.storage.getItem(this.getKey(e));if(s)try{const n=JSON.parse(s),{value:c,expire:a}=n;return a!==null&&a<Date.now()?(this.remove(e),t):c}catch{return t}return t}remove(e){this.storage.removeItem(this.getKey(e))}clear(){this.storage.clear()}}class u{constructor(){this.store=new Map}get(e){const t=this.store.get(e);return t?JSON.parse(t):null}set(e,t){this.store.set(e,JSON.stringify(t))}remove(e){this.store.delete(e)}clear(){this.store.clear()}}i.FileStorage=r,i.LocalStorage=h,i.MemoryStorage=u,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
//# sourceMappingURL=lib.umd.cjs.map