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 dropWhile
dropWhile<T>(
source: Iterable<T> | AsyncIterable<T>,
predicate: (
value: T,
index: number,
) => boolean | Promise<boolean>
,
): AsyncIterableIterator<T>

Drops elements from the beginning of an async iterable as long as a specified condition is met, and yields the remaining elements.

import { dropWhile } from "./drop.ts";

async function* gen() { yield "foo"; yield "bar"; yield "baz"; yield "qux" }
const iterable = dropWhile(gen(), v => v !== "baz");
for await (const value of iterable) {
  console.log(value);
}

The above example will print the following 2 lines:

baz
qux

An async predicate function also works. The following example will print the same 2 lines as the previous example:

import { dropWhile } from "./drop.ts";

async function* gen() { yield "foo"; yield "bar"; yield "baz"; yield "qux" }
const iterable = dropWhile(gen(), v => Promise.resolve(v !== "baz"));
for await (const value of iterable) {
  console.log(value);
}

A predicate function can take an index as well as the value.

import { dropWhile } from "./drop.ts";

async function* gen() { yield "foo"; yield "bar"; yield "baz"; yield "qux" }
const iterable = dropWhile(gen(), (_, i) => i % 2 === 0);
for await (const value of iterable) {
  console.log(value);
}

The above example will print the following 3 lines:

bar
baz
qux

Type Parameters

The type of the elements in the source and the returned async iterable.

Parameters

source: Iterable<T> | AsyncIterable<T>

The async iterable to drop elements from. It can be either finite or infinite.

predicate: (
value: T,
index: number,
) => boolean | Promise<boolean>

A predicate function to test each source element for a condition; the second parameter of the function represents the index of the source element. It can be either a synchronous or an asynchronous function.

Return Type

AsyncIterableIterator<T>

An async iterable that contains elements from the source iterable that occur that and after the element at which the predicate first fails.

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

Import directly with a jsr specifier

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

Add Package

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

Import symbol

import { dropWhile } from "@hongminhee/aitertools/drop";

Add Package

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

Import symbol

import { dropWhile } from "@hongminhee/aitertools/drop";

Add Package

vlt install jsr:@hongminhee/aitertools

Import symbol

import { dropWhile } from "@hongminhee/aitertools/drop";

Add Package

npx jsr add @hongminhee/aitertools

Import symbol

import { dropWhile } from "@hongminhee/aitertools/drop";

Add Package

bunx jsr add @hongminhee/aitertools

Import symbol

import { dropWhile } from "@hongminhee/aitertools/drop";