b4bbc7bef16f6eef0ead0cd749544a12b221f11c
[yaffs-website] / node_modules / grunt-legacy-log-utils / node_modules / lodash / chunk.js
1 var baseSlice = require('./_baseSlice'),
2     toInteger = require('./toInteger');
3
4 /* Built-in method references for those with the same name as other `lodash` methods. */
5 var nativeCeil = Math.ceil,
6     nativeMax = Math.max;
7
8 /**
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
11  * elements.
12  *
13  * @static
14  * @memberOf _
15  * @category Array
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.
19  * @example
20  *
21  * _.chunk(['a', 'b', 'c', 'd'], 2);
22  * // => [['a', 'b'], ['c', 'd']]
23  *
24  * _.chunk(['a', 'b', 'c', 'd'], 3);
25  * // => [['a', 'b', 'c'], ['d']]
26  */
27 function chunk(array, size) {
28   size = nativeMax(toInteger(size), 0);
29
30   var length = array ? array.length : 0;
31   if (!length || size < 1) {
32     return [];
33   }
34   var index = 0,
35       resIndex = -1,
36       result = Array(nativeCeil(length / size));
37
38   while (index < length) {
39     result[++resIndex] = baseSlice(array, index, (index += size));
40   }
41   return result;
42 }
43
44 module.exports = chunk;