Ensure _.iteratee clones sources for "_.matchesProperty" shorthand.

This commit is contained in:
John-David Dalton
2016-01-31 13:15:44 -08:00
parent 6c85e7015a
commit 3bdaf99cfa
2 changed files with 32 additions and 5 deletions

View File

@@ -13150,9 +13150,7 @@
* // => [{ 'user': 'fred', 'age': 40 }]
*/
function iteratee(func) {
return (isObjectLike(func) && !isArray(func))
? matches(func)
: baseIteratee(func);
return baseIteratee(typeof func == 'function' ? func : baseClone(func, true));
}
/**

View File

@@ -10623,7 +10623,7 @@
assert.strictEqual(matches({ 'b': 2 }), false);
});
QUnit.test('should not change behavior if `source` is modified', function(assert) {
QUnit.test('should not change `_.matches` behavior if `source` is modified', function(assert) {
assert.expect(9);
var sources = [
@@ -10652,7 +10652,7 @@
});
});
QUnit.test('should return an iteratee created by `_.matchesProperty` when `func` is a number or string and a value is provided', function(assert) {
QUnit.test('should return an iteratee created by `_.matchesProperty` when `func` is an array', function(assert) {
assert.expect(3);
var array = ['a', undefined],
@@ -10676,6 +10676,35 @@
assert.strictEqual(matches(object), true);
});
QUnit.test('should not change `_.matchesProperty` behavior if `source` is modified', function(assert) {
assert.expect(9);
var sources = [
{ 'a': { 'b': 2, 'c': 3 } },
{ 'a': 1, 'b': 2 },
{ 'a': 1 }
];
lodashStable.each(sources, function(source, index) {
var object = { 'a': lodashStable.cloneDeep(source) },
matches = _.iteratee(['a', source]);
assert.strictEqual(matches(object), true);
if (index) {
source.a = 2;
source.b = 1;
source.c = 3;
} else {
source.a.b = 1;
source.a.c = 2;
source.a.d = 3;
}
assert.strictEqual(matches(object), true);
assert.strictEqual(matches({ 'a': source }), false);
});
});
QUnit.test('should return an iteratee created by `_.property` when `func` is a number or string', function(assert) {
assert.expect(2);