mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 18:07:49 +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`.
|
* @returns {boolean} Returns `true` if the entry was removed successfully, else `false`.
|
||||||
*/
|
*/
|
||||||
function mapDelete(key) {
|
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.
|
* @returns {*} Returns the cached value.
|
||||||
*/
|
*/
|
||||||
function mapGet(key) {
|
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);
|
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) {
|
_.times(2, function(index) {
|
||||||
var resolver = index ? _.identity : null,
|
var resolver = index ? _.identity : null,
|
||||||
memoized = _.memoize(_.identity, resolver);
|
memoized = _.memoize(function(value) { return 'value:' + value; }, resolver),
|
||||||
|
cache = memoized.cache;
|
||||||
|
|
||||||
memoized('a');
|
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) {
|
_.times(2, function(index) {
|
||||||
var count = 0,
|
var count = 0,
|
||||||
|
key = '__proto__',
|
||||||
resolver = index && _.identity;
|
resolver = index && _.identity;
|
||||||
|
|
||||||
var memoized = _.memoize(function() {
|
var memoized = _.memoize(function() {
|
||||||
@@ -7980,11 +7991,15 @@
|
|||||||
return [];
|
return [];
|
||||||
}, resolver);
|
}, resolver);
|
||||||
|
|
||||||
memoized('__proto__');
|
var cache = memoized.cache;
|
||||||
memoized('__proto__');
|
|
||||||
|
memoized(key);
|
||||||
|
memoized(key);
|
||||||
|
|
||||||
strictEqual(count, 2);
|
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