🔢 Generates a sequence of numbers starting from
startup to (but not including, by default)end.
✅
Based on the TC39 Range proposal (opens in a new tab).
Syntax
import { range } from '@opentf/std';
range(
startOrEnd: number,
end?: number,
options?: number | { step: number; inclusive: boolean }
): number[]- If only one argument is provided, it is treated as
endandstartdefaults to0. - The
stepvalue can be auto-inferred based on thestartandendvalues. - The third argument can be either a numeric
stepvalue or anoptionsobject.
Parameters
startOrEnd: The starting value (ifendis provided) or the end value (ifendis omitted).end: The end value of the sequence.options:- As a
number: The step value. - As an
object:{ step?: number; inclusive?: boolean }.
- As a
Returns
An array containing the sequence of numbers.
Examples
range(0, 5) //=> [0, 1, 2, 3, 4]
range(5) //=> [0, 1, 2, 3, 4]
range(-4) //=> [0, -1, -2, -3]
range(0, 0) //=> []
range(0, 0, { inclusive: true }) //=> [0]
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]