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 toMap
toMap<
K,
V,
>
(source: AsyncIterable<[K, V]>): Promise<Map<K, V>>

Creates a map from an async iterable of key-value pairs. Each pair is represented as an array of two elements.

import { toMap } from "./collections.ts";

async function* gen(): AsyncIterableIterator<[string, number]> {
  yield ["foo", 1]; yield ["bar", 2]; yield ["baz", 3]; yield ["qux", 4];
}
const map = await toMap<string, number>(gen());

The map variable will be a map like Map { "foo" => 1, "bar" => 2, "baz" => 3, "qux" => 4 }.

Duplicate keys are removed except for the last occurrence of each key. E.g.:

import { fromIterable, toMap } from "./collections.ts";

const iterable = fromIterable<[string, number]>([
  ["foo", 1], ["bar", 2], ["baz", 3], ["qux", 4],
  ["foo", 5], ["bar", 6],
]);
const map = await toMap<string, number>(iterable);

The map variable will be a map like Map { "foo" => 5, "bar" => 6, "baz" => 3, "qux" => 4 }.

Note that the iterable source is assumed to be finite; otherwise, it will never return. The following example will never return:

import { toMap } from "./collections.ts";
import { count } from "./infinite.ts";
import { map } from "./map.ts";

await toMap<number, number>(
  map((v: number) => [v, v] as [number, number], count(0))
);

Type Parameters

The type of the keys in the source and the returned map.

The type of the values in the source and the returned map.

Parameters

source: AsyncIterable<[K, V]>

An async iterable of key-value pairs to create a map from. Each pair is represented as an array of two elements. It must be finite.

Return Type

A map that contains the key-value pairs from the source iterable. Duplicate keys are removed except for the last one.

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

Import directly with a jsr specifier

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

Add Package

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

Import symbol

import { toMap } from "@hongminhee/aitertools/collections";

Add Package

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

Import symbol

import { toMap } from "@hongminhee/aitertools/collections";

Add Package

vlt install jsr:@hongminhee/aitertools

Import symbol

import { toMap } from "@hongminhee/aitertools/collections";

Add Package

npx jsr add @hongminhee/aitertools

Import symbol

import { toMap } from "@hongminhee/aitertools/collections";

Add Package

bunx jsr add @hongminhee/aitertools

Import symbol

import { toMap } from "@hongminhee/aitertools/collections";