-
Notifications
You must be signed in to change notification settings - Fork 2
/
easycache.min.js
11 lines (10 loc) · 9.75 KB
/
easycache.min.js
1
2
3
4
5
6
7
8
9
10
11
/**
* JS-EasyCache v1.0.0
*
* By Hemn Chawroka
* http://iprodev.com
*
* Free to use and abuse under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
(function(a,b){if(typeof define==='function'&&define.amd){define([],b)}else if(typeof module!=="undefined"&&module.exports){module.exports=b()}else{a.EasyCache=b()}}(this,function(){var k='EasyCache-';var l='-cacheexpiration';var m=10;var n=60*1000;var o=Math.floor(8.64e15/n);var p;var q;var r='';var s=false;var t=String.fromCharCode;var u={compress:function(b){return u._compress(b,16,function(a){return t(a)})},_compress:function(a,b,c){if(a==null)return"";var i,value,context_dictionary={},context_dictionaryToCreate={},context_c="",context_wc="",context_w="",context_enlargeIn=2,context_dictSize=3,context_numBits=2,context_data=[],context_data_val=0,context_data_position=0,ii;for(ii=0;ii<a.length;ii+=1){context_c=a.charAt(ii);if(!Object.prototype.hasOwnProperty.call(context_dictionary,context_c)){context_dictionary[context_c]=context_dictSize++;context_dictionaryToCreate[context_c]=true}context_wc=context_w+context_c;if(Object.prototype.hasOwnProperty.call(context_dictionary,context_wc)){context_w=context_wc}else{if(Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)){if(context_w.charCodeAt(0)<256){for(i=0;i<context_numBits;i++){context_data_val=(context_data_val<<1);if(context_data_position==b-1){context_data_position=0;context_data.push(c(context_data_val));context_data_val=0}else{context_data_position++}}value=context_w.charCodeAt(0);for(i=0;i<8;i++){context_data_val=(context_data_val<<1)|(value&1);if(context_data_position==b-1){context_data_position=0;context_data.push(c(context_data_val));context_data_val=0}else{context_data_position++}value=value>>1}}else{value=1;for(i=0;i<context_numBits;i++){context_data_val=(context_data_val<<1)|value;if(context_data_position==b-1){context_data_position=0;context_data.push(c(context_data_val));context_data_val=0}else{context_data_position++}value=0}value=context_w.charCodeAt(0);for(i=0;i<16;i++){context_data_val=(context_data_val<<1)|(value&1);if(context_data_position==b-1){context_data_position=0;context_data.push(c(context_data_val));context_data_val=0}else{context_data_position++}value=value>>1}}context_enlargeIn--;if(context_enlargeIn==0){context_enlargeIn=Math.pow(2,context_numBits);context_numBits++}delete context_dictionaryToCreate[context_w]}else{value=context_dictionary[context_w];for(i=0;i<context_numBits;i++){context_data_val=(context_data_val<<1)|(value&1);if(context_data_position==b-1){context_data_position=0;context_data.push(c(context_data_val));context_data_val=0}else{context_data_position++}value=value>>1}}context_enlargeIn--;if(context_enlargeIn==0){context_enlargeIn=Math.pow(2,context_numBits);context_numBits++}context_dictionary[context_wc]=context_dictSize++;context_w=String(context_c)}}if(context_w!==""){if(Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)){if(context_w.charCodeAt(0)<256){for(i=0;i<context_numBits;i++){context_data_val=(context_data_val<<1);if(context_data_position==b-1){context_data_position=0;context_data.push(c(context_data_val));context_data_val=0}else{context_data_position++}}value=context_w.charCodeAt(0);for(i=0;i<8;i++){context_data_val=(context_data_val<<1)|(value&1);if(context_data_position==b-1){context_data_position=0;context_data.push(c(context_data_val));context_data_val=0}else{context_data_position++}value=value>>1}}else{value=1;for(i=0;i<context_numBits;i++){context_data_val=(context_data_val<<1)|value;if(context_data_position==b-1){context_data_position=0;context_data.push(c(context_data_val));context_data_val=0}else{context_data_position++}value=0}value=context_w.charCodeAt(0);for(i=0;i<16;i++){context_data_val=(context_data_val<<1)|(value&1);if(context_data_position==b-1){context_data_position=0;context_data.push(c(context_data_val));context_data_val=0}else{context_data_position++}value=value>>1}}context_enlargeIn--;if(context_enlargeIn==0){context_enlargeIn=Math.pow(2,context_numBits);context_numBits++}delete context_dictionaryToCreate[context_w]}else{value=context_dictionary[context_w];for(i=0;i<context_numBits;i++){context_data_val=(context_data_val<<1)|(value&1);if(context_data_position==b-1){context_data_position=0;context_data.push(c(context_data_val));context_data_val=0}else{context_data_position++}value=value>>1}}context_enlargeIn--;if(context_enlargeIn==0){context_enlargeIn=Math.pow(2,context_numBits);context_numBits++}}value=2;for(i=0;i<context_numBits;i++){context_data_val=(context_data_val<<1)|(value&1);if(context_data_position==b-1){context_data_position=0;context_data.push(c(context_data_val));context_data_val=0}else{context_data_position++}value=value>>1}while(true){context_data_val=(context_data_val<<1);if(context_data_position==b-1){context_data.push(c(context_data_val));break}else context_data_position++}return context_data.join('')},decompress:function(b){if(b==null)return"";if(b=="")return null;return u._decompress(b.length,32768,function(a){return b.charCodeAt(a)})},_decompress:function(a,b,d){var e=[],next,enlargeIn=4,dictSize=4,numBits=3,entry="",result=[],i,w,bits,resb,maxpower,power,c,data={val:d(0),position:b,index:1};for(i=0;i<3;i+=1){e[i]=i}bits=0;maxpower=Math.pow(2,2);power=1;while(power!=maxpower){resb=data.val&data.position;data.position>>=1;if(data.position==0){data.position=b;data.val=d(data.index++)}bits|=(resb>0?1:0)*power;power<<=1}switch(next=bits){case 0:bits=0;maxpower=Math.pow(2,8);power=1;while(power!=maxpower){resb=data.val&data.position;data.position>>=1;if(data.position==0){data.position=b;data.val=d(data.index++)}bits|=(resb>0?1:0)*power;power<<=1}c=t(bits);break;case 1:bits=0;maxpower=Math.pow(2,16);power=1;while(power!=maxpower){resb=data.val&data.position;data.position>>=1;if(data.position==0){data.position=b;data.val=d(data.index++)}bits|=(resb>0?1:0)*power;power<<=1}c=t(bits);break;case 2:return""}e[3]=c;w=c;result.push(c);while(true){if(data.index>a){return""}bits=0;maxpower=Math.pow(2,numBits);power=1;while(power!=maxpower){resb=data.val&data.position;data.position>>=1;if(data.position==0){data.position=b;data.val=d(data.index++)}bits|=(resb>0?1:0)*power;power<<=1}switch(c=bits){case 0:bits=0;maxpower=Math.pow(2,8);power=1;while(power!=maxpower){resb=data.val&data.position;data.position>>=1;if(data.position==0){data.position=b;data.val=d(data.index++)}bits|=(resb>0?1:0)*power;power<<=1}e[dictSize++]=t(bits);c=dictSize-1;enlargeIn--;break;case 1:bits=0;maxpower=Math.pow(2,16);power=1;while(power!=maxpower){resb=data.val&data.position;data.position>>=1;if(data.position==0){data.position=b;data.val=d(data.index++)}bits|=(resb>0?1:0)*power;power<<=1}e[dictSize++]=t(bits);c=dictSize-1;enlargeIn--;break;case 2:return result.join('')}if(enlargeIn==0){enlargeIn=Math.pow(2,numBits);numBits++}if(e[c]){entry=e[c]}else{if(c===dictSize){entry=w+w.charAt(0)}else{return null}}result.push(entry);e[dictSize++]=w+entry.charAt(0);enlargeIn--;w=entry;if(enlargeIn==0){enlargeIn=Math.pow(2,numBits);numBits++}}}};function supportsStorage(){var a='__easycachetest__';var b=a;if(p!==undefined){return p}try{setItem(a,b);removeItem(a);p=true}catch(e){if(isOutOfSpace(e)){p=true}else{p=false}}return p}function isOutOfSpace(e){if(e&&e.name==='QUOTA_EXCEEDED_ERR'||e.name==='NS_ERROR_DOM_QUOTA_REACHED'||e.name==='QuotaExceededError'){return true}return false}function supportsJSON(){if(q===undefined){q=(window.JSON!=null)}return q}function escapeRegExpSpecialCharacters(a){return a.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,'\\$&')}function expirationKey(a){return a+l}function currentTime(){return Math.floor((new Date().getTime())/n)}function getItem(a){return localStorage.getItem(k+r+a)}function setItem(a,b){localStorage.removeItem(k+r+a);localStorage.setItem(k+r+a,b)}function removeItem(a){localStorage.removeItem(k+r+a)}function eachKey(a){var b=new RegExp('^'+k+escapeRegExpSpecialCharacters(r)+'(.*)');for(var i=localStorage.length-1;i>=0;--i){var c=localStorage.key(i);c=c&&c.match(b);c=c&&c[1];if(c&&c.indexOf(l)<0){a(c,expirationKey(c))}}}function flushItem(a){var b=expirationKey(a);removeItem(a);removeItem(b)}function flushExpiredItem(a){var b=expirationKey(a);var c=getItem(b);if(c){var d=parseInt(c,m);if(currentTime()>=d){removeItem(a);removeItem(b);return true}}}function warn(a,b){if(!s)return;if(!('console'in window)||typeof window.console.warn!=='function')return;window.console.warn("JS-EasyCache - "+a);if(b)window.console.warn("JS-EasyCache - The error was: "+b.message)}var v={set:function(d,f,g){if(!supportsStorage())return;if(typeof f!=='string'){if(!supportsJSON())return;try{f=JSON.stringify(f)}catch(e){return}}if(f){f=u.compress(f)}try{setItem(d,f)}catch(e){if(isOutOfSpace(e)){var h=[];var i;eachKey(function(a,b){var c=getItem(b);if(c){c=parseInt(c,m)}else{c=o}h.push({key:a,size:(getItem(a)||'').length,expiration:c})});h.sort(function(a,b){return(b.expiration-a.expiration)});var j=(f||'').length;while(h.length&&j>0){i=h.pop();warn("Cache is full, removing item with key '"+d+"'");flushItem(i.key);j-=i.size}try{setItem(d,f)}catch(e){warn("Could not add item with key '"+d+"', perhaps it's too big?",e);return}}else{warn("Could not add item with key '"+d+"'",e);return}}if(g){setItem(expirationKey(d),(currentTime()+g).toString(m))}else{removeItem(expirationKey(d))}},get:function(a){if(!supportsStorage())return null;if(flushExpiredItem(a)){return null}var b=getItem(a);if(b){b=u.decompress(b)}if(!b||!supportsJSON()){return b}try{return JSON.parse(b)}catch(e){return b}},remove:function(a){if(!supportsStorage())return;flushItem(a)},supported:function(){return supportsStorage()},flush:function(){if(!supportsStorage())return;eachKey(function(a){flushItem(a)})},flushExpired:function(){if(!supportsStorage())return;eachKey(function(a){flushExpiredItem(a)})},setBucket:function(a){r=a},resetBucket:function(){r=''},enableWarnings:function(a){s=a}};return v}));