-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.storageapi.min.js
2 lines (2 loc) · 7.11 KB
/
jquery.storageapi.min.js
1
2
/* jQuery Storage API Plugin 1.8.1 https://github.com/julien-maurel/jQuery-Storage-API */
!function(e){"function"==typeof define&&define.amd?define(["jquery"],e):e("object"==typeof exports?require("jquery"):jQuery)}(function(e){function t(){var t,r,i,n=this._type,o=arguments.length,s=window[n],a=arguments,u=a[0];if(1>o)throw Error("Minimum 1 argument must be given");if(e.isArray(u)){r={};for(var c in u){t=u[c];try{r[t]=JSON.parse(s.getItem(t))}catch(l){r[t]=s.getItem(t)}}return r}if(1!=o){try{r=JSON.parse(s.getItem(u))}catch(l){throw new ReferenceError(u+" is not defined in this storage")}for(var c=1;o-1>c;c++)if(r=r[a[c]],void 0===r)throw new ReferenceError([].slice.call(a,1,c+1).join(".")+" is not defined in this storage");if(e.isArray(a[c])){i=r,r={};for(var f in a[c])r[a[c][f]]=i[a[c][f]];return r}return r[a[c]]}try{return JSON.parse(s.getItem(u))}catch(l){return s.getItem(u)}}function r(){var t,r,i=this._type,n=arguments.length,o=window[i],s=arguments,a=s[0],u=s[1],c={};if(1>n||!e.isPlainObject(a)&&2>n)throw Error("Minimum 2 arguments must be given or first parameter must be an object");if(e.isPlainObject(a)){for(var l in a)t=a[l],e.isPlainObject(t)||this.alwaysUseJson?o.setItem(l,JSON.stringify(t)):o.setItem(l,t);return a}if(2==n)return"object"==typeof u||this.alwaysUseJson?o.setItem(a,JSON.stringify(u)):o.setItem(a,u),u;try{r=o.getItem(a),null!=r&&(c=JSON.parse(r))}catch(f){}r=c;for(var l=1;n-2>l;l++)t=s[l],r[t]&&e.isPlainObject(r[t])||(r[t]={}),r=r[t];return r[s[l]]=s[l+1],o.setItem(a,JSON.stringify(c)),c}function i(){var t,r,i=this._type,n=arguments.length,o=window[i],s=arguments,a=s[0];if(1>n)throw Error("Minimum 1 argument must be given");if(e.isArray(a)){for(var u in a)o.removeItem(a[u]);return!0}if(1==n)return o.removeItem(a),!0;try{t=r=JSON.parse(o.getItem(a))}catch(c){throw new ReferenceError(a+" is not defined in this storage")}for(var u=1;n-1>u;u++)if(r=r[s[u]],void 0===r)throw new ReferenceError([].slice.call(s,1,u).join(".")+" is not defined in this storage");if(e.isArray(s[u]))for(var l in s[u])delete r[s[u][l]];else delete r[s[u]];return o.setItem(a,JSON.stringify(t)),!0}function n(t){var r=a.call(this);for(var n in r)i.call(this,r[n]);if(t)for(var n in e.namespaceStorages)u(n)}function o(){var r=arguments.length,i=arguments,n=i[0];if(0==r)return 0==a.call(this).length;if(e.isArray(n)){for(var s=0;s<n.length;s++)if(!o.call(this,n[s]))return!1;return!0}try{var u=t.apply(this,arguments);e.isArray(i[r-1])||(u={totest:u});for(var s in u)if(!(e.isPlainObject(u[s])&&e.isEmptyObject(u[s])||e.isArray(u[s])&&!u[s].length)&&u[s])return!1;return!0}catch(c){return!0}}function s(){var r=arguments.length,i=arguments,n=i[0];if(1>r)throw Error("Minimum 1 argument must be given");if(e.isArray(n)){for(var o=0;o<n.length;o++)if(!s.call(this,n[o]))return!1;return!0}try{var a=t.apply(this,arguments);e.isArray(i[r-1])||(a={totest:a});for(var o in a)if(void 0===a[o]||null===a[o])return!1;return!0}catch(u){return!1}}function a(){var r=this._type,i=arguments.length,n=window[r],o=arguments,s=[],a={};if(a=i>0?t.apply(this,o):n,a&&a._cookie)for(var u in e.cookie())""!=u&&s.push(u.replace(a._prefix,""));else for(var c in a)a.hasOwnProperty(c)&&s.push(c);return s}function u(t){if(!t||"string"!=typeof t)throw Error("First parameter must be a string");g?(window.localStorage.getItem(t)||window.localStorage.setItem(t,"{}"),window.sessionStorage.getItem(t)||window.sessionStorage.setItem(t,"{}")):(window.localCookieStorage.getItem(t)||window.localCookieStorage.setItem(t,"{}"),window.sessionCookieStorage.getItem(t)||window.sessionCookieStorage.setItem(t,"{}"));var r={localStorage:e.extend({},e.localStorage,{_ns:t}),sessionStorage:e.extend({},e.sessionStorage,{_ns:t})};return e.cookie&&(window.cookieStorage.getItem(t)||window.cookieStorage.setItem(t,"{}"),r.cookieStorage=e.extend({},e.cookieStorage,{_ns:t})),e.namespaceStorages[t]=r,r}function c(e){var t="jsapi";try{return window[e]?(window[e].setItem(t,t),window[e].removeItem(t),!0):!1}catch(r){return!1}}var l="ls_",f="ss_",g=c("localStorage"),h={_type:"",_ns:"",_callMethod:function(e,t){var r=[],t=Array.prototype.slice.call(t),i=t[0];return this._ns&&r.push(this._ns),"string"==typeof i&&-1!==i.indexOf(".")&&(t.shift(),[].unshift.apply(t,i.split("."))),[].push.apply(r,t),e.apply(this,r)},alwaysUseJson:!1,get:function(){return this._callMethod(t,arguments)},set:function(){var t=arguments.length,i=arguments,n=i[0];if(1>t||!e.isPlainObject(n)&&2>t)throw Error("Minimum 2 arguments must be given or first parameter must be an object");if(e.isPlainObject(n)&&this._ns){for(var o in n)this._callMethod(r,[o,n[o]]);return n}var s=this._callMethod(r,i);return this._ns?s[n.split(".")[0]]:s},remove:function(){if(arguments.length<1)throw Error("Minimum 1 argument must be given");return this._callMethod(i,arguments)},removeAll:function(e){return this._ns?(this._callMethod(r,[{}]),!0):this._callMethod(n,[e])},isEmpty:function(){return this._callMethod(o,arguments)},isSet:function(){if(arguments.length<1)throw Error("Minimum 1 argument must be given");return this._callMethod(s,arguments)},keys:function(){return this._callMethod(a,arguments)}};if(e.cookie){window.name||(window.name=Math.floor(1e8*Math.random()));var m={_cookie:!0,_prefix:"",_expires:null,_path:null,_domain:null,setItem:function(t,r){e.cookie(this._prefix+t,r,{expires:this._expires,path:this._path,domain:this._domain})},getItem:function(t){return e.cookie(this._prefix+t)},removeItem:function(t){return e.removeCookie(this._prefix+t)},clear:function(){for(var t in e.cookie())""!=t&&(!this._prefix&&-1===t.indexOf(l)&&-1===t.indexOf(f)||this._prefix&&0===t.indexOf(this._prefix))&&e.removeCookie(t)},setExpires:function(e){return this._expires=e,this},setPath:function(e){return this._path=e,this},setDomain:function(e){return this._domain=e,this},setConf:function(e){return e.path&&(this._path=e.path),e.domain&&(this._domain=e.domain),e.expires&&(this._expires=e.expires),this},setDefaultConf:function(){this._path=this._domain=this._expires=null}};g||(window.localCookieStorage=e.extend({},m,{_prefix:l,_expires:3650}),window.sessionCookieStorage=e.extend({},m,{_prefix:f+window.name+"_"})),window.cookieStorage=e.extend({},m),e.cookie.raw=!1,e.cookieStorage=e.extend({},h,{_type:"cookieStorage",setExpires:function(e){return window.cookieStorage.setExpires(e),this},setPath:function(e){return window.cookieStorage.setPath(e),this},setDomain:function(e){return window.cookieStorage.setDomain(e),this},setConf:function(e){return window.cookieStorage.setConf(e),this},setDefaultConf:function(){return window.cookieStorage.setDefaultConf(),this}})}e.initNamespaceStorage=function(e){return u(e)},g?(e.localStorage=e.extend({},h,{_type:"localStorage"}),e.sessionStorage=e.extend({},h,{_type:"sessionStorage"})):(e.localStorage=e.extend({},h,{_type:"localCookieStorage"}),e.sessionStorage=e.extend({},h,{_type:"sessionCookieStorage"})),e.namespaceStorages={},e.removeAllStorages=function(t){e.localStorage.removeAll(t),e.sessionStorage.removeAll(t),e.cookieStorage&&e.cookieStorage.removeAll(t),t||(e.namespaceStorages={})},e.alwaysUseJsonInStorage=function(t){h.alwaysUseJson=t,e.localStorage.alwaysUseJson=t,e.sessionStorage.alwaysUseJson=t,e.cookieStorage&&(e.cookieStorage.alwaysUseJson=t)}});