From 32cd726bd0d093122ddb16bee839a155485b5807 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 8 Nov 2015 20:50:21 -0800 Subject: [PATCH] Move `_#concat` to `_.concat`. --- lodash.js | 61 +++++++++++++++--------------- test/test.js | 104 +++++++++++++++++++++++---------------------------- 2 files changed, 76 insertions(+), 89 deletions(-) diff --git a/lodash.js b/lodash.js index 53c8a53ff..65c7d0f8a 100644 --- a/lodash.js +++ b/lodash.js @@ -372,11 +372,11 @@ } /** - * Creates a new array joining `array` with `other`. + * Creates a new array concatenating `array` with `other`. * * @private - * @param {Array} array The array to join. - * @param {Array} other The other array to join. + * @param {Array} array The first array to concatenate. + * @param {Array} other The second array to concatenate. * @returns {Array} Returns the new concatenated array. */ function arrayConcat(array, other) { @@ -5080,6 +5080,32 @@ return result; } + /** + * Creates a new array concatenating `array` with any additional arrays + * and/or values. + * + * @static + * @memberOf _ + * @category Array + * @param {Array} array The array to concatenate. + * @param {...*} [values] The values to concatenate. + * @returns {Array} Returns the new concatenated array. + * @example + * + * var array = [1]; + * var other = _.concat(array, 2, [3], [[4]]); + * + * console.log(other); + * // => [1, 2, 3, [4]] + * + * console.log(array); + * // => [1] + */ + var concat = rest(function(array, values) { + values = baseFlatten(values); + return arrayConcat(isArray(array) ? array : [Object(array)], values); + }); + /** * Creates an array of unique `array` values not included in the other * provided arrays using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero) @@ -6778,33 +6804,6 @@ return new LodashWrapper(this.value(), this.__chain__); } - /** - * Creates a new array joining a wrapped array with any additional arrays - * and/or values. - * - * @name concat - * @memberOf _ - * @category Chain - * @param {...*} [values] The values to concatenate. - * @returns {Array} Returns the new concatenated array. - * @example - * - * var array = [1]; - * var wrapped = _(array).concat(2, [3], [[4]]); - * - * console.log(wrapped.value()); - * // => [1, 2, 3, [4]] - * - * console.log(array); - * // => [1] - */ - var wrapperConcat = rest(function(values) { - values = baseFlatten(values); - return this.thru(function(array) { - return arrayConcat(isArray(array) ? array : [Object(array)], values); - }); - }); - /** * Gets the next value on a wrapped object following the * [iterator protocol](https://mdn.io/iteration_protocols#iterator). @@ -13441,6 +13440,7 @@ lodash.chain = chain; lodash.chunk = chunk; lodash.compact = compact; + lodash.concat = concat; lodash.conforms = conforms; lodash.conj = conj; lodash.constant = constant; @@ -13915,7 +13915,6 @@ // Add chaining functions to the `lodash` wrapper. lodash.prototype.chain = wrapperChain; lodash.prototype.commit = wrapperCommit; - lodash.prototype.concat = wrapperConcat; lodash.prototype.next = wrapperNext; lodash.prototype.plant = wrapperPlant; lodash.prototype.reverse = wrapperReverse; diff --git a/test/test.js b/test/test.js index 40f0a7fa4..f56d9c7c9 100644 --- a/test/test.js +++ b/test/test.js @@ -2492,6 +2492,51 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.concat'); + + (function() { + QUnit.test('should concat arrays and values', function(assert) { + assert.expect(2); + + var array = [1], + actual = _.concat(array, 2, [3], [[4]]); + + assert.deepEqual(actual, [1, 2, 3, [4]]); + assert.deepEqual(array, [1]); + }); + + QUnit.test('should treat sparse arrays as dense', function(assert) { + assert.expect(3); + + var expected = [], + actual = _.concat(Array(1), Array(1)); + + expected.push(undefined, undefined); + + assert.ok('0'in actual); + assert.ok('1' in actual); + assert.deepEqual(actual, expected); + }); + + QUnit.test('should return a new wrapped array', function(assert) { + assert.expect(2); + + if (!isNpm) { + var array = [1], + wrapped = _(array).concat([2, 3]), + actual = wrapped.value(); + + assert.deepEqual(array, [1]); + assert.deepEqual(actual, [1, 2, 3]); + } + else { + skipTest(assert, 2); + } + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.conforms'); (function() { @@ -21379,63 +21424,6 @@ /*--------------------------------------------------------------------------*/ - QUnit.module('lodash(...).concat'); - - (function() { - QUnit.test('should concat arrays and values', function(assert) { - assert.expect(2); - - if (!isNpm) { - var array = [1], - wrapped = _(array).concat(2, [3], [[4]]); - - assert.deepEqual(wrapped.value(), [1, 2, 3, [4]]); - assert.deepEqual(array, [1]); - } - else { - skipTest(assert, 2); - } - }); - - QUnit.test('should treat sparse arrays as dense', function(assert) { - assert.expect(3); - - if (!isNpm) { - var expected = [], - wrapped = _(Array(1)).concat(Array(1)), - actual = wrapped.value(); - - expected.push(undefined, undefined); - - assert.ok('0'in actual); - assert.ok('1' in actual); - assert.deepEqual(actual, expected); - } - else { - skipTest(assert, 3); - } - }); - - QUnit.test('should return a new wrapped array', function(assert) { - assert.expect(3); - - if (!isNpm) { - var array = [1], - wrapped = _(array).concat([2, 3]), - actual = wrapped.value(); - - assert.deepEqual(array, [1]); - assert.deepEqual(actual, [1, 2, 3]); - assert.notStrictEqual(actual, array); - } - else { - skipTest(assert, 3); - } - }); - }()); - - /*--------------------------------------------------------------------------*/ - QUnit.module('lodash(...).join'); (function() { @@ -22431,7 +22419,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(276); + assert.expect(277); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));