mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 11:27:50 +00:00
Make _.chunk default size to 1 instead of 0.
This commit is contained in:
12
lodash.js
12
lodash.js
@@ -5769,7 +5769,8 @@
|
|||||||
* @since 3.0.0
|
* @since 3.0.0
|
||||||
* @category Array
|
* @category Array
|
||||||
* @param {Array} array The array to process.
|
* @param {Array} array The array to process.
|
||||||
* @param {number} [size=0] The length of each chunk.
|
* @param {number} [size=1] The length of each chunk
|
||||||
|
* @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
|
||||||
* @returns {Array} Returns the new array containing chunks.
|
* @returns {Array} Returns the new array containing chunks.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
@@ -5779,9 +5780,12 @@
|
|||||||
* _.chunk(['a', 'b', 'c', 'd'], 3);
|
* _.chunk(['a', 'b', 'c', 'd'], 3);
|
||||||
* // => [['a', 'b', 'c'], ['d']]
|
* // => [['a', 'b', 'c'], ['d']]
|
||||||
*/
|
*/
|
||||||
function chunk(array, size) {
|
function chunk(array, size, guard) {
|
||||||
size = nativeMax(toInteger(size), 0);
|
if ((guard ? isIterateeCall(array, size, guard) : size === undefined)) {
|
||||||
|
size = 1;
|
||||||
|
} else {
|
||||||
|
size = nativeMax(toInteger(size), 0);
|
||||||
|
}
|
||||||
var length = array ? array.length : 0;
|
var length = array ? array.length : 0;
|
||||||
if (!length || size < 1) {
|
if (!length || size < 1) {
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
27
test/test.js
27
test/test.js
@@ -2385,14 +2385,28 @@
|
|||||||
assert.deepEqual(actual, [[0, 1, 2, 3], [4, 5]]);
|
assert.deepEqual(actual, [[0, 1, 2, 3], [4, 5]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should treat falsey `size` values, except `undefined`, as `0`', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var expected = lodashStable.map(falsey, function(value) {
|
||||||
|
return value === undefined ? [[0], [1], [2], [3], [4], [5]] : [];
|
||||||
|
});
|
||||||
|
|
||||||
|
var actual = lodashStable.map(falsey, function(size, index) {
|
||||||
|
return index ? _.chunk(array, size) : _.chunk(array);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
QUnit.test('should ensure the minimum `size` is `0`', function(assert) {
|
QUnit.test('should ensure the minimum `size` is `0`', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
var values = falsey.concat(-1, -Infinity),
|
var values = lodashStable.reject(falsey, lodashStable.isUndefined).concat(-1, -Infinity),
|
||||||
expected = lodashStable.map(values, alwaysEmptyArray);
|
expected = lodashStable.map(values, alwaysEmptyArray);
|
||||||
|
|
||||||
var actual = lodashStable.map(values, function(value, index) {
|
var actual = lodashStable.map(values, function(n) {
|
||||||
return index ? _.chunk(array, value) : _.chunk(array);
|
return _.chunk(array, n);
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.deepEqual(actual, expected);
|
assert.deepEqual(actual, expected);
|
||||||
@@ -2403,6 +2417,13 @@
|
|||||||
|
|
||||||
assert.deepEqual(_.chunk(array, array.length / 4), [[0], [1], [2], [3], [4], [5]]);
|
assert.deepEqual(_.chunk(array, array.length / 4), [[0], [1], [2], [3], [4], [5]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should work as an iteratee for methods like `_.map`', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var actual = lodashStable.map([[1, 2], [3, 4]], _.chunk);
|
||||||
|
assert.deepEqual(actual, [[[1], [2]], [[3], [4]]]);
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user