mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-13 04:17:49 +00:00
Add mapDelete.
This commit is contained in:
14
lodash.js
14
lodash.js
@@ -1445,6 +1445,19 @@
|
|||||||
this.__data__ = {};
|
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`.
|
* Gets the cached value for `key`.
|
||||||
*
|
*
|
||||||
@@ -9970,6 +9983,7 @@
|
|||||||
LodashWrapper.prototype = lodash.prototype;
|
LodashWrapper.prototype = lodash.prototype;
|
||||||
|
|
||||||
// Add functions to the `Map` cache.
|
// Add functions to the `Map` cache.
|
||||||
|
MapCache.prototype['delete'] = mapDelete;
|
||||||
MapCache.prototype.get = mapGet;
|
MapCache.prototype.get = mapGet;
|
||||||
MapCache.prototype.has = mapHas;
|
MapCache.prototype.has = mapHas;
|
||||||
MapCache.prototype.set = mapSet;
|
MapCache.prototype.set = mapSet;
|
||||||
|
|||||||
36
test/test.js
36
test/test.js
@@ -7966,7 +7966,7 @@
|
|||||||
|
|
||||||
memoized('a');
|
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() {
|
test('should allow `_.memoize.Cache` to be customized', 4, function() {
|
||||||
var oldCache = _.memoize.Cache;
|
var oldCache = _.memoize.Cache
|
||||||
|
|
||||||
function Cache() {
|
function Cache() {
|
||||||
this.__wrapped__ = [];
|
this.__data__ = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
Cache.prototype = {
|
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) {
|
'get': function(key) {
|
||||||
return _.find(this.__wrapped__, function(entry) {
|
return _.find(this.__data__, function(entry) {
|
||||||
return key === entry.key;
|
return key === entry.key;
|
||||||
}).value;
|
}).value;
|
||||||
},
|
},
|
||||||
'has': function(key) {
|
'has': function(key) {
|
||||||
return _.some(this.__wrapped__, function(entry) {
|
return _.some(this.__data__, function(entry) {
|
||||||
return key === entry.key;
|
return key === entry.key;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
'set': function(key, value) {
|
'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' });
|
var actual = memoized({ 'id': 'a' });
|
||||||
strictEqual(actual, '`id` is "a"');
|
strictEqual(actual, '`id` is "a"');
|
||||||
|
|
||||||
actual = memoized({ 'id': 'b' });
|
var key = { 'id': 'b' };
|
||||||
|
actual = memoized(key);
|
||||||
strictEqual(actual, '`id` is "b"');
|
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;
|
_.memoize.Cache = oldCache;
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|||||||
Reference in New Issue
Block a user