-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtz-storage.min.js
1 lines (1 loc) · 2.32 KB
/
tz-storage.min.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).storage=t()}(this,function(){"use strict";function a(e,t){for(var n=0;n<t.length;n++){var a=t[n];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}return new(function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this._namespace="",this._defaultValue=null}return function(e,t,n){t&&a(e.prototype,t),n&&a(e,n)}(e,[{key:"config",value:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},t=e.namespace,n=e.defaultValue,a=void 0===n?null:n;this.namespace=void 0===t?"":t,this.defaultValue=a}},{key:"set",value:function(e,t){var n=2<arguments.length&&void 0!==arguments[2]?arguments[2]:0;return!!this._checkSupported&&(e=this._keyHandle(e),t=JSON.stringify({data:t,expire:n}),window.localStorage.setItem(e,t),!0)}},{key:"get",value:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},n=t.defaultValue,a=void 0===n?this._defaultValue:n,o=t.type;if(!this._checkSupported)return a;e=this._keyHandle(e);var r=JSON.parse(window.localStorage.getItem(e));if(null===r)return a;var i=r.data,u=r.expire,l=(new Date).getTime();if(u&&u<l)return window.localStorage.removeItem(e),a;try{if(o&&["number","string","boolean"].indexOf(o)<0)throw"Type Error: unsupported type param."}catch(e){return console.error(e),!1}switch(o){case"string":i="".concat(i);break;case"number":i=+i;break;case"boolean":i=!!i}return null===i?a:i}},{key:"remove",value:function(e){return!!this._checkSupported&&(e=this._keyHandle(e),window.localStorage.removeItem(e),!0)}},{key:"_keyHandle",value:function(e){return this._namespace+e}},{key:"create",value:function(){return new e}},{key:"_checkSupported",get:function(){var e=!(!window.localStorage||(window.localStorage.setItem("_testStorage","storage"),"storage"!==window.localStorage.getItem("_testStorage")));return window.localStorage&&window.localStorage.removeItem("_testStorage"),e}},{key:"isSupported",get:function(){return this._checkSupported}},{key:"namespace",get:function(){return this._namespace},set:function(e){this._namespace=e?"".concat(e,"."):""}},{key:"defaultValue",set:function(e){this._defaultValue=e}}]),e}())});