Add support for an immutable Map to _.memoize.Cache.

This commit is contained in:
jdalton
2015-05-05 21:31:36 -07:00
parent 1afcfa4406
commit ca5fa9e84d
2 changed files with 69 additions and 21 deletions

View File

@@ -8154,14 +8154,14 @@
} }
var memoized = function() { var memoized = function() {
var args = arguments, var args = arguments,
cache = memoized.cache, key = resolver ? resolver.apply(this, args) : args[0],
key = resolver ? resolver.apply(this, args) : args[0]; cache = memoized.cache;
if (cache.has(key)) { if (cache.has(key)) {
return cache.get(key); return cache.get(key);
} }
var result = func.apply(this, args); var result = func.apply(this, args);
cache.set(key, result); memoized.cache = cache.set(key, result);
return result; return result;
}; };
memoized.cache = new memoize.Cache; memoized.cache = new memoize.Cache;

View File

@@ -10295,11 +10295,10 @@
}); });
test('should check cache for own properties', 1, function() { test('should check cache for own properties', 1, function() {
var actual = [], var memoized = _.memoize(_.identity);
memoized = _.memoize(_.identity);
_.each(shadowProps, function(value) { var actual = _.map(shadowProps, function(value) {
actual.push(memoized(value)); return memoized(value);
}); });
deepEqual(actual, shadowProps); deepEqual(actual, shadowProps);
@@ -10307,9 +10306,13 @@
test('should expose a `cache` object on the `memoized` function which implements `Map` interface', 18, function() { test('should expose a `cache` object on the `memoized` function which implements `Map` interface', 18, function() {
_.times(2, function(index) { _.times(2, function(index) {
var resolver = index ? _.identity : null, var resolver = index ? _.identity : null;
memoized = _.memoize(function(value) { return 'value:' + value; }, resolver),
cache = memoized.cache; var memoized = _.memoize(function(value) {
return 'value:' + value;
}, resolver);
var cache = memoized.cache;
memoized('a'); memoized('a');
@@ -10349,7 +10352,7 @@
}); });
}); });
test('should allow `_.memoize.Cache` to be customized', 4, function() { test('should allow `_.memoize.Cache` to be customized', 5, function() {
var oldCache = _.memoize.Cache var oldCache = _.memoize.Cache
function Cache() { function Cache() {
@@ -10382,27 +10385,72 @@
}, },
'set': function(key, value) { 'set': function(key, value) {
this.__data__.push({ 'key': key, 'value': value }); this.__data__.push({ 'key': key, 'value': value });
return this;
} }
}; };
_.memoize.Cache = Cache; _.memoize.Cache = Cache;
var memoized = _.memoize(function(object) { var memoized = _.memoize(function(object) {
return '`id` is "' + object.id + '"'; return 'value:' + object.id;
}); });
var actual = memoized({ 'id': 'a' }); var cache = memoized.cache,
strictEqual(actual, '`id` is "a"'); key1 = { 'id': 'a' },
key2 = { 'id': 'b' };
var key = { 'id': 'b' }; strictEqual(memoized(key1), 'value:a');
actual = memoized(key); strictEqual(cache.has(key1), true);
strictEqual(actual, '`id` is "b"');
strictEqual(memoized(key2), 'value:b');
strictEqual(cache.has(key2), true);
cache['delete'](key2);
strictEqual(cache.has(key2), false);
_.memoize.Cache = oldCache;
});
test('should works with an immutable `_.memoize.Cache` ', 2, function() {
var oldCache = _.memoize.Cache
function Cache() {
this.__data__ = [];
}
Cache.prototype = {
'get': function(key) {
return _.find(this.__data__, function(entry) {
return key === entry.key;
}).value;
},
'has': function(key) {
return _.some(this.__data__, function(entry) {
return key === entry.key;
});
},
'set': function(key, value) {
var result = new Cache;
result.__data__ = this.__data__.concat({ 'key': key, 'value': value });
return result;
}
};
_.memoize.Cache = Cache;
var memoized = _.memoize(function(object) {
return object.id;
});
var key1 = { 'id': 'a' },
key2 = { 'id': 'b' };
memoized(key1);
memoized(key2);
var cache = memoized.cache; var cache = memoized.cache;
strictEqual(cache.has(key), true); strictEqual(cache.has(key1), true);
strictEqual(cache.has(key2), true);
cache['delete'](key);
strictEqual(cache.has(key), false);
_.memoize.Cache = oldCache; _.memoize.Cache = oldCache;
}); });