hexo/node_modules/micro-memoize/dist/micro-memoize.min.js

2 lines
3.8 KiB
JavaScript

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self)["micro-memoize"]=t()}(this,(function(){"use strict";var e={isEqual:!0,isMatchingKey:!0,isPromise:!0,maxSize:!0,onCacheAdd:!0,onCacheChange:!0,onCacheHit:!0,transformKey:!0},t=Array.prototype.slice;function n(e){var n=e.length;return n?1===n?[e[0]]:2===n?[e[0],e[1]]:3===n?[e[0],e[1],e[2]]:t.call(e,0):[]}function i(e,t){return e===t||e!=e&&t!=t}function o(e,t){var n={};for(var i in e)n[i]=e[i];for(var i in t)n[i]=t[i];return n}var r=function(){function e(e){this.keys=[],this.values=[],this.options=e;var t="function"==typeof e.isMatchingKey;t?this.getKeyIndex=this._getKeyIndexFromMatchingKey:e.maxSize>1?this.getKeyIndex=this._getKeyIndexForMany:this.getKeyIndex=this._getKeyIndexForSingle,this.canTransformKey="function"==typeof e.transformKey,this.shouldCloneArguments=this.canTransformKey||t,this.shouldUpdateOnAdd="function"==typeof e.onCacheAdd,this.shouldUpdateOnChange="function"==typeof e.onCacheChange,this.shouldUpdateOnHit="function"==typeof e.onCacheHit}return Object.defineProperty(e.prototype,"size",{get:function(){return this.keys.length},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"snapshot",{get:function(){return{keys:n(this.keys),size:this.size,values:n(this.values)}},enumerable:!1,configurable:!0}),e.prototype._getKeyIndexFromMatchingKey=function(e){var t=this.options,n=t.isMatchingKey,i=t.maxSize,o=this.keys,r=o.length;if(!r)return-1;if(n(o[0],e))return 0;if(i>1)for(var s=1;s<r;s++)if(n(o[s],e))return s;return-1},e.prototype._getKeyIndexForMany=function(e){var t=this.options.isEqual,n=this.keys,i=n.length;if(!i)return-1;if(1===i)return this._getKeyIndexForSingle(e);var o,r,s=e.length;if(s>1){for(var a=0;a<i;a++)if((o=n[a]).length===s){for(r=0;r<s&&t(o[r],e[r]);r++);if(r===s)return a}}else for(a=0;a<i;a++)if((o=n[a]).length===s&&t(o[0],e[0]))return a;return-1},e.prototype._getKeyIndexForSingle=function(e){var t=this.keys;if(!t.length)return-1;var n=t[0],i=n.length;if(e.length!==i)return-1;var o=this.options.isEqual;if(i>1){for(var r=0;r<i;r++)if(!o(n[r],e[r]))return-1;return 0}return o(n[0],e[0])?0:-1},e.prototype.orderByLru=function(e,t,n){for(var i=this.keys,o=this.values,r=i.length,s=n;s--;)i[s+1]=i[s],o[s+1]=o[s];i[0]=e,o[0]=t;var a=this.options.maxSize;r===a&&n===r?(i.pop(),o.pop()):n>=a&&(i.length=o.length=a)},e.prototype.updateAsyncCache=function(e){var t=this,n=this.options,i=n.onCacheChange,o=n.onCacheHit,r=this.keys[0],s=this.values[0];this.values[0]=s.then((function(n){return t.shouldUpdateOnHit&&o(t,t.options,e),t.shouldUpdateOnChange&&i(t,t.options,e),n}),(function(e){var n=t.getKeyIndex(r);throw-1!==n&&(t.keys.splice(n,1),t.values.splice(n,1)),e}))},e}();return function t(s,a){if(void 0===a&&(a={}),function(e){return"function"==typeof e&&e.isMemoized}(s))return t(s.fn,o(s.options,a));if("function"!=typeof s)throw new TypeError("You must pass a function to `memoize`.");var h=a.isEqual,u=void 0===h?i:h,f=a.isMatchingKey,c=a.isPromise,y=void 0!==c&&c,d=a.maxSize,l=void 0===d?1:d,p=a.onCacheAdd,g=a.onCacheChange,v=a.onCacheHit,m=a.transformKey,C=o({isEqual:u,isMatchingKey:f,isPromise:y,maxSize:l,onCacheAdd:p,onCacheChange:g,onCacheHit:v,transformKey:m},function(t){var n={};for(var i in t)e[i]||(n[i]=t[i]);return n}(a)),K=new r(C),x=K.keys,z=K.values,I=K.canTransformKey,k=K.shouldCloneArguments,A=K.shouldUpdateOnAdd,M=K.shouldUpdateOnChange,O=K.shouldUpdateOnHit,b=function(){var e=k?n(arguments):arguments;I&&(e=m(e));var t=x.length?K.getKeyIndex(e):-1;if(-1!==t)O&&v(K,C,b),t&&(K.orderByLru(x[t],z[t],t),M&&g(K,C,b));else{var i=s.apply(this,arguments),o=k?e:n(arguments);K.orderByLru(o,i,x.length),y&&K.updateAsyncCache(b),A&&p(K,C,b),M&&g(K,C,b)}return z[0]};return b.cache=K,b.fn=s,b.isMemoized=!0,b.options=C,b}}));