From 79b9d20a912118bf06acd093071902ed972daf4e Mon Sep 17 00:00:00 2001 From: sina Date: Thu, 21 Jun 2018 00:54:05 +0430 Subject: [PATCH] Fix inconsistent merging of multiple sources to function property --- .gitignore | 1 + lodash.js | 2 +- test/test.js | 26 ++++++++++++++++++++++---- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 89f8a6bf9..ef37a96de 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.log doc/*.html node_modules +.idea diff --git a/lodash.js b/lodash.js index 0bda583bb..cfeb86902 100644 --- a/lodash.js +++ b/lodash.js @@ -3681,7 +3681,7 @@ if (isArguments(objValue)) { newValue = toPlainObject(objValue); } - else if (!isObject(objValue) || (srcIndex && isFunction(objValue))) { + else if (!isObject(objValue) || isFunction(objValue)) { newValue = initCloneObject(srcValue); } } diff --git a/test/test.js b/test/test.js index e57b3db47..b324e1202 100644 --- a/test/test.js +++ b/test/test.js @@ -14830,7 +14830,7 @@ }()); /*--------------------------------------------------------------------------*/ - +// sina QUnit.module('lodash.merge'); (function() { @@ -14911,8 +14911,28 @@ assert.strictEqual(Foo.a, 1); }); + QUnit.test('should merge first source object properties to function', function(assert) { + assert.expect(1); + + const Foo = function () {}; + var object = { prop: {} }, + actual = _.merge({prop: Foo}, object); + + assert.deepEqual(actual, object); + }); + + QUnit.test('should merge first and second source object properties to function', function(assert) { + assert.expect(1); + + const fn = function () {}; + var object = { prop: {} }, + actual = _.merge({prop: fn}, {prop: fn}, object); + + assert.deepEqual(actual, object); + }); + QUnit.test('should not merge onto function values of sources', function(assert) { - assert.expect(3); + assert.expect(2); var source1 = { 'a': function() {} }, source2 = { 'a': { 'b': 2 } }, @@ -14921,8 +14941,6 @@ assert.deepEqual(actual, { 'a': { 'b': 2 } }); actual = _.merge(source1, source2); - - assert.strictEqual(typeof actual.a, 'function'); assert.strictEqual(actual.a.b, 2); });