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 unique
unique<T>(
source: Iterable<T> | AsyncIterable<T>,
keySelector?: (element: T) => unknown,
): AsyncIterableIterator<T>

Eliminate duplicates in an async iterable source.

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

async function* gen() { yield "foo"; yield "bar"; yield "bar"; yield "foo" }
const iterable = unique(gen());
for await (const value of iterable) console.log(value);

The above example will print the following 2 lines:

foo
bar

For complex elements, the keySelector function can be used to specify how to compare the elements. Among duplicate elements, the one with the first occurrence of the key is kept. E.g.:

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

async function* gen() {
  yield { id: 1, name: "foo" };
  yield { id: 2, name: "bar" };
  yield { id: 3, name: "bar" };
  yield { id: 4, name: "foo" };
}

console.log("Unique by ID:");
const uniqueIds = unique(gen(), v => v.id);
for await (const value of uniqueIds) console.log(value);

console.log("Unique by name:");
const uniqueNames = unique(gen(), v => v.name);
for await (const value of uniqueNames) console.log(value);

The above example will print the following:

Unique by ID:
{ id: 1, name: "foo" }
{ id: 2, name: "bar" }
{ id: 3, name: "bar" }
{ id: 4, name: "foo" }
Unique by name:
{ id: 1, name: "foo" }
{ id: 2, name: "bar" }

Type Parameters

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

Parameters

source: Iterable<T> | AsyncIterable<T>

The async iterable to eliminate duplicates from.

optional
keySelector: (element: T) => unknown

An optional function to select the key for each element. Among duplicate elements, the one with the first occurrence of the key is kept. If omitted, the identity function is used.

Return Type

AsyncIterableIterator<T>

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

Import directly with a jsr specifier

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

Add Package

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

Import symbol

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

Add Package

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

Import symbol

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

Add Package

vlt install jsr:@hongminhee/aitertools

Import symbol

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

Add Package

npx jsr add @hongminhee/aitertools

Import symbol

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

Add Package

bunx jsr add @hongminhee/aitertools

Import symbol

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