Skip to content

Commit 53b3f81

Browse files
committed
Ensure _.merge deep clones array/typed-array/plain-object sources. [closes #1987]
1 parent bd2428d commit 53b3f81

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

‎lodash.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,7 +3081,7 @@
30813081
}
30823082
else {
30833083
isCommon = false;
3084-
newValue = baseClone(srcValue);
3084+
newValue = baseClone(srcValue, true);
30853085
}
30863086
}
30873087
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
@@ -3090,10 +3090,10 @@
30903090
}
30913091
else if (!isObject(objValue) || (srcIndex && isFunction(objValue))) {
30923092
isCommon = false;
3093-
newValue = baseClone(srcValue);
3093+
newValue = baseClone(srcValue, true);
30943094
}
30953095
else {
3096-
newValue = srcIndex ? baseClone(objValue) : objValue;
3096+
newValue = objValue;
30973097
}
30983098
}
30993099
else {

‎test/test.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13937,15 +13937,27 @@
1393713937
assert.deepEqual(actual.a, [[3, 4, 3]]);
1393813938
});
1393913939

13940-
QUnit.test('should shallow clone array/typed-array/plain-object sources', function(assert) {
13940+
QUnit.test('should deep clone array/typed-array/plain-object sources', function(assert) {
1394113941
assert.expect(1);
1394213942

13943-
var values = [[], new (Uint8Array || Object), {}],
13943+
var typedArray = Uint8Array
13944+
? new Uint8Array(new ArrayBuffer(2))
13945+
: { 'buffer': [0, 0] };
13946+
13947+
var props = ['0', 'a', 'buffer'],
13948+
values = [[{ 'a': 1 }], { 'a': [1] }, typedArray],
1394413949
expected = lodashStable.map(values, alwaysTrue);
1394513950

13946-
var actual = lodashStable.map(values, function(value) {
13947-
var object = _.merge({}, { 'value': value });
13948-
return object.value !== value && lodashStable.isEqual(object.value, value);
13951+
var actual = lodashStable.map(values, function(value, index) {
13952+
var key = props[index],
13953+
object = _.merge({}, { 'value': value }),
13954+
newValue = object.value;
13955+
13956+
return (
13957+
newValue !== value &&
13958+
newValue[key] !== value[key] &&
13959+
lodashStable.isEqual(newValue, value)
13960+
);
1394913961
});
1395013962

1395113963
assert.deepEqual(actual, expected);

0 commit comments

Comments
 (0)