UNPKG

vuejs-local-storage

Version:

Vue plugin for work with LocalStorage from Vue context

3 lines (2 loc) 3.26 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e["vue-ls"]=n()}(this,function(){"use strict";function e(e){function n(n){n(e.newValue?JSON.parse(e.newValue).value:e.newValue,e.oldValue?JSON.parse(e.oldValue).value:e.oldValue,e.url||e.uri)}if(e||(e=window.event),"undefined"!=typeof e&&"undefined"!=typeof e.key){var t=a[e.key];"undefined"!=typeof t&&t.forEach(n)}}var n={},t={getItem:function(e){return e in n?n[e]:null},setItem:function(e,t){return n[e]=t,!0},removeItem:function(e){var t=e in n;return!!t&&delete n[e]},clear:function(){return n={},!0},key:function(e){var t=Object.keys(n);return"undefined"!=typeof t[e]?t[e]:null}};Object.defineProperty(t,"length",{get:function(){return Object.keys(n).length}});var o=function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")},i=function(){function e(e,n){for(var t=0;t<n.length;t++){var o=n[t];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(n,t,o){return t&&e(n.prototype,t),o&&e(n,o),n}}(),r=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},a={},s=function(){function n(t,i){if(o(this,n),this.storage=t,this.options=r({namespace:"",events:["storage"]},i||{}),Object.defineProperty(this,"length",{get:function(){return this.storage.length}}),"undefined"!=typeof window)for(var a in this.options.events)window.addEventListener?window.addEventListener(this.options.events[a],e,!1):window.attachEvent?window.attachEvent("on"+this.options.events[a],e):window["on"+this.options.events[a]]=e}return i(n,[{key:"set",value:function(e,n){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;this.storage.setItem(this.options.namespace+e,JSON.stringify({value:n,expire:null!==t?(new Date).getTime()+t:null}))}},{key:"get",value:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,t=this.storage.getItem(this.options.namespace+e);if(null!==t){var o=JSON.parse(t);if(null===o.expire)return o.value;if(o.expire>=(new Date).getTime())return o.value;this.remove(e)}return n}},{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=[],n=0;n<this.length;n++){var t=this.storage.key(n),o=new RegExp("^"+this.options.namespace+".+","i");o.test(t)!==!1&&e.push(t)}for(var i in e)this.storage.removeItem(e[i])}}},{key:"on",value:function(e,n){a[this.options.namespace+e]?a[this.options.namespace+e].push(n):a[this.options.namespace+e]=[n]}},{key:"off",value:function(e,n){var t=a[this.options.namespace+e];t.length>1?t.splice(t.indexOf(n),1):a[this.options.namespace+e]=[]}}]),n}(),u="undefined"!=typeof window&&"localStorage"in window?window.localStorage:t,l=new s(u),f={install:function(e,n){l.options=r(l.options,{namespace:""},n||{}),e.ls=l,Object.defineProperty(e.prototype,"$ls",{get:function(){return l}})}};return"undefined"!=typeof window&&(window.VueLocalStorage=f),f}); //# sourceMappingURL=vue-ls.min.js.map