mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
Make _.memoize skip the __proto__ key.
This commit is contained in:
@@ -5340,9 +5340,11 @@
|
|||||||
throw new TypeError(funcErrorText);
|
throw new TypeError(funcErrorText);
|
||||||
}
|
}
|
||||||
var memoized = function() {
|
var memoized = function() {
|
||||||
var cache = memoized.cache,
|
var key = resolver ? resolver.apply(this, arguments) : arguments[0];
|
||||||
key = resolver ? resolver.apply(this, arguments) : '_' + arguments[0];
|
if (key == '__proto__') {
|
||||||
|
return func.apply(this, arguments);
|
||||||
|
}
|
||||||
|
var cache = memoized.cache;
|
||||||
return hasOwnProperty.call(cache, key)
|
return hasOwnProperty.call(cache, key)
|
||||||
? cache[key]
|
? cache[key]
|
||||||
: (cache[key] = func.apply(this, arguments));
|
: (cache[key] = func.apply(this, arguments));
|
||||||
|
|||||||
36
test/test.js
36
test/test.js
@@ -3443,15 +3443,6 @@
|
|||||||
deepEqual(_.uniq(largeArray), expected);
|
deepEqual(_.uniq(largeArray), expected);
|
||||||
deepEqual(_.without.apply(_, [largeArray].concat(largeArray)), []);
|
deepEqual(_.without.apply(_, [largeArray].concat(largeArray)), []);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('lodash.memoize should support values that resolve to the `__proto__` key', 1, function() {
|
|
||||||
var count = 0,
|
|
||||||
memoized = _.memoize(function() { return ++count; });
|
|
||||||
|
|
||||||
memoized('__proto__');
|
|
||||||
memoized('__proto__');
|
|
||||||
strictEqual(count, 1);
|
|
||||||
});
|
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
@@ -6098,16 +6089,35 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should expose a `cache` object on the `memoized` function', 4, function() {
|
test('should expose a `cache` object on the `memoized` function', 4, function() {
|
||||||
_.each(['_a', 'a'], function(key, index) {
|
_.times(2, function(index) {
|
||||||
var memoized = _.memoize(_.identity, index && _.identity);
|
var resolver = index && _.identity,
|
||||||
|
memoized = _.memoize(_.identity, resolver);
|
||||||
|
|
||||||
memoized('a');
|
memoized('a');
|
||||||
strictEqual(memoized.cache[key], 'a');
|
strictEqual(memoized.cache.a, 'a');
|
||||||
|
|
||||||
memoized.cache[key] = 'b';
|
memoized.cache.a = 'b';
|
||||||
strictEqual(memoized('a'), 'b');
|
strictEqual(memoized('a'), 'b');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should skip the `__proto__` key', 4, function() {
|
||||||
|
_.times(2, function(index) {
|
||||||
|
var count = 0,
|
||||||
|
resolver = index && _.identity;
|
||||||
|
|
||||||
|
var memoized = _.memoize(function() {
|
||||||
|
count++;
|
||||||
|
return [];
|
||||||
|
}, resolver);
|
||||||
|
|
||||||
|
memoized('__proto__');
|
||||||
|
memoized('__proto__');
|
||||||
|
|
||||||
|
strictEqual(count, 2);
|
||||||
|
ok(!(memoized.cache instanceof Array));
|
||||||
|
});
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user