UNPKG

3.91 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t.eg=t.eg||{},t.eg.Persist=e())}(this,function(){"use strict";var r="undefined"!=typeof window&&window||{},o=r.console,a=(r.document,r.history),i=r.location,t=r.navigator,e=r.parseFloat,n=r.performance,s=null,f=null;try{s=r.localStorage,f=r.sessionStorage}catch(t){}var c="___persist___",u="state"+c,l="depths",p="lastUrl",h=n&&n.navigation,v=h&&h.TYPE_NAVIGATE||0,d=(h&&h.TYPE_RELOAD,h&&h.TYPE_BACK_FORWARD||2),g=a&&"replaceState"in a&&"state"in a;function y(t){if(t){var e="__tmp__"+c;try{return t.setItem(e,c),t.getItem(e)===c}catch(t){return!1}}}var w,m=(y(f)?w=f:y(s)&&(w=s),w);function E(){console.warn("window.history or session/localStorage has no valid format data to be handled in persist.")}function _(){return m}function x(t){var e,n;if(m?n=m.getItem(t):a.state?"object"==typeof a.state&&null!==a.state?n=a.state[t]:E():n=a.state,null===n)return{};var r="string"==typeof n&&0<n.length&&"null"!==n;try{var i=!("object"!=typeof(e=JSON.parse(n))||e instanceof Array);if(!r||!i)throw new Error}catch(t){E(),e={}}return e}function S(t,e){if(g||m){var n=x(t)[e];return"null"!==n&&void 0!==n||(n=null),n}}function A(t,e){if(m)e?m.setItem(t,JSON.stringify(e)):m.removeItem(t);else try{var n=a&&null!=a.state?a.state:{};a&&"object"==typeof n?(n[t]=JSON.stringify(e),a.replaceState(n,document.title,i.href)):console.warn("To use a history object, it must be an object that is not a primitive type.")}catch(t){console.warn(t.message)}e?r[c]=!0:delete r[c]}function R(t,e,n){if(g||m){var r=x(t);r[e]=n,A(t,r)}}function b(t){A(t,null)}var N,O,j,I,k,P=t?t.userAgent:"",T=(N=new RegExp("iPhone|iPad","i").test(P),O=new RegExp("Mac","i").test(P)&&!new RegExp("Chrome","i").test(P)&&new RegExp("Apple","i").test(P),j=new RegExp("Android ","i").test(P),I=new RegExp("wv; |inapp;","i").test(P),k=j?e(new RegExp("(Android)\\s([\\d_\\.]+|\\d_0)","i").exec(P)[2]):void 0,!(N||O||j&&(k<=4.3&&I||k<3)));function D(){return i?i.href.split("#")[0]:""}function B(t){return t+c}var J="";function K(t,e,n){var r=t;r||(r=isNaN(e[0])?{}:[]);var i=e.shift();return 0===e.length?(r instanceof Array&&isNaN(i)&&o.warn("Don't use key string on array"),n(r,i)):r[i]=K(r[i],e,n),r}function Y(e,n){try{R(u,e,n)}catch(t){if(!L())throw t;e===p?Y(e,n):e===l&&Y(e,n&&n.slice(1))}}function C(t){return S(u,t)}function F(t){void 0===t&&(t=0);var e=D();if(J!==e){J=e;var n=C(l)||[];if(t===d){~n.indexOf(J)&&Y(p,J)}else{var r=C(p);if(b(B(J)),t===v&&e!==r){var i=n.indexOf(r);n.splice(i+1,n.length).forEach(function(t){b(B(t))});var o=n.indexOf(J);~o&&n.splice(o,1)}n.indexOf(e)<0&&n.push(e),Y(l,n),Y(p,e)}}}function L(){var t=C(l)||[],e=t.splice(0,1);if(!e.length)return!1;var n=e[0];return b(B(n)),!(J===n&&(Y(p,J=""),!t.length))&&(Y(l,t),!0)}"onpopstate"in r&&r.addEventListener("popstate",function(){F(d)});var M=function(){var t=function(){function t(t){this.key=t||""}t.clear=function(){(C(l)||[]).forEach(function(t){b(B(t))}),b(u),J=""},t.isNeeded=function(){return T};var e=t.prototype;return e.get=function(t){F(v);var e=S(B(D()),this.key);if(!t||0===t.length)return e;for(var n=t.split("."),r=e,i=!0,o=0;o<n.length;o++){if(!r){i=!1;break}r=r[n[o]]}return i&&null!=r?r:null},e.set=function(e,n){F(v);var t=this.key,r=B(D()),i=S(r,t);try{if(0===e.length)R(r,t,n);else R(r,t,K(i,e.split("."),function(t,e){t[e]=n}))}catch(t){if(!L())throw t;this.set(e,n)}return this},e.remove=function(e){F(v);var t=this.key,n=B(D()),r=S(n,t);try{if(0===e.length)R(n,t,null);else R(n,t,K(r,e.split("."),function(t,e){"object"==typeof t&&delete t[e]}))}catch(t){if(!L())throw t;this.remove(e)}return this},t}();return t.VERSION="2.4.0",t.StorageManager={reset:b,setStateByKey:R,getStateByKey:S,getStorage:_},t}();return F(n&&n.navigation&&n.navigation.type),M.updateDepth=F,M});
2//# sourceMappingURL=persist.min.js.map