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

Eliminates all elements from the iterable source that do not satisfy the predicate function.

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

async function* gen() { yield "foo"; yield "bar"; yield "baz"; yield "qux" }
const iterable = filter((v: string) => !!v.match(/^b/), gen());
for await (const value of iterable) console.log(value);

The above example will print the following 2 lines:

bar
baz

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

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

const iterable = filter(
  (v: string, i: number) => !v.match(/^b/) && i % 2 === 0,
  ["foo", "bar", "baz", "qux", "quux"]
);
for await (const value of iterable) console.log(value);

The above example will print the following 2 lines:

foo
quux

Type Parameters

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

Parameters

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

A predicate function that takes the value and the index of the element and returns a boolean, which indicates whether the element should be included in the resulting iterable. It can be either synchronous or asynchronous.

source: Iterable<T> | AsyncIterable<T>

An iterable to filter elements from. It can be either finite or infinite.

Return Type

AsyncIterableIterator<T>

An async iterable that contains elements from the source iterable that satisfy the predicate function.

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

Import directly with a jsr specifier

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

Add Package

vlt install jsr:@hongminhee/aitertools

Import symbol

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

Add Package

npx jsr add @hongminhee/aitertools

Import symbol

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

Add Package

bunx jsr add @hongminhee/aitertools

Import symbol

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