memoize-id
Advanced tools
Comparing version 0.1.2 to 0.2.0
'use strict'; | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; } | ||
jest.dontMock('../index'); | ||
@@ -106,2 +108,122 @@ var memoize = require('../index').default; | ||
}); | ||
it('memoizes by the first N arguments when the arity option is passed', function () { | ||
var i = 0; | ||
function fn(foo, bar, baz) { | ||
i += 1; | ||
return { | ||
args: [foo, bar, baz], | ||
i: i | ||
}; | ||
} | ||
var memoizedFn = memoize(fn, { arity: 2 }); | ||
expect(memoizedFn('foo', 'bar', 'baz')).toEqual({ args: ['foo', 'bar', 'baz'], i: 1 }); | ||
expect(memoizedFn('foo', 'bar', 'qux')).toEqual({ args: ['foo', 'bar', 'baz'], i: 1 }); | ||
}); | ||
it('memoizes the initial return value when and arity of 0 is passed', function () { | ||
var i = 0; | ||
function fn(foo, bar, baz) { | ||
i += 1; | ||
return { | ||
args: [foo, bar, baz], | ||
i: i | ||
}; | ||
} | ||
var memoizedFn = memoize(fn, { arity: 0 }); | ||
expect(memoizedFn('foo', 'bar', 'baz')).toEqual({ args: ['foo', 'bar', 'baz'], i: 1 }); | ||
expect(memoizedFn('qux')).toEqual({ args: ['foo', 'bar', 'baz'], i: 1 }); | ||
}); | ||
pit('memoizes node-style async functions', _asyncToGenerator(regeneratorRuntime.mark(function _callee() { | ||
var i, fn, memoizedFn; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
fn = function fn(foo, bar, callback) { | ||
i += 1; | ||
setImmediate(function () { | ||
callback(null, { | ||
args: [foo, bar], | ||
i: i | ||
}); | ||
}); | ||
}; | ||
i = 0; | ||
memoizedFn = memoize(fn, { async: true }); | ||
_context.next = 5; | ||
return new Promise(function (resolve) { | ||
memoizedFn('foo', 'bar', function (error, result) { | ||
expect(result).toEqual({ args: ['foo', 'bar'], i: 1 }); | ||
resolve(); | ||
}); | ||
}); | ||
case 5: | ||
_context.next = 7; | ||
return new Promise(function (resolve) { | ||
var sync = true; | ||
memoizedFn('foo', 'bar', function (error, result) { | ||
expect(result).toEqual({ args: ['foo', 'bar'], i: 1 }); | ||
expect(sync).toBe(false); | ||
resolve(); | ||
}); | ||
sync = false; | ||
}); | ||
case 7: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, undefined); | ||
}))); | ||
pit('calls the callback immediately when using async: \'immediate\'', _asyncToGenerator(regeneratorRuntime.mark(function _callee2() { | ||
var i, fn, memoizedFn; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
fn = function fn(foo, bar, callback) { | ||
i += 1; | ||
setImmediate(function () { | ||
callback(null, { | ||
args: [foo, bar], | ||
i: i | ||
}); | ||
}); | ||
}; | ||
i = 0; | ||
memoizedFn = memoize(fn, { async: 'immediate' }); | ||
_context2.next = 5; | ||
return new Promise(function (resolve) { | ||
memoizedFn('foo', 'bar', function (error, result) { | ||
expect(result).toEqual({ args: ['foo', 'bar'], i: 1 }); | ||
resolve(); | ||
}); | ||
}); | ||
case 5: | ||
_context2.next = 7; | ||
return new Promise(function (resolve) { | ||
var sync = true; | ||
memoizedFn('foo', 'bar', function (error, result) { | ||
expect(result).toEqual({ args: ['foo', 'bar'], i: 1 }); | ||
expect(sync).toBe(true); | ||
resolve(); | ||
}); | ||
sync = false; | ||
}); | ||
case 7: | ||
case 'end': | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, undefined); | ||
}))); | ||
}); |
@@ -8,2 +8,4 @@ 'use strict'; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } | ||
@@ -66,16 +68,71 @@ | ||
function memoizeAsync(fn, _ref) { | ||
var arity = _ref.arity; | ||
var immediate = _ref.immediate; | ||
var cache = new Map(); | ||
return function memoized() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var fnArgs = args.slice(0, -1); | ||
var callback = args[args.length - 1]; | ||
var cacheArgs = arity !== undefined ? fnArgs.slice(0, arity) : fnArgs; | ||
var cached = get(cache, cacheArgs); | ||
if (cached) { | ||
if (immediate) { | ||
callback(null, cached); | ||
return; | ||
} | ||
setImmediate(function () { | ||
return callback(null, cached); | ||
}); | ||
return; | ||
} | ||
fn.apply(undefined, _toConsumableArray(fnArgs).concat([function (error, result) { | ||
if (!error) { | ||
set(cache, cacheArgs, result); | ||
} | ||
callback(error, result); | ||
}])); | ||
}; | ||
} | ||
/** | ||
* Memoize the given function by the identity of its arguments. | ||
* @param {Function} fn The function to memoize | ||
* @return {Function} The memoized function | ||
* @param {Function} fn | ||
* The function to memoize | ||
* @param {Object} [options] | ||
* Options | ||
* @param {Number} [options.arity] | ||
* The number of arguments to memoize by. If undefined, all arguments will be used. | ||
* @param {Boolean|String} [options.async] | ||
* Pass `true` if fn is a Node-style asynchronous function: results passed to the final | ||
* callback argument will be cached and provided asynchronously | ||
* (via `setImmediate`) to subsequent calls. | ||
* Pass 'immediate' to call the callback immediately for subsequent calls. | ||
* If an error is returned (as the first argument to the callback), the result | ||
* will not be cached. | ||
* @return {Function} | ||
* The memoized function | ||
*/ | ||
function memoize(fn) { | ||
var _ref2 = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var arity = _ref2.arity; | ||
var async = _ref2.async; | ||
if (async) { | ||
return memoizeAsync(fn, { arity: arity, immediate: async === 'immediate' }); | ||
} | ||
var cache = new Map(); | ||
return function memoized() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return get(cache, args) || set(cache, args, fn.apply(undefined, args)); | ||
var cacheArgs = arity !== undefined ? args.slice(0, arity) : args; | ||
return get(cache, cacheArgs) || set(cache, cacheArgs, fn.apply(undefined, args)); | ||
}; | ||
} |
{ | ||
"name": "memoize-id", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Memoize a function by the identity of its arguments, using ES2015 Map", | ||
@@ -28,11 +28,13 @@ "main": "lib/index.js", | ||
"babel-cli": "^6.5.1", | ||
"babel-jest": "^6.0.1", | ||
"babel-eslint": "^5.0.0", | ||
"babel-jest": "^9.0.0", | ||
"babel-plugin-transform-async-to-generator": "^6.7.0", | ||
"babel-polyfill": "^6.6.1", | ||
"babel-preset-es2015": "^6.5.0", | ||
"eslint": "^1.10.3", | ||
"eslint-config-airbnb": "^5.0.1", | ||
"eslint-plugin-react": "^3.16.1", | ||
"jest-cli": "^0.8.2" | ||
"eslint": "~2.2.0", | ||
"eslint-config-airbnb": "^6.0.2", | ||
"eslint-plugin-react": "^4.0.0", | ||
"jest-cli": "^0.9.0" | ||
}, | ||
"jest": { | ||
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest", | ||
"testPathDirs": [ | ||
@@ -39,0 +41,0 @@ "<rootDir>/src" |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25465
12
617
10
1