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