1 var baseSlice = require('./_baseSlice'),
2 toInteger = require('./toInteger');
4 /* Built-in method references for those with the same name as other `lodash` methods. */
5 var nativeCeil = Math.ceil,
9 * Creates an array of elements split into groups the length of `size`.
10 * If `array` can't be split evenly, the final chunk will be the remaining
16 * @param {Array} array The array to process.
17 * @param {number} [size=0] The length of each chunk.
18 * @returns {Array} Returns the new array containing chunks.
21 * _.chunk(['a', 'b', 'c', 'd'], 2);
22 * // => [['a', 'b'], ['c', 'd']]
24 * _.chunk(['a', 'b', 'c', 'd'], 3);
25 * // => [['a', 'b', 'c'], ['d']]
27 function chunk(array, size) {
28 size = nativeMax(toInteger(size), 0);
30 var length = array ? array.length : 0;
31 if (!length || size < 1) {
36 result = Array(nativeCeil(length / size));
38 while (index < length) {
39 result[++resIndex] = baseSlice(array, index, (index += size));
44 module.exports = chunk;