!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t("undefined"==typeof angular?require("angular"):angular):"function"==typeof define&&define.amd?define("angular-cache",["angular"],t):n.angularCacheModuleName=t(n.angular)}(this,function(n){"use strict";function e(n,t){if(n||(n=function(n){return n}),t||(t=function(n,t){return n===t}),"function"!=typeof n)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "weightFunc" must be a function!');if("function"!=typeof t)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "compareFunc" must be a function!');this.weightFunc=n;this.compareFunc=t;this.heap=[]}function o(n){var i=[],r=void 0;if(!t.isObject(n))return i;for(r in n)n.hasOwnProperty(r)&&i.push(r);return i}function c(n){return n&&"function"==typeof n.then}function l(n){return t.isNumber(n)?n.toString():n}function a(n){var r={},i=void 0;if(!t.isObject(n))return r;for(i in n)n.hasOwnProperty(i)&&(r[i]=i);return r}function v(n,r){if(n in i)throw new Error(n+" already exists!");if(!t.isString(n))throw new Error("cacheId must be a string!");var h={},p={},f=null,y=new e(function(n){return n.expires},t.equals),v=new e(function(n){return n.accessed},t.equals),w=i[n]={$$id:n,destroy:function(){clearInterval(this.$$cacheFlushIntervalId);clearInterval(this.$$recycleFreqId);this.removeAll();f&&(f().removeItem(this.$$prefix+".keys"),f().removeItem(this.$$prefix));f=null;h=null;v=null;y=null;this.$$prefix=null;delete i[this.$$id]},disable:function(){this.$$disabled=!0},enable:function(){delete this.$$disabled},get:function(n,i){var a=this,e,r,o,c,u;if(Array.isArray(n)){if(e=function(){var r=n,t=[];return r.forEach(function(n){var r=a.get(n,i);null!==r&&void 0!==r&&t.push(r)}),{v:t}}(),"object"===("undefined"==typeof e?"undefined":s["typeof"](e)))return e.v}else if(n=l(n),this.$$disabled)return;if(i=i||{},!t.isString(n))throw new Error("key must be a string!");if(i&&!t.isObject(i))throw new Error("options must be an object!");if(i.onExpire&&!t.isFunction(i.onExpire))throw new Error("options.onExpire must be a function!");if(r=void 0,f){if(p[n])return p[n];if(o=f().getItem(this.$$prefix+".data."+n),!o)return;r=t.fromJson(o)}else if(t.isObject(h)){if(!(n in h))return;r=h[n]}return c=r.value,u=(new Date).getTime(),f?(v.remove({key:n,accessed:r.accessed}),r.accessed=u,v.push({key:n,accessed:u})):(v.remove(r),r.accessed=u,v.push(r)),"passive"===this.$$deleteOnExpire&&"expires"in r&&r.expires<u?(this.remove(n),this.$$onExpire?this.$$onExpire(n,r.value,i.onExpire):i.onExpire&&i.onExpire.call(this,n,r.value),c=void 0):f&&f().setItem(this.$$prefix+".data."+n,JSON.stringify(r)),c},info:function(n){var i,r;return n?(i=void 0,f)?(r=f().getItem(this.$$prefix+".data."+n),r?(i=t.fromJson(r),{created:i.created,accessed:i.accessed,expires:i.expires,isExpired:(new Date).getTime()-i.created>(i.maxAge||this.$$maxAge)}):void 0):t.isObject(h)&&n in h?(i=h[n],{created:i.created,accessed:i.accessed,expires:i.expires,isExpired:(new Date).getTime()-i.created>(i.maxAge||this.$$maxAge)}):void 0:{id:this.$$id,capacity:this.$$capacity,maxAge:this.$$maxAge,deleteOnExpire:this.$$deleteOnExpire,onExpire:this.$$onExpire,cacheFlushInterval:this.$$cacheFlushInterval,recycleFreq:this.$$recycleFreq,storageMode:this.$$storageMode,storageImpl:f?f():void 0,disabled:!!this.$$disabled,size:v&&v.size()||0}},keys:function(){if(f){var n=f().getItem(this.$$prefix+".keys");return n?t.fromJson(n):[]}return o(h)},keySet:function(){var r,u,i,n;if(f){if(r=f().getItem(this.$$prefix+".keys"),u={},r)for(i=t.fromJson(r),n=0;n<i.length;n++)u[i[n]]=i[n];return u}return a(h)},put:function(n,i,r){var w=this,s,u,a,o;r||(r={});var d="storeOnResolve"in r?!!r.storeOnResolve:this.$$storeOnResolve,g="storeOnReject"in r?!!r.storeOnReject:this.$$storeOnReject,b=function(i,r){return function(u){if(i&&(delete p[n],t.isObject(u)&&"status"in u&&"data"in u?(u=[u.status,u.data,u.headers(),u.statusText],w.put(n,u)):w.put(n,u)),r){if(t.Promise)return t.Promise.reject(u);throw u;}return u}};if(!this.$$disabled&&t.isObject(h)&&null!==i&&void 0!==i){if(n=l(n),!t.isString(n))throw new Error("key must be a string!");if(s=(new Date).getTime(),u={key:n,value:c(i)?i.then(b(d,!1),b(g,!0)):i,created:void 0===r.created?s:r.created,accessed:void 0===r.accessed?s:r.accessed},r.maxAge&&(u.maxAge=r.maxAge),u.expires=void 0===r.expires?u.created+(u.maxAge||this.$$maxAge):r.expires,f){if(c(u.value))return p[n]=u.value,p[n];var k=f().getItem(this.$$prefix+".keys"),e=k?t.fromJson(k):[],nt=f().getItem(this.$$prefix+".data."+n);for(nt&&this.remove(n),y.push({key:n,expires:u.expires}),v.push({key:n,accessed:u.accessed}),f().setItem(this.$$prefix+".data."+n,JSON.stringify(u)),a=!1,o=0;o<e.length;o++)if(e[o]===n){a=!0;break}a||e.push(n);f().setItem(this.$$prefix+".keys",JSON.stringify(e))}else h[n]&&this.remove(n),y.push(u),v.push(u),h[n]=u,delete p[n];return v.size()>this.$$capacity&&this.remove(v.peek().key),i}},remove:function(n){var r,i,s;if(n+="",delete p[n],f){if(r=f().getItem(this.$$prefix+".data."+n),r){i=t.fromJson(r);v.remove({key:n,accessed:i.accessed});y.remove({key:n,expires:i.expires});f().removeItem(this.$$prefix+".data."+n);var e=f().getItem(this.$$prefix+".keys"),u=e?t.fromJson(e):[],o=u.indexOf(n);return o>=0&&u.splice(o,1),f().setItem(this.$$prefix+".keys",JSON.stringify(u)),i.value}}else if(t.isObject(h))return s=h[n]?h[n].value:void 0,v.remove(h[n]),y.remove(h[n]),h[n]=null,delete h[n],s},removeAll:function(){var i,r,n,u;if(f){if(v.removeAll(),y.removeAll(),i=f().getItem(this.$$prefix+".keys"),i)for(r=t.fromJson(i),n=0;n<r.length;n++)this.remove(r[n]);f().setItem(this.$$prefix+".keys",JSON.stringify([]))}else if(t.isObject(h)){v.removeAll();y.removeAll();for(u in h)h[u]=null;h={}}else v.removeAll(),y.removeAll(),h={};p={}},removeExpired:function(){for(var u,e=(new Date).getTime(),i={},n=void 0,r=void 0;(r=y.peek())&&r.expires<=e;)i[r.key]=r.value?r.value:null,y.pop();if(f)for(n in i)u=f().getItem(this.$$prefix+".data."+n),u&&(i[n]=t.fromJson(u).value,this.remove(n));else for(n in i)this.remove(n);if(this.$$onExpire)for(n in i)this.$$onExpire(n,i[n]);return i},setCacheFlushInterval:function(n){var i=this;if(null===n)delete i.$$cacheFlushInterval;else{if(!t.isNumber(n))throw new Error("cacheFlushInterval must be a number!");if(0>n)throw new Error("cacheFlushInterval must be greater than zero!");n!==i.$$cacheFlushInterval&&(i.$$cacheFlushInterval=n,clearInterval(i.$$cacheFlushIntervalId),i.$$cacheFlushIntervalId=setInterval(function(){i.removeAll()},i.$$cacheFlushInterval))}},setCapacity:function(n){if(null===n)delete this.$$capacity;else{if(!t.isNumber(n))throw new Error("capacity must be a number!");if(0>n)throw new Error("capacity must be greater than zero!");this.$$capacity=n}for(var i={};v.size()>this.$$capacity;)i[v.peek().key]=this.remove(v.peek().key);return i},setDeleteOnExpire:function(n,i){if(null===n)delete this.$$deleteOnExpire;else{if(!t.isString(n))throw new Error("deleteOnExpire must be a string!");if("none"!==n&&"passive"!==n&&"aggressive"!==n)throw new Error('deleteOnExpire must be "none", "passive" or "aggressive"!');this.$$deleteOnExpire=n}i!==!1&&this.setRecycleFreq(this.$$recycleFreq)},setMaxAge:function(n){var s,c,e;if(null===n)this.$$maxAge=Number.MAX_VALUE;else{if(!t.isNumber(n))throw new Error("maxAge must be a number!");if(0>n)throw new Error("maxAge must be greater than zero!");this.$$maxAge=n}var i=void 0,u=void 0,r=void 0;if(y.removeAll(),f)for(s=f().getItem(this.$$prefix+".keys"),u=s?t.fromJson(s):[],i=0;i<u.length;i++)r=u[i],c=f().getItem(this.$$prefix+".data."+r),c&&(e=t.fromJson(c),e.expires=this.$$maxAge===Number.MAX_VALUE?Number.MAX_VALUE:e.created+(e.maxAge||this.$$maxAge),y.push({key:r,expires:e.expires}));else for(u=o(h),i=0;i<u.length;i++)r=u[i],h[r].expires=this.$$maxAge===Number.MAX_VALUE?Number.MAX_VALUE:h[r].created+(h[r].maxAge||this.$$maxAge),y.push(h[r]);return"aggressive"===this.$$deleteOnExpire?this.removeExpired():{}},setOnExpire:function(n){if(null===n)delete this.$$onExpire;else{if(!t.isFunction(n))throw new Error("onExpire must be a function!");this.$$onExpire=n}},setOptions:function(n,i){if(n=n||{},i=!!i,!t.isObject(n))throw new Error("cacheOptions must be an object!");"storagePrefix"in n?this.$$storagePrefix=n.storagePrefix:i&&(this.$$storagePrefix=u.storagePrefix);this.$$prefix=this.$$storagePrefix+this.$$id;"disabled"in n?this.$$disabled=!!n.disabled:i&&(this.$$disabled=u.disabled);"deleteOnExpire"in n?this.setDeleteOnExpire(n.deleteOnExpire,!1):i&&this.setDeleteOnExpire(u.deleteOnExpire,!1);"recycleFreq"in n?this.setRecycleFreq(n.recycleFreq):i&&this.setRecycleFreq(u.recycleFreq);"maxAge"in n?this.setMaxAge(n.maxAge):i&&this.setMaxAge(u.maxAge);"storeOnResolve"in n?this.$$storeOnResolve=!!n.storeOnResolve:i&&(this.$$storeOnResolve=u.storeOnResolve);"storeOnReject"in n?this.$$storeOnReject=!!n.storeOnReject:i&&(this.$$storeOnReject=u.storeOnReject);"capacity"in n?this.setCapacity(n.capacity):i&&this.setCapacity(u.capacity);"cacheFlushInterval"in n?this.setCacheFlushInterval(n.cacheFlushInterval):i&&this.setCacheFlushInterval(u.cacheFlushInterval);"onExpire"in n?this.setOnExpire(n.onExpire):i&&this.setOnExpire(u.onExpire);"storageMode"in n||"storageImpl"in n?this.setStorageMode(n.storageMode||u.storageMode,n.storageImpl||u.storageImpl):i&&this.setStorageMode(u.storageMode,u.storageImpl)},setRecycleFreq:function(n){if(null===n)delete this.$$recycleFreq;else{if(!t.isNumber(n))throw new Error("recycleFreq must be a number!");if(0>n)throw new Error("recycleFreq must be greater than zero!");this.$$recycleFreq=n}clearInterval(this.$$recycleFreqId);"aggressive"===this.$$deleteOnExpire?!function(n){n.$$recycleFreqId=setInterval(function(){n.removeExpired()},n.$$recycleFreq)}(this):delete this.$$recycleFreqId},setStorageMode:function(n,i){function o(n,i){var o=this.keys(),h=o.length,e;if(h){for(var r=void 0,c=t.isObject(i),f=0;h>f;f++)(r=o[f],n)?(e=n().getItem(this.$$prefix+".data."+r),e&&(u[r]=t.fromJson(e))):c&&(u[r]=i[r]),this.remove(r);s=!0}}var r,e;if(!t.isString(n))throw new Error("storageMode must be a string!");if("memory"!==n&&"localStorage"!==n&&"sessionStorage"!==n)throw new Error('storageMode must be "memory", "localStorage" or "sessionStorage"!');var c=f,l=h,s=!1,u={};if(this.$$initializing||o.call(this,c,l),this.$$storageMode=n,i){if(!t.isObject(i))throw new Error("storageImpl must be an object!");if(!("setItem"in i&&"function"==typeof i.setItem))throw new Error('storageImpl must implement "setItem(key, value)"!');if(!("getItem"in i&&"function"==typeof i.getItem))throw new Error('storageImpl must implement "getItem(key)"!');if(!("removeItem"in i)||"function"!=typeof i.removeItem)throw new Error('storageImpl must implement "removeItem(key)"!');f=function(){return i}}else if("localStorage"===this.$$storageMode)try{localStorage.setItem("cachefactory","cachefactory");localStorage.removeItem("cachefactory");f=function(){return localStorage}}catch(a){f=null;this.$$storageMode="memory"}else if("sessionStorage"===this.$$storageMode)try{sessionStorage.setItem("cachefactory","cachefactory");sessionStorage.removeItem("cachefactory");f=function(){return sessionStorage}}catch(a){f=null;this.$$storageMode="memory"}else f=null,this.$$storageMode="memory";if(this.$$initializing&&o.call(this,f,h),s){r=void 0;for(e in u)r=u[e],this.put(e,r.value,{created:r.created,accessed:r.accessed,expires:r.expires})}},touch:function(n,t){var f=this,r,u,i;if(n)r=this.get(n,{onExpire:function(n,t){return f.put(n,t)}}),r&&this.put(n,r,t);else for(u=this.keys(),i=0;i<u.length;i++)this.touch(u[i],t)},values:function(){for(var t=this.keys(),i=[],n=0;n<t.length;n++)i.push(this.get(t[n]));return i}};return w.$$initializing=!0,w.setOptions(r,!0),w.$$initializing=!1,w}function r(n,t){return v(n,t)}function w(){this.$get=function(){return r.BinaryHeap}}function b(){this.defaults=r.defaults;this.defaults.storagePrefix="angular-cache.caches.";this.$get=["$q",function(n){return r.utils.Promise=n,r}]}var s,h;n="default"in n?n["default"]:n;s={};s["typeof"]="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol?"symbol":typeof n};var y=function(n,t,i){for(var r,f,u=n[i],e=t(u);i>0;){if(r=Math.floor((i+1)/2)-1,f=n[r],e>=t(f))break;n[r]=u;n[i]=f;i=r}},p=function(n,t,i){for(var h,c,l,a,e=n.length,o=n[i],s=t(o);;){var u=2*(i+1),f=u-1,r=null;if(e>f&&(h=n[f],c=t(h),s>c&&(r=f)),e>u&&(l=n[u],a=t(l),a<(null===r?s:t(n[f]))&&(r=u)),null===r)break;n[i]=n[r];n[r]=o;i=r}},f=e.prototype;f.push=function(n){this.heap.push(n);y(this.heap,this.weightFunc,this.heap.length-1)};f.peek=function(){return this.heap[0]};f.pop=function(){var n=this.heap[0],t=this.heap.pop();return this.heap.length>0&&(this.heap[0]=t,p(this.heap,this.weightFunc,0)),n};f.remove=function(n){for(var r,u,i=this.heap.length,t=0;i>t;t++)if(this.compareFunc(this.heap[t],n))return r=this.heap[t],u=this.heap.pop(),t!==i-1&&(this.heap[t]=u,y(this.heap,this.weightFunc,t),p(this.heap,this.weightFunc,t)),r;return null};f.removeAll=function(){this.heap=[]};f.size=function(){return this.heap.length};h=null;try{h=window.Promise}catch(k){}var t={isNumber:function(n){return"number"==typeof n},isString:function(n){return"string"==typeof n},isObject:function(n){return null!==n&&"object"===("undefined"==typeof n?"undefined":s["typeof"](n))},isFunction:function(n){return"function"==typeof n},fromJson:function(n){return JSON.parse(n)},equals:function(n,t){return n===t},Promise:h},u={capacity:Number.MAX_VALUE,maxAge:Number.MAX_VALUE,deleteOnExpire:"none",onExpire:null,cacheFlushInterval:null,recycleFreq:1e3,storageMode:"memory",storageImpl:null,disabled:!1,storagePrefix:"cachefactory.caches.",storeOnResolve:!1,storeOnReject:!1},i={};return r.createCache=v,r.defaults=u,r.info=function(){var r=o(i),f={size:r.length,caches:{}},n,t,e;for(n in u)u.hasOwnProperty(n)&&(f[n]=u[n]);for(t=0;t<r.length;t++)e=r[t],f.caches[e]=i[e].info();return f},r.get=function(n){return i[n]},r.keySet=function(){return a(i)},r.keys=function(){return o(i)},r.destroy=function(n){i[n]&&(i[n].destroy(),delete i[n])},r.destroyAll=function(){for(var n in i)i[n].destroy();i={}},r.clearAll=function(){for(var n in i)i[n].removeAll()},r.removeExpiredFromAll=function(){var n={};for(var t in i)n[t]=i[t].removeExpired();return n},r.enableAll=function(){for(var n in i)i[n].$$disabled=!1},r.disableAll=function(){for(var n in i)i[n].$$disabled=!0},r.touchAll=function(){for(var n in i)i[n].touch()},r.utils=t,r.BinaryHeap=e,r.utils.equals=n.equals,r.utils.isObject=n.isObject,r.utils.fromJson=n.fromJson,n.module("angular-cache",[]).provider("BinaryHeap",w).provider("CacheFactory",b),"angular-cache"})