-
Notifications
You must be signed in to change notification settings - Fork 74
/
tomap.ts
62 lines (60 loc) · 2.05 KB
/
tomap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { identityAsync } from '../util/identity.js';
import { wrapWithAbort } from './operators/withabort.js';
import { throwIfAborted } from '../aborterror.js';
/**
* The options for the toMap method which include an optional element selector and abort signal for cancellation.
*
* @interface ToMapOptions
* @template TSource
* @template TElement
* @ignore
*/
export interface ToMapOptions<TSource, TKey, TElement> {
/**
* The selector to get the key for the map.
*
* @memberof ToMapOptions
*/
keySelector: (item: TSource, signal?: AbortSignal) => TKey | Promise<TKey>;
/**
* The selector used to get the element for the Map.
*
* @memberof ToMapOptions
*/
elementSelector?: (item: TSource, signal?: AbortSignal) => TElement | Promise<TElement>;
/**
* An optional abort signal to cancel the operation at any time.
*
* @type {AbortSignal}
* @memberof ToMapOptions
*/
signal?: AbortSignal;
}
/**
* Converts an async-iterable to a map with a key selector and options for an element selector and cancellation.
*
* @template TSource The type of elements in the source collection.
* @template TKey The type of key used for the map.
* @template TElement The type of element to use for the map.
* @param {AsyncIterable<TSource>} source The source collection to turn into a map.
* @param {ToMapOptions<TSource, TElement>} [options]
* @returns {(Promise<Map<TKey, TElement | TSource>>)}
*/
export async function toMap<TSource, TKey, TElement = TSource>(
source: AsyncIterable<TSource>,
options: ToMapOptions<TSource, TKey, TElement>
): Promise<Map<TKey, TElement | TSource>> {
const {
['signal']: signal,
['elementSelector']: elementSelector = identityAsync as any,
['keySelector']: keySelector = identityAsync as any,
} = options || {};
throwIfAborted(signal);
const map = new Map<TKey, TElement | TSource>();
for await (const item of wrapWithAbort(source, signal)) {
const value = await elementSelector!(item, signal);
const key = await keySelector(item, signal);
map.set(key, value);
}
return map;
}