Skip to main content
Home

Built and signed on GitHub Actions

Well-tested utility functions dealing with async iterables

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
88%
Published
2 years ago (0.6.0)
class Range
implements Iterable<T>, AsyncIterable<T>

An immutable sequence of numbers. It implements both Iterable and AsyncIterable.

It is similar to Python's range() function.

Constructors

new
Range(
start: T,
stop: T,
step: T,
)

Constructs a new Range object.

Type Parameters

T extends number | bigint

The type of the elements in the range. It must be either number or bigint.

Properties

readonly
length: number

The length of the range. Note that it guarantees to return the same value as Array.from(range).length.

import { range } from "./range.ts";

console.log(range(10, -10, -3.5).length);

The above example will print 6.

readonly
start: T

The start of the range. It must be a finite number.

readonly
step: T

The step of the range. It must be a finite number, and cannot be zero.

readonly
stop: T

The stop of the range. It must be a finite number.

Methods

Iterates over the elements of the range, in an asynchronous manner.

import { range } from "./range.ts";

for await (const value of range(4)) console.log(value);

The above example will print the following 4 lines:

0
1
2
3

Iterates over the elements of the range.

import { range } from "./range.ts";

for (const value of range(4n)) console.log(value);

The above example will print the following 4 lines:

0
1
2
3
at(index: number): T | undefined

Returns the element at the specified index in the range. Note that it guarantees to return the same value as Array.from(range).at(index).

import { range } from "./range.ts";

const r = range(10, -10, -3.5);
console.log(r.at(3), r.at(-1));

The above example will print the following 2 lines:

-0.5
-7.5

Represents the range as a string.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@hongminhee/aitertools

Import symbol

import { Range } from "@hongminhee/aitertools";
or

Import directly with a jsr specifier

import { Range } from "jsr:@hongminhee/aitertools";

Add Package

pnpm i jsr:@hongminhee/aitertools
or (using pnpm 10.8 or older)
pnpm dlx jsr add @hongminhee/aitertools

Import symbol

import { Range } from "@hongminhee/aitertools";

Add Package

yarn add jsr:@hongminhee/aitertools
or (using Yarn 4.8 or older)
yarn dlx jsr add @hongminhee/aitertools

Import symbol

import { Range } from "@hongminhee/aitertools";

Add Package

vlt install jsr:@hongminhee/aitertools

Import symbol

import { Range } from "@hongminhee/aitertools";

Add Package

npx jsr add @hongminhee/aitertools

Import symbol

import { Range } from "@hongminhee/aitertools";

Add Package

bunx jsr add @hongminhee/aitertools

Import symbol

import { Range } from "@hongminhee/aitertools";