@flexi-ui/storage
Version:
This package is to manage the local, file, and memory storage
3 lines (2 loc) • 1.81 kB
JavaScript
(function(i,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(i=typeof globalThis<"u"?globalThis:i||self,s(i["@flexi-ui/storage"]={}))})(this,(function(i){"use strict";var g=Object.defineProperty;var d=(i,s,o)=>s in i?g(i,s,{enumerable:!0,configurable:!0,writable:!0,value:o}):i[s]=o;var a=(i,s,o)=>d(i,typeof s!="symbol"?s+"":s,o);class s{constructor(e="./storage.json"){a(this,"filePath");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 r=this.readFile();r[e]=t,this.writeFile(r)}remove(e){const t=this.readFile();delete t[e],this.writeFile(t)}clear(){this.writeFile({})}}const o=3600*24*7;class u{constructor(e="",t=localStorage){a(this,"storage");a(this,"prefixKey");this.storage=t,this.prefixKey=e}getKey(e){return`${this.prefixKey}${e}`.toUpperCase()}set(e,t,r=o){const n=JSON.stringify({value:t,expire:r!==null?new Date().getTime()+r*1e3:null});this.storage.setItem(this.getKey(e),n)}get(e,t=null){const r=this.storage.getItem(this.getKey(e));if(r)try{const n=JSON.parse(r),{value:f,expire:h}=n;return h!==null&&h<Date.now()?(this.remove(e),t):f}catch{return t}return t}remove(e){this.storage.removeItem(this.getKey(e))}clear(){this.storage.clear()}}class c{constructor(){a(this,"store");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=s,i.LocalStorage=u,i.MemoryStorage=c,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=lib.umd.cjs.map