micro-memoize
Advanced tools
Comparing version
# micro-memoize CHANGELOG | ||
## 1.6.0 | ||
* Add [`onCacheAdd`](README.md#oncacheadd) option | ||
* Pass through unused properties in `options` for higher-order memoization library usage | ||
## 1.5.0 | ||
@@ -4,0 +9,0 @@ |
@@ -90,4 +90,4 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__utils__ = __webpack_require__(/*! ./utils */ "./src/utils.js"); | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
// utils | ||
@@ -126,16 +126,20 @@ | ||
maxSize = _ref$maxSize === undefined ? 1 : _ref$maxSize, | ||
_ref$onCacheAdd = _ref.onCacheAdd, | ||
onCacheAdd = _ref$onCacheAdd === undefined ? __WEBPACK_IMPORTED_MODULE_0__utils__["d" /* onCacheOperation */] : _ref$onCacheAdd, | ||
_ref$onCacheChange = _ref.onCacheChange, | ||
onCacheChange = _ref$onCacheChange === undefined ? __WEBPACK_IMPORTED_MODULE_0__utils__["d" /* onCacheChangeOrHitNoOp */] : _ref$onCacheChange, | ||
onCacheChange = _ref$onCacheChange === undefined ? __WEBPACK_IMPORTED_MODULE_0__utils__["d" /* onCacheOperation */] : _ref$onCacheChange, | ||
_ref$onCacheHit = _ref.onCacheHit, | ||
onCacheHit = _ref$onCacheHit === undefined ? __WEBPACK_IMPORTED_MODULE_0__utils__["d" /* onCacheChangeOrHitNoOp */] : _ref$onCacheHit, | ||
transformKey = _ref.transformKey; | ||
onCacheHit = _ref$onCacheHit === undefined ? __WEBPACK_IMPORTED_MODULE_0__utils__["d" /* onCacheOperation */] : _ref$onCacheHit, | ||
transformKey = _ref.transformKey, | ||
extraOptions = _objectWithoutProperties(_ref, ['isEqual', 'isPromise', 'maxSize', 'onCacheAdd', 'onCacheChange', 'onCacheHit', 'transformKey']); | ||
var normalizedOptions = { | ||
var normalizedOptions = Object.assign({}, extraOptions, { | ||
isEqual: isEqual, | ||
isPromise: isPromise, | ||
maxSize: maxSize, | ||
onCacheAdd: onCacheAdd, | ||
onCacheChange: onCacheChange, | ||
onCacheHit: onCacheHit, | ||
transformKey: transformKey | ||
}; | ||
}); | ||
@@ -163,2 +167,4 @@ var getKeyIndex = Object(__WEBPACK_IMPORTED_MODULE_0__utils__["a" /* createGetKeyIndex */])(isEqual); | ||
if (~keyIndex) { | ||
onCacheHit(cache, normalizedOptions); | ||
if (keyIndex) { | ||
@@ -170,4 +176,2 @@ Object(__WEBPACK_IMPORTED_MODULE_0__utils__["e" /* orderByLru */])(cache.keys, keyIndex); | ||
} | ||
onCacheHit(cache, normalizedOptions); | ||
} else { | ||
@@ -186,2 +190,3 @@ if (cache.keys.length >= maxSize) { | ||
onCacheAdd(cache, normalizedOptions); | ||
onCacheChange(cache, normalizedOptions); | ||
@@ -232,4 +237,4 @@ } | ||
\**********************/ | ||
/*! exports provided: createAreKeysEqual, createGetKeyIndex, createGetTransformedKey, isSameValueZero, onCacheChangeOrHitNoOp, orderByLru, setPromiseCatch */ | ||
/*! exports used: createGetKeyIndex, createGetTransformedKey, isSameValueZero, onCacheChangeOrHitNoOp, orderByLru, setPromiseCatch */ | ||
/*! exports provided: createAreKeysEqual, createGetKeyIndex, createGetTransformedKey, isSameValueZero, onCacheOperation, orderByLru, setPromiseCatch */ | ||
/*! exports used: createGetKeyIndex, createGetTransformedKey, isSameValueZero, onCacheOperation, orderByLru, setPromiseCatch */ | ||
/***/ (function(module, __webpack_exports__, __webpack_require__) { | ||
@@ -242,3 +247,3 @@ | ||
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "c", function() { return isSameValueZero; }); | ||
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "d", function() { return onCacheChangeOrHitNoOp; }); | ||
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "d", function() { return onCacheOperation; }); | ||
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "e", function() { return orderByLru; }); | ||
@@ -334,3 +339,3 @@ /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "f", function() { return setPromiseCatch; }); | ||
/* eslint-disable no-unused-vars */ | ||
var onCacheChangeOrHitNoOp = function onCacheChangeOrHitNoOp(cacheIgnored, optionsIgnored) {}; | ||
var onCacheOperation = function onCacheOperation(cacheIgnored, optionsIgnored) {}; | ||
/* eslint-enable */ | ||
@@ -337,0 +342,0 @@ |
@@ -1,1 +0,1 @@ | ||
!(function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define("memoize",[],n):"object"==typeof exports?exports.memoize=n():e.memoize=n()})("undefined"!=typeof self?self:this,(function(){return (function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var t={};return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n(n.s=0)})({"./src/index.js":function(e,n,t){"use strict";function r(e,n){function t(){var n=C?C(l.call(arguments)):arguments,t=z(P.keys,n);return~t?(t&&(f(P.keys,t),f(P.values,t),g(P,j)),b(P,j)):(P.keys.length>=h&&(P.keys.pop(),P.values.pop()),P.keys.unshift(C?n:l.call(n)),P.values.unshift(e.apply(this,arguments)),d&&a(P,P.keys[0],z),g(P,j)),P.values[0]}if(e.isMemoized)return e;var r=n||{},o=r.isEqual,p=void 0===o?c:o,v=r.isPromise,d=void 0!==v&&v,y=r.maxSize,h=void 0===y?1:y,m=r.onCacheChange,g=void 0===m?s:m,x=r.onCacheHit,b=void 0===x?s:x,k=r.transformKey,j={isEqual:p,isPromise:d,maxSize:h,onCacheChange:g,onCacheHit:b,transformKey:k},z=u(p),C=k?i(k):null,P={keys:[],values:[]};return Object.defineProperties(t,{cache:{configurable:!0,get:function(){return P}},cacheSnapshot:{configurable:!0,get:function(){return{keys:[].concat(P.keys),values:[].concat(P.values)}}},isMemoized:{configurable:!0,get:function(){return!0}},options:{configurable:!0,get:function(){return j}}}),t}Object.defineProperty(n,"__esModule",{value:!0});var o=function(e){return function(n,t){if(n.length!==t.length)return!1;for(var r=0;r<n.length;r++)if(!e(n[r],t[r]))return!1;return!0}},u=function(e){var n=o(e);return function(e,t){for(var r=0;r<e.length;r++)if(n(e[r],t))return r;return-1}},i=function(e){return function(n){var t=e(n);return Array.isArray(t)?t:[t]}},c=function(e,n){return e===n||e!==e&&n!==n},s=function(e,n){},f=function(e,n){if(n){for(var t=e[n],r=n;r--;)e[r+1]=e[r];e[0]=t}},a=function(e,n,t){e.values[0]=e.values[0].catch((function(r){var o=t(e.keys,n);throw~o&&(e.keys.splice(o,1),e.values.splice(o,1)),r}))};n.default=r;var l=[].slice},0:function(e,n,t){e.exports=t("./src/index.js")}})})); | ||
!(function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define("memoize",[],n):"object"==typeof exports?exports.memoize=n():e.memoize=n()})("undefined"!=typeof self?self:this,(function(){return (function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var t={};return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n(n.s=0)})({"./src/index.js":function(e,n,t){"use strict";function r(e,n){var t={};for(var r in e)n.indexOf(r)>=0||Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}function o(e,n){function t(){var n=M?M(p.call(arguments)):arguments,t=A(S.keys,n);return~t?(k(S,P),t&&(f(S.keys,t),f(S.values,t),C(S,P))):(S.keys.length>=m&&(S.keys.pop(),S.values.pop()),S.keys.unshift(M?n:p.call(n)),S.values.unshift(e.apply(this,arguments)),h&&l(S,S.keys[0],A),x(S,P),C(S,P)),S.values[0]}if(e.isMemoized)return e;var o=n||{},i=o.isEqual,d=void 0===i?a:i,v=o.isPromise,h=void 0!==v&&v,y=o.maxSize,m=void 0===y?1:y,g=o.onCacheAdd,x=void 0===g?s:g,b=o.onCacheChange,C=void 0===b?s:b,j=o.onCacheHit,k=void 0===j?s:j,O=o.transformKey,z=r(o,["isEqual","isPromise","maxSize","onCacheAdd","onCacheChange","onCacheHit","transformKey"]),P=Object.assign({},z,{isEqual:d,isPromise:h,maxSize:m,onCacheAdd:x,onCacheChange:C,onCacheHit:k,transformKey:O}),A=u(d),M=O?c(O):null,S={keys:[],values:[]};return Object.defineProperties(t,{cache:{configurable:!0,get:function(){return S}},cacheSnapshot:{configurable:!0,get:function(){return{keys:[].concat(S.keys),values:[].concat(S.values)}}},isMemoized:{configurable:!0,get:function(){return!0}},options:{configurable:!0,get:function(){return P}}}),t}Object.defineProperty(n,"__esModule",{value:!0});var i=function(e){return function(n,t){if(n.length!==t.length)return!1;for(var r=0;r<n.length;r++)if(!e(n[r],t[r]))return!1;return!0}},u=function(e){var n=i(e);return function(e,t){for(var r=0;r<e.length;r++)if(n(e[r],t))return r;return-1}},c=function(e){return function(n){var t=e(n);return Array.isArray(t)?t:[t]}},a=function(e,n){return e===n||e!==e&&n!==n},s=function(e,n){},f=function(e,n){if(n){for(var t=e[n],r=n;r--;)e[r+1]=e[r];e[0]=t}},l=function(e,n,t){e.values[0]=e.values[0].catch((function(r){var o=t(e.keys,n);throw~o&&(e.keys.splice(o,1),e.values.splice(o,1)),r}))};n.default=o;var p=[].slice},0:function(e,n,t){e.exports=t("./src/index.js")}})})); |
@@ -0,5 +1,5 @@ | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
// utils | ||
import { createGetKeyIndex, createGetTransformedKey, isSameValueZero, onCacheChangeOrHitNoOp, orderByLru, setPromiseCatch } from './utils'; | ||
import { createGetKeyIndex, createGetTransformedKey, isSameValueZero, onCacheOperation, orderByLru, setPromiseCatch } from './utils'; | ||
@@ -36,16 +36,20 @@ // types | ||
maxSize = _ref$maxSize === undefined ? 1 : _ref$maxSize, | ||
_ref$onCacheAdd = _ref.onCacheAdd, | ||
onCacheAdd = _ref$onCacheAdd === undefined ? onCacheOperation : _ref$onCacheAdd, | ||
_ref$onCacheChange = _ref.onCacheChange, | ||
onCacheChange = _ref$onCacheChange === undefined ? onCacheChangeOrHitNoOp : _ref$onCacheChange, | ||
onCacheChange = _ref$onCacheChange === undefined ? onCacheOperation : _ref$onCacheChange, | ||
_ref$onCacheHit = _ref.onCacheHit, | ||
onCacheHit = _ref$onCacheHit === undefined ? onCacheChangeOrHitNoOp : _ref$onCacheHit, | ||
transformKey = _ref.transformKey; | ||
onCacheHit = _ref$onCacheHit === undefined ? onCacheOperation : _ref$onCacheHit, | ||
transformKey = _ref.transformKey, | ||
extraOptions = _objectWithoutProperties(_ref, ['isEqual', 'isPromise', 'maxSize', 'onCacheAdd', 'onCacheChange', 'onCacheHit', 'transformKey']); | ||
var normalizedOptions = { | ||
var normalizedOptions = Object.assign({}, extraOptions, { | ||
isEqual: isEqual, | ||
isPromise: isPromise, | ||
maxSize: maxSize, | ||
onCacheAdd: onCacheAdd, | ||
onCacheChange: onCacheChange, | ||
onCacheHit: onCacheHit, | ||
transformKey: transformKey | ||
}; | ||
}); | ||
@@ -73,2 +77,4 @@ var getKeyIndex = createGetKeyIndex(isEqual); | ||
if (~keyIndex) { | ||
onCacheHit(cache, normalizedOptions); | ||
if (keyIndex) { | ||
@@ -80,4 +86,2 @@ orderByLru(cache.keys, keyIndex); | ||
} | ||
onCacheHit(cache, normalizedOptions); | ||
} else { | ||
@@ -96,2 +100,3 @@ if (cache.keys.length >= maxSize) { | ||
onCacheAdd(cache, normalizedOptions); | ||
onCacheChange(cache, normalizedOptions); | ||
@@ -98,0 +103,0 @@ } |
@@ -89,3 +89,3 @@ | ||
/* eslint-disable no-unused-vars */ | ||
export var onCacheChangeOrHitNoOp = function onCacheChangeOrHitNoOp(cacheIgnored, optionsIgnored) {}; | ||
export var onCacheOperation = function onCacheOperation(cacheIgnored, optionsIgnored) {}; | ||
/* eslint-enable */ | ||
@@ -92,0 +92,0 @@ |
@@ -11,2 +11,3 @@ declare module 'micro-memoize' { | ||
maxSize?: number, | ||
onCacheAdd?: (cache: Cache, options: Options) => void, | ||
onCacheChange?: (cache: Cache, options: Options) => void, | ||
@@ -13,0 +14,0 @@ onCacheHit?: (cache: Cache, options: Options) => void, |
@@ -10,2 +10,3 @@ interface Cache = { | ||
maxSize?: number; | ||
onCacheAdd?: (cache: Cache, options: Options) => void; | ||
onCacheChange?: (cache: Cache, options: Options) => void; | ||
@@ -12,0 +13,0 @@ onCacheHit?: (cache: Cache, options: Options) => void; |
@@ -8,2 +8,7 @@ 'use strict'; | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
// utils | ||
// types | ||
@@ -24,5 +29,2 @@ var slice = [].slice; | ||
*/ | ||
// utils | ||
function memoize(fn, options) { | ||
@@ -41,16 +43,20 @@ // if it is a memoized method, don't re-memoize it | ||
maxSize = _ref$maxSize === undefined ? 1 : _ref$maxSize, | ||
_ref$onCacheAdd = _ref.onCacheAdd, | ||
onCacheAdd = _ref$onCacheAdd === undefined ? _utils.onCacheOperation : _ref$onCacheAdd, | ||
_ref$onCacheChange = _ref.onCacheChange, | ||
onCacheChange = _ref$onCacheChange === undefined ? _utils.onCacheChangeOrHitNoOp : _ref$onCacheChange, | ||
onCacheChange = _ref$onCacheChange === undefined ? _utils.onCacheOperation : _ref$onCacheChange, | ||
_ref$onCacheHit = _ref.onCacheHit, | ||
onCacheHit = _ref$onCacheHit === undefined ? _utils.onCacheChangeOrHitNoOp : _ref$onCacheHit, | ||
transformKey = _ref.transformKey; | ||
onCacheHit = _ref$onCacheHit === undefined ? _utils.onCacheOperation : _ref$onCacheHit, | ||
transformKey = _ref.transformKey, | ||
extraOptions = _objectWithoutProperties(_ref, ['isEqual', 'isPromise', 'maxSize', 'onCacheAdd', 'onCacheChange', 'onCacheHit', 'transformKey']); | ||
var normalizedOptions = { | ||
var normalizedOptions = Object.assign({}, extraOptions, { | ||
isEqual: isEqual, | ||
isPromise: isPromise, | ||
maxSize: maxSize, | ||
onCacheAdd: onCacheAdd, | ||
onCacheChange: onCacheChange, | ||
onCacheHit: onCacheHit, | ||
transformKey: transformKey | ||
}; | ||
}); | ||
@@ -78,2 +84,4 @@ var getKeyIndex = (0, _utils.createGetKeyIndex)(isEqual); | ||
if (~keyIndex) { | ||
onCacheHit(cache, normalizedOptions); | ||
if (keyIndex) { | ||
@@ -85,4 +93,2 @@ (0, _utils.orderByLru)(cache.keys, keyIndex); | ||
} | ||
onCacheHit(cache, normalizedOptions); | ||
} else { | ||
@@ -101,2 +107,3 @@ if (cache.keys.length >= maxSize) { | ||
onCacheAdd(cache, normalizedOptions); | ||
onCacheChange(cache, normalizedOptions); | ||
@@ -103,0 +110,0 @@ } |
@@ -88,3 +88,3 @@ 'use strict'; | ||
/* eslint-disable no-unused-vars */ | ||
var onCacheChangeOrHitNoOp = exports.onCacheChangeOrHitNoOp = function onCacheChangeOrHitNoOp(cacheIgnored, optionsIgnored) {}; | ||
var onCacheOperation = exports.onCacheOperation = function onCacheOperation(cacheIgnored, optionsIgnored) {}; | ||
/* eslint-enable */ | ||
@@ -91,0 +91,0 @@ |
@@ -33,4 +33,4 @@ { | ||
"babel-core": "^6.26.0", | ||
"babel-eslint": "^8.2.1", | ||
"babel-loader": "^7.1.2", | ||
"babel-eslint": "^8.2.2", | ||
"babel-loader": "^7.1.3", | ||
"babel-plugin-syntax-flow": "^6.18.0", | ||
@@ -46,7 +46,7 @@ "babel-plugin-transform-flow-strip-types": "^6.22.0", | ||
"cli-table2": "^0.2.0", | ||
"eslint": "^4.17.0", | ||
"eslint": "^4.18.1", | ||
"eslint-config-rapid7": "^2.11.1", | ||
"eslint-friendly-formatter": "^3.0.0", | ||
"eslint-loader": "^1.9.0", | ||
"fast-equals": "^1.0.6", | ||
"fast-equals": "^1.1.0", | ||
"fast-memoize": "^2.3.0", | ||
@@ -59,3 +59,3 @@ "flow-babel-webpack-plugin": "^1.1.1", | ||
"lru-memoize": "^1.0.2", | ||
"memoizee": "^0.4.11", | ||
"memoizee": "^0.4.12", | ||
"memoizerific": "^1.11.2", | ||
@@ -73,5 +73,5 @@ "moize": "^4.0.4", | ||
"rimraf": "^2.6.2", | ||
"sinon": "^4.2.2", | ||
"sinon": "^4.4.2", | ||
"underscore": "^1.8.3", | ||
"webpack": "^3.11.0", | ||
"webpack": "^3.10.0", | ||
"webpack-dev-server": "^2.11.1" | ||
@@ -123,3 +123,3 @@ }, | ||
"types": "./index.d.ts", | ||
"version": "1.5.0" | ||
"version": "1.6.0" | ||
} |
156
README.md
@@ -13,2 +13,3 @@ # micro-memoize | ||
* [maxSize](#maxsize) | ||
* [onCacheAdd](#oncacheadd) | ||
* [onCacheChange](#oncachechange) | ||
@@ -33,3 +34,3 @@ * [onCacheHit](#oncachehit) | ||
As the author of [`moize`](https://github.com/planttheidea/moize), I created a consistently fast memoization library, but `moize` has a lot of features to satisfy a large number of edge cases. `micro-memoize` is a simpler approach, focusing on the core feature set with a much smaller footprint (~1.2kB minified+gzipped). Stripping out these edge cases also allows `micro-memoize` to be faster across the board than `moize`. | ||
As the author of [`moize`](https://github.com/planttheidea/moize), I created a consistently fast memoization library, but `moize` has a lot of features to satisfy a large number of edge cases. `micro-memoize` is a simpler approach, focusing on the core feature set with a much smaller footprint (~1.3kB minified+gzipped). Stripping out these edge cases also allows `micro-memoize` to be faster across the board than `moize`. | ||
@@ -40,6 +41,6 @@ ## Usage | ||
// ES2015+ | ||
import memoize from 'micro-memoize'; | ||
import memoize from "micro-memoize"; | ||
// CommonJS | ||
const memoize = require('micro-memoize').default; | ||
const memoize = require("micro-memoize").default; | ||
@@ -50,3 +51,3 @@ // old-school | ||
const assembleToObject = (one, two) => { | ||
return {one, two}; | ||
return { one, two }; | ||
}; | ||
@@ -56,4 +57,4 @@ | ||
console.log(memoized('one', 'two')); // {one: 'one', two: 'two'} | ||
console.log(memoized('one', 'two')); // pulled from cache, {one: 'one', two: 'two'} | ||
console.log(memoized("one", "two")); // {one: 'one', two: 'two'} | ||
console.log(memoized("one", "two")); // pulled from cache, {one: 'one', two: 'two'} | ||
``` | ||
@@ -76,5 +77,5 @@ | ||
```javascript | ||
import {deepEqual} from 'fast-equals'; | ||
import { deepEqual } from "fast-equals"; | ||
const deepObject = (object) => { | ||
const deepObject = object => { | ||
return { | ||
@@ -86,3 +87,3 @@ foo: object.foo, | ||
const memoizedDeepObject = memoize(deepObject, {isEqual: deepEqual}); | ||
const memoizedDeepObject = memoize(deepObject, { isEqual: deepEqual }); | ||
@@ -92,9 +93,9 @@ console.log( | ||
foo: { | ||
deep: 'foo' | ||
deep: "foo" | ||
}, | ||
bar: { | ||
deep: 'bar' | ||
deep: "bar" | ||
}, | ||
baz: { | ||
deep: 'baz' | ||
deep: "baz" | ||
} | ||
@@ -107,9 +108,9 @@ }) | ||
foo: { | ||
deep: 'foo' | ||
deep: "foo" | ||
}, | ||
bar: { | ||
deep: 'bar' | ||
deep: "bar" | ||
}, | ||
baz: { | ||
deep: 'baz' | ||
deep: "baz" | ||
} | ||
@@ -132,3 +133,3 @@ }) | ||
setTimeout(() => { | ||
reject(new Error({one, two})); | ||
reject(new Error({ one, two })); | ||
}, 500); | ||
@@ -138,5 +139,5 @@ }); | ||
const memoized = memoize(fn, {isPromise: true}); | ||
const memoized = memoize(fn, { isPromise: true }); | ||
memoized('one', 'two'); | ||
memoized("one", "two"); | ||
@@ -165,13 +166,13 @@ console.log(memoized.cacheSnapshot.keys); // [['one', 'two']] | ||
const memoized = memoize(manyPossibleArgs, {maxSize: 3}); | ||
const memoized = memoize(manyPossibleArgs, { maxSize: 3 }); | ||
console.log(memoized('one', 'two')); // ['one', 'two'] | ||
console.log(memoized('two', 'three')); // ['two', 'three'] | ||
console.log(memoized('three', 'four')); // ['three', 'four'] | ||
console.log(memoized("one", "two")); // ['one', 'two'] | ||
console.log(memoized("two", "three")); // ['two', 'three'] | ||
console.log(memoized("three", "four")); // ['three', 'four'] | ||
console.log(memoized('one', 'two')); // pulled from cache | ||
console.log(memoized('two', 'three')); // pulled from cache | ||
console.log(memoized('three', 'four')); // pulled from cache | ||
console.log(memoized("one", "two")); // pulled from cache | ||
console.log(memoized("two", "three")); // pulled from cache | ||
console.log(memoized("three", "four")); // pulled from cache | ||
console.log(memoized('four', 'five')); // ['four', 'five'], drops ['one', 'two'] from cache | ||
console.log(memoized("four", "five")); // ['four', 'five'], drops ['one', 'two'] from cache | ||
``` | ||
@@ -181,2 +182,35 @@ | ||
#### onCacheAdd | ||
`function(cache: Cache, options: Options): void`, _defaults to noop_ | ||
Callback method that executes whenever the cache is added to. This is mainly to allow for higher-order caching managers that use `micro-memoize` to perform superset functionality on the `cache` object. | ||
```javascript | ||
const fn = (one, two) => { | ||
return [one, two]; | ||
}; | ||
const memoized = memoize(fn, { | ||
onCacheAdd(cache, options) { | ||
console.log("cache has been added to: ", cache); | ||
console.log("memoized method has the following options applied: ", options); | ||
} | ||
}); | ||
memoized("foo", "bar"); // cache has been added to | ||
memoized("foo", "bar"); | ||
memoized("foo", "bar"); | ||
memoized("bar", "foo"); // cache has been added to | ||
memoized("bar", "foo"); | ||
memoized("bar", "foo"); | ||
memoized("foo", "bar"); | ||
memoized("foo", "bar"); | ||
memoized("foo", "bar"); | ||
``` | ||
**NOTE**: This method is not executed when the `cache` is manually manipulated, only when changed via calling the memoized method. | ||
#### onCacheChange | ||
@@ -186,3 +220,3 @@ | ||
Callback method that fires whenever the cache is added to or the order is updated. This is mainly to allow for higher-order caching managers that use `micro-memoize` to perform superset functionality on the `cache` object. | ||
Callback method that executes whenever the cache is added to or the order is updated. This is mainly to allow for higher-order caching managers that use `micro-memoize` to perform superset functionality on the `cache` object. | ||
@@ -196,21 +230,21 @@ ```javascript | ||
onCacheChange(cache, options) { | ||
console.log('cache has changed: ', cache); | ||
console.log('memoized method has the following options applied: ', options); | ||
console.log("cache has changed: ", cache); | ||
console.log("memoized method has the following options applied: ", options); | ||
} | ||
}); | ||
memoized('foo', 'bar'); // cache has changed | ||
memoized('foo', 'bar'); | ||
memoized('foo', 'bar'); | ||
memoized("foo", "bar"); // cache has changed | ||
memoized("foo", "bar"); | ||
memoized("foo", "bar"); | ||
memoized('bar', 'foo'); // cache has changed | ||
memoized('bar', 'foo'); | ||
memoized('bar', 'foo'); | ||
memoized("bar", "foo"); // cache has changed | ||
memoized("bar", "foo"); | ||
memoized("bar", "foo"); | ||
memoized('foo', 'bar'); // cache has changed | ||
memoized('foo', 'bar'); | ||
memoized('foo', 'bar'); | ||
memoized("foo", "bar"); // cache has changed | ||
memoized("foo", "bar"); | ||
memoized("foo", "bar"); | ||
``` | ||
**NOTE**: This method is not fired when the `cache` is manually manipulated, only when changed via calling the memoized method. | ||
**NOTE**: This method is not executed when the `cache` is manually manipulated, only when changed via calling the memoized method. When the execution of other cache listeners (`onCacheAdd`, `onCacheHit`) is applicable, this method will execute after those methods. | ||
@@ -221,3 +255,3 @@ #### onCacheHit | ||
Callback method that fires whenever the cache is hit, whether the order is updated or not. This is mainly to allow for higher-order caching managers that use `micro-memoize` to perform superset functionality on the `cache` object. | ||
Callback method that executes whenever the cache is hit, whether the order is updated or not. This is mainly to allow for higher-order caching managers that use `micro-memoize` to perform superset functionality on the `cache` object. | ||
@@ -232,21 +266,21 @@ ```javascript | ||
onCacheHit(cache, options) { | ||
console.log('cache was hit: ', cache); | ||
console.log('memoized method has the following options applied: ', options); | ||
console.log("cache was hit: ", cache); | ||
console.log("memoized method has the following options applied: ", options); | ||
} | ||
}); | ||
memoized('foo', 'bar'); | ||
memoized('foo', 'bar'); // cache was hit | ||
memoized('foo', 'bar'); // cache was hit | ||
memoized("foo", "bar"); | ||
memoized("foo", "bar"); // cache was hit | ||
memoized("foo", "bar"); // cache was hit | ||
memoized('bar', 'foo'); | ||
memoized('bar', 'foo'); // cache was hit | ||
memoized('bar', 'foo'); // cache was hit | ||
memoized("bar", "foo"); | ||
memoized("bar", "foo"); // cache was hit | ||
memoized("bar", "foo"); // cache was hit | ||
memoized('foo', 'bar'); // cache was hit | ||
memoized('foo', 'bar'); // cache was hit | ||
memoized('foo', 'bar'); // cache was hit | ||
memoized("foo", "bar"); // cache was hit | ||
memoized("foo", "bar"); // cache was hit | ||
memoized("foo", "bar"); // cache was hit | ||
``` | ||
**NOTE**: This method is not fired when the `cache` is manually manipulated, only when changed via calling the memoized method. | ||
**NOTE**: This method is not executed when the `cache` is manually manipulated, only when changed via calling the memoized method. | ||
@@ -268,4 +302,4 @@ #### transformKey | ||
console.log(memoized('one', () => {})); // ['one', () => {}] | ||
console.log(memoized('one', () => {})); // pulled from cache, ['one', () => {}] | ||
console.log(memoized("one", () => {})); // ['one', () => {}] | ||
console.log(memoized("one", () => {})); // pulled from cache, ['one', () => {}] | ||
``` | ||
@@ -291,4 +325,4 @@ | ||
console.log(memoized('one', () => {})); // ['one', () => {}] | ||
console.log(memoized('one', () => {})); // pulled from cache, ['one', () => {}] | ||
console.log(memoized("one", () => {})); // ['one', () => {}] | ||
console.log(memoized("one", () => {})); // pulled from cache, ['one', () => {}] | ||
``` | ||
@@ -315,3 +349,3 @@ | ||
const method = (one, two) => { | ||
return {one, two}; | ||
return { one, two }; | ||
}; | ||
@@ -321,6 +355,6 @@ | ||
memoized.cache.keys.push(['one', 'two']); | ||
memoized.cache.values.push('cached'); | ||
memoized.cache.keys.push(["one", "two"]); | ||
memoized.cache.values.push("cached"); | ||
console.log(memoized('one', 'two')); // 'cached' | ||
console.log(memoized("one", "two")); // 'cached' | ||
``` | ||
@@ -421,3 +455,3 @@ | ||
* Chrome (all versions) | ||
* Firefox (all versions) | ||
* executefox (all versions) | ||
* Edge (all versions) | ||
@@ -424,0 +458,0 @@ * Opera 15+ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
326821
1.79%1230
1.65%462
7.94%