Skip to content

Function: range()

range(start, count, step?): Stream<number>

Defined in: streams/range.ts:18

Creates a stream that emits a sequence of numbers, starting from start, incrementing by step, and emitting a total of count values.

This operator is a powerful way to generate a numerical sequence in a reactive context. It's similar to a standard for loop but produces values as a stream. It's built upon the loop operator for its underlying logic.

Parameters

start

number

The first number to emit in the sequence.

count

number

The total number of values to emit. Must be a non-negative number.

step?

number = 1

The amount to increment or decrement the value in each step.

Returns

Stream<number>

A stream that emits a sequence of numbers.

Examples

From range.spec.ts:4

typescript
const start = 1;
const count = 5;
const expectedValues = [1, 2, 3, 4, 5];
const emittedValues: number[] = [];
const rangeStream = range(start, count);
await new Promise<void>((resolve, reject) => {
  rangeStream.subscribe({
    next: (value) => emittedValues.push(value),
    complete: () => {
      try {
        expect(emittedValues).toEqual(expectedValues);
        resolve(); // Resolve the promise on successful completion
      } catch (e) {
        reject(e); // Reject if the expectation fails
      }
    },
    error: (err) => reject(err), // Reject the promise if an error occurs
  });
});

Released under the MIT License.