UNPKG

826 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { assert } from "../assert.js";
4/**
5 * @name arrayRange
6 * @summary Returns a range of numbers ith the size and the specified offset
7 * @description
8 * Returns a new array of numbers with the specific size. Optionally, when `startAt`, is provided, it generates the range to start at a specific value.
9 * @example
10 * <BR>
11 *
12 * ```javascript
13 * import { arrayRange } from '@polkadot/util';
14 *
15 * arrayRange(5); // [0, 1, 2, 3, 4]
16 * arrayRange(3, 5); // [5, 6, 7]
17 * ```
18 */
19
20export function arrayRange(size, startAt = 0) {
21 assert(size > 0, 'Expected non-zero, positive number as a range size');
22 const result = new Array(size);
23
24 for (let i = 0; i < size; i++) {
25 result[i] = i + startAt;
26 }
27
28 return result;
29}
\No newline at end of file