From 3bdaf99cfaedd7aa16b815aeb17f9edc500f6fd6 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 31 Jan 2016 13:15:44 -0800 Subject: [PATCH] Ensure `_.iteratee` clones sources for "_.matchesProperty" shorthand. --- lodash.js | 4 +--- test/test.js | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lodash.js b/lodash.js index 93cffa545..71476698c 100644 --- a/lodash.js +++ b/lodash.js @@ -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)); } /** diff --git a/test/test.js b/test/test.js index 9672c9647..2a39ef54f 100644 --- a/test/test.js +++ b/test/test.js @@ -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);