1 var baseSlice = require('./_baseSlice'),
2 isIterateeCall = require('./_isIterateeCall'),
3 toInteger = require('./toInteger');
5 /* Built-in method references for those with the same name as other `lodash` methods. */
6 var nativeCeil = Math.ceil,
10 * Creates an array of elements split into groups the length of `size`.
11 * If `array` 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 an iteratee for methods like `_.map`.
21 * @returns {Array} Returns the new array of 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 === undefined)) {
34 size = nativeMax(toInteger(size), 0);
36 var length = array == null ? 0 : array.length;
37 if (!length || size < 1) {
42 result = Array(nativeCeil(length / size));
44 while (index < length) {
45 result[resIndex++] = baseSlice(array, index, (index += size));
50 module.exports = chunk;