Avoid type coercion of size in _.chunk unless needed.

This commit is contained in:
John-David Dalton
2014-07-29 09:37:25 -07:00
parent 6fdaf905c4
commit e48b314ad0

View File

@@ -3125,7 +3125,7 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/** /**
* Creates an array of elements split into groups the length of `chunkSize`. * Creates an array of elements split into groups the length of `size`.
* If `collection` can't be split evenly, the final chunk will be the remaining * If `collection` can't be split evenly, the final chunk will be the remaining
* elements. * elements.
* *
@@ -3133,7 +3133,7 @@
* @memberOf _ * @memberOf _
* @category Array * @category Array
* @param {Array} array The array to process. * @param {Array} array The array to process.
* @param {numer} [chunkSize=1] The size of each chunk. * @param {numer} [size=1] The length of each chunk.
* @returns {Array} Returns the new array containing chunks. * @returns {Array} Returns the new array containing chunks.
* @example * @example
* *
@@ -3143,15 +3143,15 @@
* _.chunk(['a', 'b', 'c', 'd'], 3); * _.chunk(['a', 'b', 'c', 'd'], 3);
* // => [['a', 'b', 'c'], ['d']] * // => [['a', 'b', 'c'], ['d']]
*/ */
function chunk(array, chunkSize) { function chunk(array, size) {
var index = 0, var index = 0,
length = array ? array.length : 0, length = array ? array.length : 0,
resIndex = -1, resIndex = -1,
result = []; result = [];
chunkSize = nativeMax(+chunkSize || 1, 1); size = typeof size == 'undefined' ? 1 : nativeMax(+size || 1, 1);
while (index < length) { while (index < length) {
result[++resIndex] = slice(array, index, (index += chunkSize)); result[++resIndex] = slice(array, index, (index += size));
} }
return result; return result;
} }