🔢 Generates a sequence of numbers starting from start up to (but not including, by default) end.

Info

Based on the TC39 Range proposal.

Syntax

TypeScript
import { range } from '@opentf/std';

range(
  startOrEnd: number,
  end?: number,
  options?: number | { step: number; inclusive: boolean }
): number[]
Info
  • If only one argument is provided, it is treated as end and start defaults to 0.

  • The step value can be auto-inferred based on the start and end values.

  • The third argument can be either a numeric step value or an options object.

Parameters

  • startOrEnd: The starting value (if end is provided) or the end value (if end is omitted).

  • end: The end value of the sequence.

  • options:

    • As a number: The step value.

    • As an object: { step?: number; inclusive?: boolean }.

Returns

An array containing the sequence of numbers.

Examples

TypeScript
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]
Last updated on
Edit this page