UNPKG

4.67 kBJavaScriptView Raw
1!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).VueStorage=t()}(this,function(){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}function t(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}function n(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(t);e&&(o=o.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),n.push.apply(n,o)}return n}function i(r){for(var e=1;e<arguments.length;e++){var i=null!=arguments[e]?arguments[e]:{};e%2?n(Object(i),!0).forEach(function(e){var t,n,o;t=r,o=i[n=e],n in t?Object.defineProperty(t,n,{value:o,enumerable:!0,configurable:!0,writable:!0}):t[n]=o}):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(i)):n(Object(i)).forEach(function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(i,e))})}return r}var a={},s=new(function(){function e(){o(this,e),Object.defineProperty(this,"length",{get:function(){return Object.keys(a).length}})}return t(e,[{key:"getItem",value:function(e){return e in a?a[e]:null}},{key:"setItem",value:function(e,t){return a[e]=t,!0}},{key:"removeItem",value:function(e){return!!(e in a)&&delete a[e]}},{key:"clear",value:function(){return a={},!0}},{key:"key",value:function(e){var t=Object.keys(a);return void 0!==t[e]?t[e]:null}}]),e}()),u={},c=function(){function e(){o(this,e)}return t(e,null,[{key:"on",value:function(e,t){void 0===u[e]&&(u[e]=[]),u[e].push(t)}},{key:"off",value:function(e,t){u[e].length?u[e].splice(u[e].indexOf(t),1):u[e]=[]}},{key:"emit",value:function(e){var t=e||window.event,n=function(t){try{return JSON.parse(t).value}catch(e){return t}};if(void 0!==t&&void 0!==t.key){var o=u[t.key];void 0!==o&&o.forEach(function(e){e(n(t.newValue),n(t.oldValue),t.url||t.uri)})}}}]),e}(),l=function(){function n(e){if(o(this,n),this.storage=e,this.options={namespace:"",events:["storage"]},Object.defineProperty(this,"length",{get:function(){return this.storage.length}}),"undefined"!=typeof window)for(var t in this.options.events)window.addEventListener?window.addEventListener(this.options.events[t],c.emit,!1):window.attachEvent?window.attachEvent("on".concat(this.options.events[t]),c.emit):window["on".concat(this.options.events[t])]=c.emit}return t(n,[{key:"setOptions",value:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};this.options=Object.assign(this.options,e)}},{key:"set",value:function(e,t){var n=2<arguments.length&&void 0!==arguments[2]?arguments[2]:null,o=JSON.stringify({value:t,expire:null!==n?(new Date).getTime()+n:null});this.storage.setItem(this.options.namespace+e,o)}},{key:"get",value:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,n=this.storage.getItem(this.options.namespace+e);if(null!==n)try{var o=JSON.parse(n);if(null===o.expire)return o.value;if(o.expire>=(new Date).getTime())return o.value;this.remove(e)}catch(e){return t}return t}},{key:"key",value:function(e){return this.storage.key(e)}},{key:"remove",value:function(e){return this.storage.removeItem(this.options.namespace+e)}},{key:"clear",value:function(){if(0!==this.length){for(var e=[],t=0;t<this.length;t++){var n=this.storage.key(t);!1!==new RegExp("^".concat(this.options.namespace,".+"),"i").test(n)&&e.push(n)}for(var o in e)this.storage.removeItem(e[o])}}},{key:"on",value:function(e,t){c.on(this.options.namespace+e,t)}},{key:"off",value:function(e,t){c.off(this.options.namespace+e,t)}}]),n}(),f="undefined"!=typeof window?window:global||{},e={install:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},n=i(i({},t),{},{storage:t.storage||"local",name:t.name||"ls"});if(n.storage&&-1===["memory","local","session"].indexOf(n.storage))throw new Error('Vue-ls: Storage "'.concat(n.storage,'" is not supported'));var o=null;switch(n.storage){case"local":o="localStorage"in f?f.localStorage:null;break;case"session":o="sessionStorage"in f?f.sessionStorage:null;break;case"memory":o=s}o||(o=s,console.error('Vue-ls: Storage "'.concat(n.storage,'" is not supported your system, use memory storage')));var r=new l(o);r.setOptions(Object.assign(r.options,{namespace:""},n||{})),e[n.name]=r,Object.defineProperty(e.prototype||e.config.globalProperties,"$".concat(n.name),{get:function(){return r}})}};return f.VueStorage=e});