mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Ensure _.merge doesn't modify array/typed-array/plain-object source values.
This commit is contained in:
@@ -2565,12 +2565,12 @@
|
|||||||
if (isArray(srcValue) || isTypedArray(srcValue)) {
|
if (isArray(srcValue) || isTypedArray(srcValue)) {
|
||||||
newValue = isArray(oldValue)
|
newValue = isArray(oldValue)
|
||||||
? oldValue
|
? oldValue
|
||||||
: ((isObject(oldValue) && isArrayLike(oldValue)) ? copyArray(oldValue) : srcValue);
|
: ((isObject(oldValue) && isArrayLike(oldValue)) ? copyArray(oldValue) : baseClone(srcValue));
|
||||||
}
|
}
|
||||||
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
|
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
|
||||||
newValue = isArguments(oldValue)
|
newValue = isArguments(oldValue)
|
||||||
? toPlainObject(oldValue)
|
? toPlainObject(oldValue)
|
||||||
: (isObject(oldValue) ? oldValue : srcValue);
|
: (isObject(oldValue) ? oldValue : baseClone(srcValue));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
isCommon = isFunction(srcValue);
|
isCommon = isFunction(srcValue);
|
||||||
|
|||||||
22
test/test.js
22
test/test.js
@@ -12245,17 +12245,31 @@
|
|||||||
assert.strictEqual(actual.a, null);
|
assert.strictEqual(actual.a, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should assign non array/plain-object values directly', function(assert) {
|
QUnit.test('should assign non array/typed-array/plain-object sources directly', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
function Foo() {}
|
function Foo() {}
|
||||||
|
|
||||||
var values = [new Foo, new Boolean, new Date, Foo, new Number, new String, new RegExp, new (Uint8Array || noop)],
|
var values = [new Foo, new Boolean, new Date, Foo, new Number, new String, new RegExp],
|
||||||
expected = _.map(values, _.constant(true));
|
expected = _.map(values, _.constant(true));
|
||||||
|
|
||||||
var actual = _.map(values, function(value) {
|
var actual = _.map(values, function(value) {
|
||||||
var object = _.merge({}, { 'a': value });
|
var object = _.merge({}, { 'value': value });
|
||||||
return object.a === value;
|
return object.value === value;
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test('should shallow clone array/typed-array/plain-object sources', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var values = [[], new (Uint8Array || Object), {}],
|
||||||
|
expected = _.map(values, _.constant(true));
|
||||||
|
|
||||||
|
var actual = _.map(values, function(value) {
|
||||||
|
var object = _.merge({}, { 'value': value });
|
||||||
|
return object.value !== value && _.isEqual(object.value, value);
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.deepEqual(actual, expected);
|
assert.deepEqual(actual, expected);
|
||||||
|
|||||||
Reference in New Issue
Block a user