mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 18:17:48 +00:00
Make _.memoize avoid using keyPrefix if passed a resolver function. [closes #330]
Former-commit-id: 97e3bb353d988c92eea394dfb496ebf7594ae25f
This commit is contained in:
16
lodash.js
16
lodash.js
@@ -5528,6 +5528,20 @@
|
||||
* var fibonacci = _.memoize(function(n) {
|
||||
* return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
|
||||
* });
|
||||
*
|
||||
* var data = {
|
||||
* 'moe': { 'name': 'moe', 'age': 40 },
|
||||
* 'curly': { 'name': 'curly', 'age': 60 }
|
||||
* };
|
||||
*
|
||||
* // modifying the result cache
|
||||
* var stooge = _.memoize(function(name) { return data[name]; }, _.identity);
|
||||
* stooge('curly');
|
||||
* // => { 'name': 'curly', 'age': 60 }
|
||||
*
|
||||
* stooge.cache.curly.name = 'jerome';
|
||||
* stooge('curly');
|
||||
* // => { 'name': 'jerome', 'age': 60 }
|
||||
*/
|
||||
function memoize(func, resolver) {
|
||||
if (!isFunction(func)) {
|
||||
@@ -5535,7 +5549,7 @@
|
||||
}
|
||||
var memoized = function() {
|
||||
var cache = memoized.cache,
|
||||
key = keyPrefix + (resolver ? resolver.apply(this, arguments) : arguments[0]);
|
||||
key = resolver ? resolver.apply(this, arguments) : keyPrefix + arguments[0];
|
||||
|
||||
return hasOwnProperty.call(cache, key)
|
||||
? cache[key]
|
||||
|
||||
Reference in New Issue
Block a user