Ensure _.concat cases wraps nullish array values in an array.

This commit is contained in:
John-David Dalton
2016-03-28 17:44:13 -07:00
parent a7bf3352df
commit 3fe2efa311
2 changed files with 23 additions and 37 deletions

View File

@@ -5853,13 +5853,19 @@
* console.log(array); * console.log(array);
* // => [1] * // => [1]
*/ */
var concat = rest(function(array, values) { function concat() {
if (!isArray(array)) { var length = arguments.length;
array = array == null ? [] : [array]; if (!length) {
return [];
} }
values = baseFlatten(values, 1); var array = castArray(arguments[0]),
return arrayConcat(array, values); args = Array(length - 1);
});
while (length--) {
args[length - 1] = arguments[length];
}
return arrayConcat(array, baseFlatten(args, 1));
}
/** /**
* Creates an array of unique `array` values not included in the other given * Creates an array of unique `array` values not included in the other given

View File

@@ -3066,46 +3066,26 @@
assert.deepEqual(array, [1]); assert.deepEqual(array, [1]);
}); });
QUnit.test('should return an empty array when `array` is nullish', function(assert) { QUnit.test('should cast non-array `array` values to arrays', function(assert) {
assert.expect(1); assert.expect(2);
var values = [, null, undefined], var values = [, null, undefined, false, true, 1, NaN, 'a'];
expected = lodashStable.map(values, alwaysEmptyArray);
var expected = lodashStable.map(values, function(value, index) {
return index ? [value] : [];
});
var actual = lodashStable.map(values, function(value, index) { var actual = lodashStable.map(values, function(value, index) {
try { return index ? _.concat(value) : _.concat();
return index ? _.concat(value) : _.concat();
} catch (e) {}
}); });
assert.deepEqual(actual, expected); assert.deepEqual(actual, expected);
});
QUnit.test('should treat nullish `array` values as empty arrays', function(assert) { expected = lodashStable.map(values, function(value) {
assert.expect(1); return [value, 2, [3]];
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); actual = lodashStable.map(values, function(value) {
});
QUnit.test('should cast non-array `array` values to arrays', function(assert) {
assert.expect(1);
var values = [true, false, 1, NaN, 'a'];
var expected = lodashStable.map(values, function(value) {
return [value, 2, [3]]
});
var actual = lodashStable.map(values, function(value) {
return _.concat(value, [2], [[3]]); return _.concat(value, [2], [[3]]);
}); });