Ensure _.concat treats nullish values as empty arrays.

This commit is contained in:
John-David Dalton
2016-01-21 11:15:37 -06:00
parent 78982f3074
commit 6cbd2c4188
2 changed files with 18 additions and 3 deletions

View File

@@ -5336,11 +5336,11 @@
* // => [1] * // => [1]
*/ */
var concat = rest(function(array, values) { var concat = rest(function(array, values) {
if (array == null) { if (!isArray(array)) {
return []; array = array == null ? [] : [Object(array)];
} }
values = baseFlatten(values); values = baseFlatten(values);
return arrayConcat(isArray(array) ? array : [Object(array)], values); return arrayConcat(array, values);
}); });
/** /**

View File

@@ -2842,6 +2842,21 @@
assert.deepEqual(actual, expected); assert.deepEqual(actual, expected);
}); });
QUnit.test('should treat nullish `array` values as empty arrays', function(assert) {
assert.expect(1);
var values = [null, undefined],
expected = lodashStable.map(values, lodashStable.constant([1, 2, [3]]));
var actual = lodashStable.map(values, function(value) {
try {
return _.concat(value, 1, [2], [[3]]);
} catch (e) {}
});
assert.deepEqual(actual, expected);
});
QUnit.test('should treat sparse arrays as dense', function(assert) { QUnit.test('should treat sparse arrays as dense', function(assert) {
assert.expect(3); assert.expect(3);