Generates a sequence of numbers starting at the
first
argument, progressing by thestep
argument, and stopping at thesecond
argument.
✅
It is based on the Range proposal (opens in a new tab) & Iterator.range (opens in a new tab).
Syntax
range(
start: number,
end: number,
options?: number | { step: number; inclusive: false }
): number[]
The step
value can be auto infered based on the start
& end
values.
The third
argument can be either a step
value or an options
object.
Usage
import { range } from '@opentf/std';
range(start, end);
Examples
range() // It throws error
range(0) // It throws error
range(NaN, 0) // It throws error
range(0, 0) //=> []
range(0, 0, { inclusive: true }) //=> [0]
range(0, 5) //=> [0, 1, 2, 3, 4]
range(0, 5, { inclusive: true }) //=> [0, 1, 2, 3, 4, 5]
range(3, 9, { step: 3 }) //=> [3, 6]
range(0, -5) //=> [0, -1, -2, -3, -4]
range(0, -4, { step: -1 }) //=> [0, -1, -2, -3]
range(-10, -5) //=> [-10, -9, -8, -7, -6]
range(10, 12, { step: 0.5 }) //=> [10, 10.5, 11, 11.5]