From 53b3f81abee94d9575474edd10e81191a36f658d Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 11 Feb 2016 22:56:33 -0800 Subject: [PATCH] Ensure `_.merge` deep clones array/typed-array/plain-object sources. [closes #1987] --- lodash.js | 6 +++--- test/test.js | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lodash.js b/lodash.js index 673b88fc0..5dafbfa1c 100644 --- a/lodash.js +++ b/lodash.js @@ -3081,7 +3081,7 @@ } else { isCommon = false; - newValue = baseClone(srcValue); + newValue = baseClone(srcValue, true); } } else if (isPlainObject(srcValue) || isArguments(srcValue)) { @@ -3090,10 +3090,10 @@ } else if (!isObject(objValue) || (srcIndex && isFunction(objValue))) { isCommon = false; - newValue = baseClone(srcValue); + newValue = baseClone(srcValue, true); } else { - newValue = srcIndex ? baseClone(objValue) : objValue; + newValue = objValue; } } else { diff --git a/test/test.js b/test/test.js index b51385111..b340daa94 100644 --- a/test/test.js +++ b/test/test.js @@ -13937,15 +13937,27 @@ assert.deepEqual(actual.a, [[3, 4, 3]]); }); - QUnit.test('should shallow clone array/typed-array/plain-object sources', function(assert) { + QUnit.test('should deep clone array/typed-array/plain-object sources', function(assert) { assert.expect(1); - var values = [[], new (Uint8Array || Object), {}], + var typedArray = Uint8Array + ? new Uint8Array(new ArrayBuffer(2)) + : { 'buffer': [0, 0] }; + + var props = ['0', 'a', 'buffer'], + values = [[{ 'a': 1 }], { 'a': [1] }, typedArray], expected = lodashStable.map(values, alwaysTrue); - var actual = lodashStable.map(values, function(value) { - var object = _.merge({}, { 'value': value }); - return object.value !== value && lodashStable.isEqual(object.value, value); + var actual = lodashStable.map(values, function(value, index) { + var key = props[index], + object = _.merge({}, { 'value': value }), + newValue = object.value; + + return ( + newValue !== value && + newValue[key] !== value[key] && + lodashStable.isEqual(newValue, value) + ); }); assert.deepEqual(actual, expected);