UNPKG

1.18 kBJavaScriptView Raw
1import { identityAsync } from '../util/identity';
2import { wrapWithAbort } from './operators/withabort';
3import { throwIfAborted } from '../aborterror';
4/**
5 * Converts an async-iterable to a map with a key selector and options for an element selector and cancellation.
6 *
7 * @export
8 * @template TSource The type of elements in the source collection.
9 * @template TKey The type of key used for the map.
10 * @template TElement The type of element to use for the map.
11 * @param {AsyncIterable<TSource>} source The source collection to turn into a map.
12 * @param {ToMapOptions<TSource, TElement>} [options]
13 * @returns {(Promise<Map<TKey, TElement | TSource>>)}
14 */
15export async function toMap(source, options) {
16 const { ['signal']: signal, ['elementSelector']: elementSelector = identityAsync, ['keySelector']: keySelector = identityAsync, } = options || {};
17 throwIfAborted(signal);
18 const map = new Map();
19 for await (const item of wrapWithAbort(source, signal)) {
20 const value = await elementSelector(item, signal);
21 const key = await keySelector(item, signal);
22 map.set(key, value);
23 }
24 return map;
25}
26
27//# sourceMappingURL=tomap.mjs.map