Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 13x 10x 10x 10x 10x 20x 10x | /**
* @class
*
* Array helpers for splitting work into bounded batches.
*
* @example
* ```ts
* List.chunk([1, 2, 3, 4, 5], 2) // [[1, 2], [3, 4], [5]]
* ```
*
* @author Bayu Dwiyan Satria
* @version 1.3.0
* @since 1.3.0
*/
export class List {
/**
* Splits a list into consecutive batches of at most `size` elements.
*
* @remarks
* Fractional sizes are truncated. Non-finite sizes and sizes below one use a
* batch size of one, keeping invalid configuration from producing an unbounded batch.
*
* @typeParam T The element type.
* @param values The list to split. Not modified.
* @param size Maximum batch length. Invalid sizes use one.
* @returns The batches in input order, or an empty array for empty input.
*/
public static chunk<T>(values: readonly T[], size: number): T[][] {
const requested = Math.trunc(size)
const step = Number.isFinite(requested) && requested > 1 ? requested : 1
const runs: T[][] = []
for (let index = 0; index < values.length; index += step) {
runs.push(values.slice(index, index + step))
}
return runs
}
}
|