micro-memoize
Advanced tools
Comparing version 4.0.4 to 4.0.5
@@ -67,2 +67,14 @@ import { Cache } from '../src/Cache'; | ||
describe('getKeyIndex', () => { | ||
it('will return -1 if no keys exist', () => { | ||
const isEqual = (o1: any, o2: any) => o1 === o2; | ||
const cache = new Cache({ isEqual }); | ||
const keyToMatch = ['key']; | ||
const result = cache.getKeyIndex(keyToMatch); | ||
expect(result).toEqual(-1); | ||
}); | ||
it('will return the index of the match found', () => { | ||
@@ -110,2 +122,14 @@ const isEqual = (o1: any, o2: any) => o1 === o2; | ||
it('will return -1 if no keys exist with larger maxSize', () => { | ||
const isEqual = (o1: any, o2: any) => o1 === o2; | ||
const cache = new Cache({ isEqual, maxSize: 2 }); | ||
const keyToMatch = ['key']; | ||
const result = cache.getKeyIndex(keyToMatch); | ||
expect(result).toEqual(-1); | ||
}); | ||
it('will return the index of the match found with larger maxSize', () => { | ||
@@ -234,2 +258,24 @@ const isEqual = (o1: any, o2: any) => o1 === o2; | ||
it('will return -1 if the isMatchingKey method is passed and there are no keys', () => { | ||
const isEqual = (o1: any, o2: any) => o1 === o2; | ||
const isMatchingKey = (o1: any, o2: any) => { | ||
const existingKey = o1[0]; | ||
const key = o2[0]; | ||
return ( | ||
existingKey.hasOwnProperty('foo') && | ||
key.hasOwnProperty('foo') && | ||
(existingKey.bar === 'bar' || key.bar === 'baz') | ||
); | ||
}; | ||
const cache = new Cache({ isEqual, isMatchingKey }); | ||
const keyToMatch = ['key']; | ||
const result = cache.getKeyIndex(keyToMatch); | ||
expect(result).toEqual(-1); | ||
}); | ||
it('will return -1 if the isMatchingKey method is passed and no match is found', () => { | ||
@@ -236,0 +282,0 @@ const isEqual = (o1: any, o2: any) => o1 === o2; |
# micro-memoize CHANGELOG | ||
## 4.0.5 | ||
- Fix failure when `getKeyIndex` is used and no keys are in the cache | ||
## 4.0.4 | ||
@@ -4,0 +8,0 @@ |
@@ -144,2 +144,6 @@ 'use strict'; | ||
var keys = this.keys; | ||
var keysLength = keys.length; | ||
if (!keysLength) { | ||
return -1; | ||
} | ||
if (isMatchingKey(keys[0], keyToMatch)) { | ||
@@ -149,3 +153,2 @@ return 0; | ||
if (maxSize > 1) { | ||
var keysLength = keys.length; | ||
for (var index = 1; index < keysLength; index++) { | ||
@@ -172,2 +175,5 @@ if (isMatchingKey(keys[index], keyToMatch)) { | ||
var keysLength = keys.length; | ||
if (!keysLength) { | ||
return -1; | ||
} | ||
var keyLength = keyToMatch.length; | ||
@@ -202,9 +208,13 @@ var existingKey; | ||
Cache.prototype._getKeyIndexForSingle = function (keyToMatch) { | ||
var existingKey = this.keys[0]; | ||
var keyLength = existingKey.length; | ||
if (keyToMatch.length !== keyLength) { | ||
var keys = this.keys; | ||
if (!keys.length) { | ||
return -1; | ||
} | ||
var existingKey = keys[0]; | ||
var length = existingKey.length; | ||
if (keyToMatch.length !== length) { | ||
return -1; | ||
} | ||
var isEqual = this.options.isEqual; | ||
for (var index = 0; index < keyLength; index++) { | ||
for (var index = 0; index < length; index++) { | ||
if (!isEqual(existingKey[index], keyToMatch[index])) { | ||
@@ -211,0 +221,0 @@ return -1; |
@@ -142,2 +142,6 @@ /** | ||
var keys = this.keys; | ||
var keysLength = keys.length; | ||
if (!keysLength) { | ||
return -1; | ||
} | ||
if (isMatchingKey(keys[0], keyToMatch)) { | ||
@@ -147,3 +151,2 @@ return 0; | ||
if (maxSize > 1) { | ||
var keysLength = keys.length; | ||
for (var index = 1; index < keysLength; index++) { | ||
@@ -170,2 +173,5 @@ if (isMatchingKey(keys[index], keyToMatch)) { | ||
var keysLength = keys.length; | ||
if (!keysLength) { | ||
return -1; | ||
} | ||
var keyLength = keyToMatch.length; | ||
@@ -200,9 +206,13 @@ var existingKey; | ||
Cache.prototype._getKeyIndexForSingle = function (keyToMatch) { | ||
var existingKey = this.keys[0]; | ||
var keyLength = existingKey.length; | ||
if (keyToMatch.length !== keyLength) { | ||
var keys = this.keys; | ||
if (!keys.length) { | ||
return -1; | ||
} | ||
var existingKey = keys[0]; | ||
var length = existingKey.length; | ||
if (keyToMatch.length !== length) { | ||
return -1; | ||
} | ||
var isEqual = this.options.isEqual; | ||
for (var index = 0; index < keyLength; index++) { | ||
for (var index = 0; index < length; index++) { | ||
if (!isEqual(existingKey[index], keyToMatch[index])) { | ||
@@ -209,0 +219,0 @@ return -1; |
@@ -148,2 +148,6 @@ (function (global, factory) { | ||
var keys = this.keys; | ||
var keysLength = keys.length; | ||
if (!keysLength) { | ||
return -1; | ||
} | ||
if (isMatchingKey(keys[0], keyToMatch)) { | ||
@@ -153,3 +157,2 @@ return 0; | ||
if (maxSize > 1) { | ||
var keysLength = keys.length; | ||
for (var index = 1; index < keysLength; index++) { | ||
@@ -176,2 +179,5 @@ if (isMatchingKey(keys[index], keyToMatch)) { | ||
var keysLength = keys.length; | ||
if (!keysLength) { | ||
return -1; | ||
} | ||
var keyLength = keyToMatch.length; | ||
@@ -206,9 +212,13 @@ var existingKey; | ||
Cache.prototype._getKeyIndexForSingle = function (keyToMatch) { | ||
var existingKey = this.keys[0]; | ||
var keyLength = existingKey.length; | ||
if (keyToMatch.length !== keyLength) { | ||
var keys = this.keys; | ||
if (!keys.length) { | ||
return -1; | ||
} | ||
var existingKey = keys[0]; | ||
var length = existingKey.length; | ||
if (keyToMatch.length !== length) { | ||
return -1; | ||
} | ||
var isEqual = this.options.isEqual; | ||
for (var index = 0; index < keyLength; index++) { | ||
for (var index = 0; index < length; index++) { | ||
if (!isEqual(existingKey[index], keyToMatch[index])) { | ||
@@ -215,0 +225,0 @@ return -1; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self)["micro-memoize"]=t()}(this,function(){"use strict";var A={isEqual:!0,isMatchingKey:!0,isPromise:!0,maxSize:!0,onCacheAdd:!0,onCacheChange:!0,onCacheHit:!0,transformKey:!0};function I(e,t){return e===t||e!=e&&t!=t}function M(e,t){var n={};for(var o in e)n[o]=e[o];for(var o in t)n[o]=t[o];return n}var O=Function.prototype.bind.call(Function.prototype.call,Array.prototype.slice),b=(Object.defineProperty(e.prototype,"size",{get:function(){return this.keys.length},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"snapshot",{get:function(){return{keys:O(this.keys,0),size:this.size,values:O(this.values,0)}},enumerable:!0,configurable:!0}),e.prototype._getKeyIndexFromMatchingKey=function(e){var t=this.options,n=t.isMatchingKey,o=t.maxSize,i=this.keys;if(n(i[0],e))return 0;if(1<o)for(var r=i.length,s=1;s<r;s++)if(n(i[s],e))return s;return-1},e.prototype._getKeyIndexForMany=function(e){for(var t,n,o=this.options.isEqual,i=this.keys,r=i.length,s=e.length,a=0;a<r;a++)if((t=i[a]).length===s){for(n=0;n<s&&o(t[n],e[n]);n++);if(n===s)return a}return-1},e.prototype._getKeyIndexForSingle=function(e){var t=this.keys[0],n=t.length;if(e.length!==n)return-1;for(var o=this.options.isEqual,i=0;i<n;i++)if(!o(t[i],e[i]))return-1;return 0},e.prototype.orderByLru=function(e,t,n){for(var o=this.keys,i=this.values,r=o.length,s=n;s--;)o[s+1]=o[s],i[s+1]=i[s];o[0]=e,i[0]=t;var a=this.options.maxSize;r===a&&n===r?(o.pop(),i.pop()):a<=n&&(o.length=i.length=a)},e.prototype.updateAsyncCache=function(t){var n=this,e=this.options,o=e.onCacheChange,i=e.onCacheHit,r=this.keys[0],s=this.values[0];this.values[0]=s.then(function(e){return n.shouldUpdateOnHit&&i(n,n.options,t),n.shouldUpdateOnChange&&o(n,n.options,t),e}).catch(function(e){var t=n.getKeyIndex(r);throw-1!==t&&(n.keys.splice(t,1),n.values.splice(t,1)),e})},e);function e(e){this.keys=[],this.values=[];var t="function"==typeof(this.options=e).isMatchingKey;t?this.getKeyIndex=this._getKeyIndexFromMatchingKey:1<e.maxSize?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 function e(i,t){if(void 0===t&&(t={}),function(e){return"function"==typeof e&&e.isMemoized}(i))return e(i.fn,M(i.options,t));if("function"!=typeof i)throw new TypeError("You must pass a function to `memoize`.");function r(){var e=K?O(arguments,0):arguments;C&&(e=d(e));var t=m.length?g.getKeyIndex(e):-1;if(-1!==t)k&&p(g,l,r),t&&(g.orderByLru(m[t],v[t],t),z&&y(g,l,r));else{var n=i.apply(this,arguments),o=K?e:O(arguments,0);g.orderByLru(o,n,m.length),h&&g.updateAsyncCache(r),x&&f(g,l,r),z&&y(g,l,r)}return v[0]}var n=t.isEqual,o=void 0===n?I:n,s=t.isMatchingKey,a=t.isPromise,h=void 0!==a&&a,u=t.maxSize,c=void 0===u?1:u,f=t.onCacheAdd,y=t.onCacheChange,p=t.onCacheHit,d=t.transformKey,l=M({isEqual:o,isMatchingKey:s,isPromise:h,maxSize:c,onCacheAdd:f,onCacheChange:y,onCacheHit:p,transformKey:d},function(e){var t={};for(var n in e)A[n]||(t[n]=e[n]);return t}(t)),g=new b(l),m=g.keys,v=g.values,C=g.canTransformKey,K=g.shouldCloneArguments,x=g.shouldUpdateOnAdd,z=g.shouldUpdateOnChange,k=g.shouldUpdateOnHit;return r.cache=g,r.fn=i,r.isMemoized=!0,r.options=l,r}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self)["micro-memoize"]=t()}(this,function(){"use strict";var A={isEqual:!0,isMatchingKey:!0,isPromise:!0,maxSize:!0,onCacheAdd:!0,onCacheChange:!0,onCacheHit:!0,transformKey:!0};function I(e,t){return e===t||e!=e&&t!=t}function M(e,t){var n={};for(var o in e)n[o]=e[o];for(var o in t)n[o]=t[o];return n}var O=Function.prototype.bind.call(Function.prototype.call,Array.prototype.slice),b=(Object.defineProperty(e.prototype,"size",{get:function(){return this.keys.length},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"snapshot",{get:function(){return{keys:O(this.keys,0),size:this.size,values:O(this.values,0)}},enumerable:!0,configurable:!0}),e.prototype._getKeyIndexFromMatchingKey=function(e){var t=this.options,n=t.isMatchingKey,o=t.maxSize,i=this.keys,r=i.length;if(!r)return-1;if(n(i[0],e))return 0;if(1<o)for(var s=1;s<r;s++)if(n(i[s],e))return s;return-1},e.prototype._getKeyIndexForMany=function(e){var t=this.options.isEqual,n=this.keys,o=n.length;if(!o)return-1;for(var i,r,s=e.length,a=0;a<o;a++)if((i=n[a]).length===s){for(r=0;r<s&&t(i[r],e[r]);r++);if(r===s)return a}return-1},e.prototype._getKeyIndexForSingle=function(e){var t=this.keys;if(!t.length)return-1;var n=t[0],o=n.length;if(e.length!==o)return-1;for(var i=this.options.isEqual,r=0;r<o;r++)if(!i(n[r],e[r]))return-1;return 0},e.prototype.orderByLru=function(e,t,n){for(var o=this.keys,i=this.values,r=o.length,s=n;s--;)o[s+1]=o[s],i[s+1]=i[s];o[0]=e,i[0]=t;var a=this.options.maxSize;r===a&&n===r?(o.pop(),i.pop()):a<=n&&(o.length=i.length=a)},e.prototype.updateAsyncCache=function(t){var n=this,e=this.options,o=e.onCacheChange,i=e.onCacheHit,r=this.keys[0],s=this.values[0];this.values[0]=s.then(function(e){return n.shouldUpdateOnHit&&i(n,n.options,t),n.shouldUpdateOnChange&&o(n,n.options,t),e}).catch(function(e){var t=n.getKeyIndex(r);throw-1!==t&&(n.keys.splice(t,1),n.values.splice(t,1)),e})},e);function e(e){this.keys=[],this.values=[];var t="function"==typeof(this.options=e).isMatchingKey;t?this.getKeyIndex=this._getKeyIndexFromMatchingKey:1<e.maxSize?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 function e(i,t){if(void 0===t&&(t={}),function(e){return"function"==typeof e&&e.isMemoized}(i))return e(i.fn,M(i.options,t));if("function"!=typeof i)throw new TypeError("You must pass a function to `memoize`.");function r(){var e=K?O(arguments,0):arguments;C&&(e=d(e));var t=v.length?g.getKeyIndex(e):-1;if(-1!==t)k&&p(g,l,r),t&&(g.orderByLru(v[t],m[t],t),z&&y(g,l,r));else{var n=i.apply(this,arguments),o=K?e:O(arguments,0);g.orderByLru(o,n,v.length),h&&g.updateAsyncCache(r),x&&c(g,l,r),z&&y(g,l,r)}return m[0]}var n=t.isEqual,o=void 0===n?I:n,s=t.isMatchingKey,a=t.isPromise,h=void 0!==a&&a,u=t.maxSize,f=void 0===u?1:u,c=t.onCacheAdd,y=t.onCacheChange,p=t.onCacheHit,d=t.transformKey,l=M({isEqual:o,isMatchingKey:s,isPromise:h,maxSize:f,onCacheAdd:c,onCacheChange:y,onCacheHit:p,transformKey:d},function(e){var t={};for(var n in e)A[n]||(t[n]=e[n]);return t}(t)),g=new b(l),v=g.keys,m=g.values,C=g.canTransformKey,K=g.shouldCloneArguments,x=g.shouldUpdateOnAdd,z=g.shouldUpdateOnChange,k=g.shouldUpdateOnHit;return r.cache=g,r.fn=i,r.isMemoized=!0,r.options=l,r}}); |
@@ -107,3 +107,3 @@ { | ||
"types": "./index.d.ts", | ||
"version": "4.0.4" | ||
"version": "4.0.5" | ||
} |
@@ -73,3 +73,8 @@ import { MicroMemoize } from './types'; | ||
const { keys } = this; | ||
const keysLength = keys.length; | ||
if (!keysLength) { | ||
return -1; | ||
} | ||
if (isMatchingKey(keys[0], keyToMatch)) { | ||
@@ -80,4 +85,2 @@ return 0; | ||
if (maxSize > 1) { | ||
const keysLength = keys.length; | ||
for (let index = 1; index < keysLength; index++) { | ||
@@ -108,2 +111,6 @@ if (isMatchingKey(keys[index], keyToMatch)) { | ||
if (!keysLength) { | ||
return -1; | ||
} | ||
const keyLength = keyToMatch.length; | ||
@@ -145,12 +152,18 @@ | ||
_getKeyIndexForSingle(keyToMatch: MicroMemoize.Key) { | ||
const existingKey = this.keys[0]; | ||
const keyLength = existingKey.length; | ||
const { keys } = this; | ||
if (keyToMatch.length !== keyLength) { | ||
if (!keys.length) { | ||
return -1; | ||
} | ||
const existingKey = keys[0]; | ||
const { length } = existingKey; | ||
if (keyToMatch.length !== length) { | ||
return -1; | ||
} | ||
const { isEqual } = this.options; | ||
for (let index = 0; index < keyLength; index++) { | ||
for (let index = 0; index < length; index++) { | ||
if (!isEqual(existingKey[index], keyToMatch[index])) { | ||
@@ -157,0 +170,0 @@ return -1; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
213609
3402