mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 02:17:48 +00:00
Expose memoized function's cache. [closes #265]
Former-commit-id: fc44676386854ec9d5fd7a4fac8583508d63949f
This commit is contained in:
@@ -93,6 +93,7 @@
|
|||||||
'bind',
|
'bind',
|
||||||
'bindAll',
|
'bindAll',
|
||||||
'bindKey',
|
'bindKey',
|
||||||
|
'cache',
|
||||||
'clearTimeout',
|
'clearTimeout',
|
||||||
'clone',
|
'clone',
|
||||||
'cloneDeep',
|
'cloneDeep',
|
||||||
|
|||||||
11
lodash.js
11
lodash.js
@@ -4615,13 +4615,16 @@
|
|||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
function memoize(func, resolver) {
|
function memoize(func, resolver) {
|
||||||
var cache = {};
|
function memoized() {
|
||||||
return function() {
|
var cache = memoized.cache,
|
||||||
var key = keyPrefix + (resolver ? resolver.apply(this, arguments) : arguments[0]);
|
key = keyPrefix + (resolver ? resolver.apply(this, arguments) : arguments[0]);
|
||||||
|
|
||||||
return hasOwnProperty.call(cache, key)
|
return hasOwnProperty.call(cache, key)
|
||||||
? cache[key]
|
? cache[key]
|
||||||
: (cache[key] = func.apply(this, arguments));
|
: (cache[key] = func.apply(this, arguments));
|
||||||
};
|
}
|
||||||
|
memoized.cache = {};
|
||||||
|
return memoized;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
16
test/test.js
16
test/test.js
@@ -1900,6 +1900,22 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.memoize');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
test('should expose a `cache` object on the `memoized` function', function() {
|
||||||
|
var memoized = _.memoize(_.identity);
|
||||||
|
memoized('x');
|
||||||
|
|
||||||
|
var cache = memoized.cache,
|
||||||
|
key = _.keys(cache)[0];
|
||||||
|
|
||||||
|
equal(cache[key], 'x');
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.merge');
|
QUnit.module('lodash.merge');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user