mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 23:37:49 +00:00
Ensure _.merge correctly coerces objects to arrays. [closes #1061]
This commit is contained in:
@@ -2581,7 +2581,7 @@
|
||||
if (isLength(srcValue.length) && (isArray(srcValue) || isTypedArray(srcValue))) {
|
||||
result = isArray(value)
|
||||
? value
|
||||
: (value ? arrayCopy(value) : []);
|
||||
: ((value && value.length) ? arrayCopy(value) : []);
|
||||
}
|
||||
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
|
||||
result = isArguments(value)
|
||||
|
||||
18
test/test.js
18
test/test.js
@@ -9995,8 +9995,10 @@
|
||||
});
|
||||
|
||||
test('should work with four arguments', 1, function() {
|
||||
var expected = { 'a': 4 };
|
||||
deepEqual(_.merge({ 'a': 1 }, { 'a': 2 }, { 'a': 3 }, expected), expected);
|
||||
var expected = { 'a': 4 },
|
||||
actual = _.merge({ 'a': 1 }, { 'a': 2 }, { 'a': 3 }, expected);
|
||||
|
||||
deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
test('should assign `null` values', 1, function() {
|
||||
@@ -10009,7 +10011,7 @@
|
||||
deepEqual(actual, { 'a': 1 });
|
||||
});
|
||||
|
||||
test('should not not error on DOM elements', 1, function() {
|
||||
test('should not error on DOM elements', 1, function() {
|
||||
var object1 = { 'el': document && document.createElement('div') },
|
||||
object2 = { 'el': document && document.createElement('div') },
|
||||
pairs = [[{}, object1], [object1, object2]],
|
||||
@@ -10038,6 +10040,16 @@
|
||||
deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
test('should convert values to an array when merging with arrays of `source`', 2, function() {
|
||||
var object = { 'a': { '1': 'y', 'b': 'z', 'length': 2 } },
|
||||
actual = _.merge(object, { 'a': ['x'] });
|
||||
|
||||
deepEqual(actual, { 'a': ['x', 'y'] });
|
||||
|
||||
actual = _.merge({ 'a': {} }, { 'a': [] });
|
||||
deepEqual(actual, { 'a': [] });
|
||||
});
|
||||
|
||||
test('should work with a function `object` value', 2, function() {
|
||||
function Foo() {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user