mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 18:07:49 +00:00
Bump to v4.0.0.
This commit is contained in:
44
chunk.js
Normal file
44
chunk.js
Normal file
@@ -0,0 +1,44 @@
|
||||
var baseSlice = require('./internal/baseSlice'),
|
||||
toInteger = require('./toInteger');
|
||||
|
||||
/* Built-in method references for those with the same name as other `lodash` methods. */
|
||||
var nativeCeil = Math.ceil,
|
||||
nativeMax = Math.max;
|
||||
|
||||
/**
|
||||
* Creates an array of elements split into groups the length of `size`.
|
||||
* If `array` can't be split evenly, the final chunk will be the remaining
|
||||
* elements.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to process.
|
||||
* @param {number} [size=0] The length of each chunk.
|
||||
* @returns {Array} Returns the new array containing chunks.
|
||||
* @example
|
||||
*
|
||||
* _.chunk(['a', 'b', 'c', 'd'], 2);
|
||||
* // => [['a', 'b'], ['c', 'd']]
|
||||
*
|
||||
* _.chunk(['a', 'b', 'c', 'd'], 3);
|
||||
* // => [['a', 'b', 'c'], ['d']]
|
||||
*/
|
||||
function chunk(array, size) {
|
||||
size = nativeMax(toInteger(size), 0);
|
||||
|
||||
var length = array ? array.length : 0;
|
||||
if (!length || size < 1) {
|
||||
return [];
|
||||
}
|
||||
var index = 0,
|
||||
resIndex = -1,
|
||||
result = Array(nativeCeil(length / size));
|
||||
|
||||
while (index < length) {
|
||||
result[++resIndex] = baseSlice(array, index, (index += size));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = chunk;
|
||||
Reference in New Issue
Block a user