UNPKG

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