mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
Add MapCache tests.
This commit is contained in:
@@ -1455,7 +1455,7 @@
|
||||
* @returns {boolean} Returns `true` if the entry was removed successfully, else `false`.
|
||||
*/
|
||||
function mapDelete(key) {
|
||||
return delete this.__data__[key];
|
||||
return this.has(key) && delete this.__data__[key];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1468,7 +1468,7 @@
|
||||
* @returns {*} Returns the cached value.
|
||||
*/
|
||||
function mapGet(key) {
|
||||
return this.__data__[key];
|
||||
return key == '__proto__' ? undefined : this.__data__[key];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
29
test/test.js
29
test/test.js
@@ -7959,20 +7959,31 @@
|
||||
deepEqual(actual, shadowProps);
|
||||
});
|
||||
|
||||
test('should expose a `cache` object on the `memoized` function which implements `Map` interface', 2, function() {
|
||||
test('should expose a `cache` object on the `memoized` function which implements `Map` interface', 18, function() {
|
||||
_.times(2, function(index) {
|
||||
var resolver = index ? _.identity : null,
|
||||
memoized = _.memoize(_.identity, resolver);
|
||||
memoized = _.memoize(function(value) { return 'value:' + value; }, resolver),
|
||||
cache = memoized.cache;
|
||||
|
||||
memoized('a');
|
||||
|
||||
deepEqual(_.functions(memoized.cache).sort(), ['delete', 'get', 'has', 'set']);
|
||||
strictEqual(cache.has('a'), true);
|
||||
strictEqual(cache.get('a'), 'value:a');
|
||||
strictEqual(cache['delete']('a'), true);
|
||||
strictEqual(cache['delete']('b'), false);
|
||||
|
||||
strictEqual(cache.set('b', 'value:b'), cache);
|
||||
strictEqual(cache.has('b'), true);
|
||||
strictEqual(cache.get('b'), 'value:b');
|
||||
strictEqual(cache['delete']('b'), true);
|
||||
strictEqual(cache['delete']('a'), false);
|
||||
});
|
||||
});
|
||||
|
||||
test('should skip the `__proto__` key', 4, function() {
|
||||
test('should skip the `__proto__` key', 8, function() {
|
||||
_.times(2, function(index) {
|
||||
var count = 0,
|
||||
key = '__proto__',
|
||||
resolver = index && _.identity;
|
||||
|
||||
var memoized = _.memoize(function() {
|
||||
@@ -7980,11 +7991,15 @@
|
||||
return [];
|
||||
}, resolver);
|
||||
|
||||
memoized('__proto__');
|
||||
memoized('__proto__');
|
||||
var cache = memoized.cache;
|
||||
|
||||
memoized(key);
|
||||
memoized(key);
|
||||
|
||||
strictEqual(count, 2);
|
||||
ok(!(memoized.cache instanceof Array));
|
||||
strictEqual(cache.get(key), undefined);
|
||||
strictEqual(cache['delete'](key), false);
|
||||
ok(!(cache.__data__ instanceof Array));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user