mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 15:57:48 +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)) {
|
||||
newValue = isArray(oldValue)
|
||||
? oldValue
|
||||
: ((isObject(oldValue) && isArrayLike(oldValue)) ? copyArray(oldValue) : srcValue);
|
||||
: ((isObject(oldValue) && isArrayLike(oldValue)) ? copyArray(oldValue) : baseClone(srcValue));
|
||||
}
|
||||
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
|
||||
newValue = isArguments(oldValue)
|
||||
? toPlainObject(oldValue)
|
||||
: (isObject(oldValue) ? oldValue : srcValue);
|
||||
: (isObject(oldValue) ? oldValue : baseClone(srcValue));
|
||||
}
|
||||
else {
|
||||
isCommon = isFunction(srcValue);
|
||||
|
||||
22
test/test.js
22
test/test.js
@@ -12245,17 +12245,31 @@
|
||||
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);
|
||||
|
||||
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));
|
||||
|
||||
var actual = _.map(values, function(value) {
|
||||
var object = _.merge({}, { 'a': value });
|
||||
return object.a === value;
|
||||
var object = _.merge({}, { 'value': 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);
|
||||
|
||||
Reference in New Issue
Block a user