mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 02:17:48 +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))) {
|
if (isLength(srcValue.length) && (isArray(srcValue) || isTypedArray(srcValue))) {
|
||||||
result = isArray(value)
|
result = isArray(value)
|
||||||
? value
|
? value
|
||||||
: (value ? arrayCopy(value) : []);
|
: ((value && value.length) ? arrayCopy(value) : []);
|
||||||
}
|
}
|
||||||
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
|
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
|
||||||
result = isArguments(value)
|
result = isArguments(value)
|
||||||
|
|||||||
18
test/test.js
18
test/test.js
@@ -9995,8 +9995,10 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should work with four arguments', 1, function() {
|
test('should work with four arguments', 1, function() {
|
||||||
var expected = { 'a': 4 };
|
var expected = { 'a': 4 },
|
||||||
deepEqual(_.merge({ 'a': 1 }, { 'a': 2 }, { 'a': 3 }, expected), expected);
|
actual = _.merge({ 'a': 1 }, { 'a': 2 }, { 'a': 3 }, expected);
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should assign `null` values', 1, function() {
|
test('should assign `null` values', 1, function() {
|
||||||
@@ -10009,7 +10011,7 @@
|
|||||||
deepEqual(actual, { 'a': 1 });
|
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') },
|
var object1 = { 'el': document && document.createElement('div') },
|
||||||
object2 = { 'el': document && document.createElement('div') },
|
object2 = { 'el': document && document.createElement('div') },
|
||||||
pairs = [[{}, object1], [object1, object2]],
|
pairs = [[{}, object1], [object1, object2]],
|
||||||
@@ -10038,6 +10040,16 @@
|
|||||||
deepEqual(actual, expected);
|
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() {
|
test('should work with a function `object` value', 2, function() {
|
||||||
function Foo() {}
|
function Foo() {}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user