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
* elements.
*
@@ -3133,7 +3133,7 @@
* @memberOf _
* @category Array
* @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.
* @example
*
@@ -3143,15 +3143,15 @@
* _.chunk(['a', 'b', 'c', 'd'], 3);
* // => [['a', 'b', 'c'], ['d']]
*/
function chunk(array, chunkSize) {
function chunk(array, size) {
var index = 0,
length = array ? array.length : 0,
resIndex = -1,
result = [];
chunkSize = nativeMax(+chunkSize || 1, 1);
size = typeof size == 'undefined' ? 1 : nativeMax(+size || 1, 1);
while (index < length) {
result[++resIndex] = slice(array, index, (index += chunkSize));
result[++resIndex] = slice(array, index, (index += size));
}
return result;
}