UNPKG

1.02 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.arrayRange = arrayRange;
7
8var _assert = require("../assert");
9
10// Copyright 2017-2022 @polkadot/util authors & contributors
11// SPDX-License-Identifier: Apache-2.0
12
13/**
14 * @name arrayRange
15 * @summary Returns a range of numbers ith the size and the specified offset
16 * @description
17 * 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.
18 * @example
19 * <BR>
20 *
21 * ```javascript
22 * import { arrayRange } from '@polkadot/util';
23 *
24 * arrayRange(5); // [0, 1, 2, 3, 4]
25 * arrayRange(3, 5); // [5, 6, 7]
26 * ```
27 */
28function arrayRange(size) {
29 let startAt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
30 (0, _assert.assert)(size > 0, 'Expected non-zero, positive number as a range size');
31 const result = new Array(size);
32
33 for (let i = 0; i < size; i++) {
34 result[i] = i + startAt;
35 }
36
37 return result;
38}
\No newline at end of file