Skip to main content
Module

x/aitertools/unique.ts>unique

Well-tested utility functions dealing with async iterables
Go to Latest
function unique
import { unique } from "https://deno.land/x/aitertools@0.4.0/unique.ts";

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

T

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.

Returns

AsyncIterableIterator<T>