Add mapDelete.

This commit is contained in:
John-David Dalton
2014-11-28 13:47:38 -08:00
parent fb9118a044
commit ab3b972185
2 changed files with 42 additions and 8 deletions

View File

@@ -1445,6 +1445,19 @@
this.__data__ = {};
}
/**
* Removes `key` and its value from the cache.
*
* @private
* @name get
* @memberOf _.memoize.Cache
* @param {string} key The key of the value to remove.
* @returns {boolean} Returns `true` if the entry was removed successfully, else `false`.
*/
function mapDelete(key) {
return delete this.__data__[key];
}
/**
* Gets the cached value for `key`.
*
@@ -9970,6 +9983,7 @@
LodashWrapper.prototype = lodash.prototype;
// Add functions to the `Map` cache.
MapCache.prototype['delete'] = mapDelete;
MapCache.prototype.get = mapGet;
MapCache.prototype.has = mapHas;
MapCache.prototype.set = mapSet;

View File

@@ -7966,7 +7966,7 @@
memoized('a');
deepEqual(_.functions(memoized.cache).sort(), ['get', 'has', 'set']);
deepEqual(_.functions(memoized.cache).sort(), ['delete', 'get', 'has', 'set']);
});
});
@@ -7988,26 +7988,39 @@
});
});
test('should allow `_.memoize.Cache` to be customized', 2, function() {
var oldCache = _.memoize.Cache;
test('should allow `_.memoize.Cache` to be customized', 4, function() {
var oldCache = _.memoize.Cache
function Cache() {
this.__wrapped__ = [];
this.__data__ = [];
}
Cache.prototype = {
'delete': function(key) {
var data = this.__data__;
var index = _.findIndex(data, function(entry) {
return key === entry.key;
});
if (index < 0) {
return false;
}
data.splice(index, 1);
return true;
},
'get': function(key) {
return _.find(this.__wrapped__, function(entry) {
return _.find(this.__data__, function(entry) {
return key === entry.key;
}).value;
},
'has': function(key) {
return _.some(this.__wrapped__, function(entry) {
return _.some(this.__data__, function(entry) {
return key === entry.key;
});
},
'set': function(key, value) {
this.__wrapped__.push({ 'key': key, 'value': value });
this.__data__.push({ 'key': key, 'value': value });
}
};
@@ -8020,9 +8033,16 @@
var actual = memoized({ 'id': 'a' });
strictEqual(actual, '`id` is "a"');
actual = memoized({ 'id': 'b' });
var key = { 'id': 'b' };
actual = memoized(key);
strictEqual(actual, '`id` is "b"');
var cache = memoized.cache;
strictEqual(cache.has(key), true);
cache['delete'](key);
strictEqual(cache.has(key), false);
_.memoize.Cache = oldCache;
});
}());