jsi-just-save-it
Version:
JSI[just,save,it] the most SUPER SUPREME storage wrapper
10 lines (9 loc) • 1.68 kB
JavaScript
/*!
* JSI[just,save,it] - v1.1.0
* the most SUPER SUPREME of the storage wrappers (͠≖ ͜ʖ͠≖)👌
* 2022 - RealFakeNerd
* @license MIT
*
* https://fsymbols.com/ fsymbols - symbol generator is used for the the symbols above
*/
class e{constructor(){return null==e.instance&&(e.instance=this),e.instance}log(e){const t=new Error;throw t.name=e.name,t.message=e.message,t}}const t=new e;function n(e){return(new Date).getTime()+e}Object.freeze(t);const a=localStorage;class r{static set(e,r,s={}){const o=s.prefix?s.prefix:"_JSI_",i=s.type?"local"==s.type?localStorage:sessionStorage:a;if("string"==typeof r)return void i.setItem(`${o}${e}`,r);"string"!=typeof e&&t.log({name:"SetItem Key Error!",message:`'${e}' can't be used as key name, use a 'string' instead`}),r||t.log({name:"SetItem Value Error!",message:"there's no data to be saved, try adding something."});let l=function(e,t=null){return JSON.stringify({value:e,expTime:null!==t?n(t):null})}(r,s.expire);i.setItem(`${o}${e}`,l)}static get(e){return function(e,n){const a=localStorage.getItem(e),r=JSON.parse(a);if(null!==a&&null!=a)return null==r.expTime||r.expTime<=(new Date).getTime()&&t.log({name:"Key Expired Error!",message:"the key is expired :( try setting a new one :)"}),r.value;t.log({name:"GetItem Key Error!",message:`returned data can't be '${a}', this happens if theres no key with the of '${e}'`})}(`_JSI_${e}`)}static remove(e){e in localStorage?localStorage.removeItem(`_JSI_${e}`):t.log({name:"RemoveItem Key Error!",message:`data can't be removed if isn't there, try changing '${e}' to another thing`})}static clear(){localStorage.clear()}}export{r as default};