mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 07:17:50 +00:00
38 lines
920 B
JavaScript
38 lines
920 B
JavaScript
import slice from './slice.js'
|
|
|
|
/**
|
|
* 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.
|
|
*
|
|
* @since 3.0.0
|
|
* @category Array
|
|
* @param {Array} array The array to process.
|
|
* @param {number} [size=1] The length of each chunk
|
|
* @returns {Array} Returns the new array of 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 = Math.max(size, 0)
|
|
const length = array == null ? 0 : array.length
|
|
if (!length || size < 1) {
|
|
return []
|
|
}
|
|
let index = 0
|
|
let resIndex = 0
|
|
const result = new Array(Math.ceil(length / size))
|
|
|
|
while (index < length) {
|
|
result[resIndex++] = slice(array, index, (index += size))
|
|
}
|
|
return result
|
|
}
|
|
|
|
export default chunk
|