UNPKG

812 BJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.arrayChunk = arrayChunk;
7
8// Copyright 2017-2022 @polkadot/util authors & contributors
9// SPDX-License-Identifier: Apache-2.0
10
11/**
12 * @name arrayChunk
13 * @summary Split T[] into T[][] based on the defind size
14 * @description
15 * Returns a set ao arrays based on the chunksize
16 * @example
17 * <BR>
18 *
19 * ```javascript
20 * import { arrayChunk } from '@polkadot/util';
21 *
22 * arrayChunk([1, 2, 3, 4, 5]); // [[1, 2], [3, 4], [5]]
23 * ```
24 */
25function arrayChunk(array, chunkSize) {
26 const outputSize = Math.ceil(array.length / chunkSize);
27 const output = Array(outputSize);
28
29 for (let i = 0; i < outputSize; i++) {
30 const offset = i * chunkSize;
31 output[i] = array.slice(offset, offset + chunkSize);
32 }
33
34 return output;
35}
\No newline at end of file