Expose memoized function's cache. [closes #265]

Former-commit-id: fc44676386854ec9d5fd7a4fac8583508d63949f
This commit is contained in:
John-David Dalton
2013-05-10 23:20:10 -07:00
parent 6d86b3a950
commit 9bd0c01702
3 changed files with 24 additions and 4 deletions

View File

@@ -93,6 +93,7 @@
'bind',
'bindAll',
'bindKey',
'cache',
'clearTimeout',
'clone',
'cloneDeep',

View File

@@ -4615,13 +4615,16 @@
* });
*/
function memoize(func, resolver) {
var cache = {};
return function() {
var key = keyPrefix + (resolver ? resolver.apply(this, arguments) : arguments[0]);
function memoized() {
var cache = memoized.cache,
key = keyPrefix + (resolver ? resolver.apply(this, arguments) : arguments[0]);
return hasOwnProperty.call(cache, key)
? cache[key]
: (cache[key] = func.apply(this, arguments));
};
}
memoized.cache = {};
return memoized;
}
/**

View File

@@ -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');
(function() {