forked from pamelafox/lscache
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lscache.min.js
1 lines (1 loc) · 2.73 KB
/
lscache.min.js
1
!function(a,b){var c={"function":!0,object:!0},d=c[typeof exports]&&exports&&!exports.nodeType&&exports,e=c[typeof module]&&module&&!module.nodeType&&module,f=e&&e.exports===d&&d;"function"==typeof define&&"object"==typeof define.amd&&define.amd?(a.lscache=b(),define([],b)):d&&e?f?e.exports=b():d.lscache=b():a.lscache=b()}(this,function(){function a(){var a="__lscachetest__",c=a;if(void 0!==o)return o;try{h(a,c),i(a),o=!0}catch(d){o=b(d)?!0:!1}return o}function b(a){return a&&"QUOTA_EXCEEDED_ERR"===a.name||"NS_ERROR_DOM_QUOTA_REACHED"===a.name||"QuotaExceededError"===a.name?!0:!1}function c(){return void 0===p&&(p=null!=window.JSON),p}function d(a){return a.replace(/[[\]{}()*+?.\\^$|]/g,"\\$&")}function e(a){return a+r}function f(){return Math.floor((new Date).getTime()/t)}function g(a){return localStorage.getItem(q+w+a)}function h(a,b){localStorage.removeItem(q+w+a),localStorage.setItem(q+w+a,b)}function i(a){localStorage.removeItem(q+w+a)}function j(a){for(var b=new RegExp("^"+q+d(w)+"(.*)"),c=localStorage.length-1;c>=0;--c){var f=localStorage.key(c);f=f&&f.match(b),f=f&&f[1],f&&f.indexOf(r)<0&&a(f,e(f))}}function k(a){var b=e(a);i(a),i(b)}function l(a){var b=e(a),c=g(b);if(c){var d=parseInt(c,s);return f()>=d}return!1}function m(a){return l(a)?(k(a),!0):void 0}function n(a,b){x&&"console"in window&&"function"==typeof window.console.warn&&(window.console.warn("lscache - "+a),b&&window.console.warn("lscache - The error was: "+b.message))}var o,p,q="lscache-",r="-cacheexpiration",s=10,t=6e4,u="_lscache-expiry-units-key",v=Math.floor(864e13/t),w="",x=!1,y={setExpiryUnitMs:function(a){var b=y.get(u);"number"!=typeof a&&(a="number"==typeof b?b:t),b!==a&&y.flush(),y.set(u,a),t=a},set:function(d,l,m){if(a()){if("string"!=typeof l){if(!c())return;try{l=JSON.stringify(l)}catch(o){return}}try{h(d,l)}catch(o){if(!b(o))return void n("Could not add item with key '"+d+"'",o);var p,q=[];j(function(a,b){var c=g(b);c=c?parseInt(c,s):v,q.push({key:a,size:(g(a)||"").length,expiration:c})}),q.sort(function(a,b){return b.expiration-a.expiration});for(var r=(l||"").length;q.length&&r>0;)p=q.pop(),n("Cache is full, removing item with key '"+d+"'"),k(p.key),r-=p.size;try{h(d,l)}catch(o){return void n("Could not add item with key '"+d+"', perhaps it's too big?",o)}}m?h(e(d),(f()+m).toString(s)):i(e(d))}},isExpired:function(a){return l(a)},get:function(b,d,e){if(!a())return null;var f=g(b);if(l(b)&&(d||k(b),!e))return null;if(!f||!c())return f;try{return JSON.parse(f)}catch(h){return f}},remove:function(b){a()&&k(b)},supported:function(){return a()},flush:function(){a()&&(j(function(a){k(a)}),k(u))},flushExpired:function(){a()&&j(function(a){m(a)})},setBucket:function(a){w=a},resetBucket:function(){w=""},enableWarnings:function(a){x=a}};return y.setExpiryUnitMs(),y});