1 | /**
|
2 | * @name arrayChunk
|
3 | * @summary Split T[] into T[][] based on the defind size
|
4 | * @description
|
5 | * Returns a set ao arrays based on the chunksize
|
6 | * @example
|
7 | * <BR>
|
8 | *
|
9 | * ```javascript
|
10 | * import { arrayChunk } from '@polkadot/util';
|
11 | *
|
12 | * arrayChunk([1, 2, 3, 4, 5]); // [[1, 2], [3, 4], [5]]
|
13 | * ```
|
14 | */
|
15 | export function arrayChunk(array, chunkSize) {
|
16 | const outputSize = Math.ceil(array.length / chunkSize);
|
17 | // noop for the single-split case
|
18 | if (outputSize === 1) {
|
19 | return [array];
|
20 | }
|
21 | const output = Array(outputSize);
|
22 | for (let i = 0; i < outputSize; i++) {
|
23 | const offset = i * chunkSize;
|
24 | output[i] = array.slice(offset, offset + chunkSize);
|
25 | }
|
26 | return output;
|
27 | } |
\ | No newline at end of file |