UNPKG

@jswork/next-local-expires

Version:

LocalStorage which can be set expires.

9 lines 1.04 kB
/*! * name: @jswork/next-local-expires * description: LocalStorage which can be set expires. * homepage: https://github.com/afeiship/next-local-expires * version: 1.0.0 * date: 2020-11-22 16:08:31 * license: MIT */ !function(){var r=(this||window||Function("return this")()).nx||require("@jswork/next"),t=(r.json||require("@jswork/next-json"),{prefix:"__EXPIRES__",expiresKey:"expires"}),i="@",e=r.declare("nx.LocalExpires",{methods:{init:function(e){e=r.mix(null,t,e);this.prefix=e.prefix,this.options=e},set:function(e,t,i){e=this.__key(e);localStorage.setItem(e,r.stringify(t)),localStorage.setItem(this.__expiresKey(),i)},get:function(e){e=this.__key(e);return Date.now()<localStorage.getItem(this.__expiresKey())?r.parse(localStorage.getItem(e)):(localStorage.removeItem(e),localStorage.removeItem(this.__expiresKey()),null)},__key:function(e){var t=this.prefix;return t?[t,i,e].join(""):e},__expiresKey:function(){return this.__key(this.options.expiresKey)}}});"undefined"!=typeof module&&module.exports&&(module.exports=e)}();