jsi-just-save-it
Version:
JSI[just,save,it] the most SUPER SUPREME storage wrapper
10 lines (9 loc) • 1.7 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
*/
const JSI=function(){"use strict";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 r=localStorage;return class{static set(e,a,s={}){const o=s.prefix?s.prefix:"_JSI_",i=s.type?"local"==s.type?localStorage:sessionStorage:r;if("string"==typeof a)return void i.setItem(`${o}${e}`,a);"string"!=typeof e&&t.log({name:"SetItem Key Error!",message:`'${e}' can't be used as key name, use a 'string' instead`}),a||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})}(a,s.expire);i.setItem(`${o}${e}`,l)}static get(e){return function(e,n){const r=localStorage.getItem(e),a=JSON.parse(r);if(null!==r&&null!=r)return null==a.expTime||a.expTime<=(new Date).getTime()&&t.log({name:"Key Expired Error!",message:"the key is expired :( try setting a new one :)"}),a.value;t.log({name:"GetItem Key Error!",message:`returned data can't be '${r}', 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()}}}();