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)
function count
count(
start?: number,
step?: number,
): AsyncIterableIterator<number>

Makes an infinite async iterable of evenly spaced values starting with the start number.

import { count } from "./infinite.ts";
const iterable = count(5);
for await (const value of iterable) {
  console.log(value);
}

The above example will print the following and keep going forever:

5
6
7
8
9
(...)

You could adjust the interval by passing a second argument to count():

import { count } from "./infinite.ts";
const iterable = count(0, 3);
for await (const value of iterable) {
  console.log(value);
}

The above example will print the following and keep going forever:

0
3
6
9
12
(...)

As it's infinite, it's usually used with break to stop the iteration:

import { count } from "./infinite.ts";
for await (const value of count(0)) {
  if (value > 4) break;
  console.log(value);
}

Or with other async generators like takeWhile():

import { count } from "./infinite.ts";
import { takeWhile } from "./take.ts";
for await (const value of takeWhile(count(0), v => v <= 4)) {
  console.log(value);
}

The both examples above will print the following 4 lines:

0
1
2
3

Parameters

optional
start: number = 0

The first value in the sequence. Defaults to 0.

optional
step: number = 1

The difference between each value in the sequence. Defaults to 1.

Return Type

AsyncIterableIterator<number>

An async iterable of evenly spaced values.

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 { count } from "@hongminhee/aitertools";
or

Import directly with a jsr specifier

import { count } 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 { count } 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 { count } from "@hongminhee/aitertools";

Add Package

vlt install jsr:@hongminhee/aitertools

Import symbol

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

Add Package

npx jsr add @hongminhee/aitertools

Import symbol

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

Add Package

bunx jsr add @hongminhee/aitertools

Import symbol

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