1 var baseSlice = require('../internal/baseSlice'),
2 isIterateeCall = require('../internal/isIterateeCall');
4 /** Native method references. */
7 /* Native method references for those with the same name as other `lodash` methods. */
8 var nativeMax = Math.max;
11 * Creates an array of elements split into groups the length of `size`.
12 * If `collection` can't be split evenly, the final chunk will be the remaining
18 * @param {Array} array The array to process.
19 * @param {number} [size=1] The length of each chunk.
20 * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
21 * @returns {Array} Returns the new array containing chunks.
24 * _.chunk(['a', 'b', 'c', 'd'], 2);
25 * // => [['a', 'b'], ['c', 'd']]
27 * _.chunk(['a', 'b', 'c', 'd'], 3);
28 * // => [['a', 'b', 'c'], ['d']]
30 function chunk(array, size, guard) {
31 if (guard ? isIterateeCall(array, size, guard) : size == null) {
34 size = nativeMax(+size || 1, 1);
37 length = array ? array.length : 0,
39 result = Array(ceil(length / size));
41 while (index < length) {
42 result[++resIndex] = baseSlice(array, index, (index += size));
47 module.exports = chunk;