UNPKG

2.36 kBSource Map (JSON)View Raw
1{"version":3,"sources":["iterable/tomap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAwB5C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,KAAK,CACnB,MAAyB,EACzB,OAAwC;IAExC,MAAM,EACJ,CAAC,iBAAiB,CAAC,EAAE,eAAe,GAAG,QAAe,EACtD,CAAC,aAAa,CAAC,EAAE,WAAW,GAAG,QAAe,GAC/C,GAAG,OAAO,IAAI,EAAE,CAAC;IAClB,MAAM,GAAG,GAAG,IAAI,GAAG,EAA4B,CAAC;IAChD,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE;QACzB,MAAM,KAAK,GAAG,eAAgB,CAAC,IAAI,CAAC,CAAC;QACrC,MAAM,GAAG,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;QAC9B,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;KACrB;IACD,OAAO,GAAG,CAAC;AACb,CAAC","file":"tomap.js","sourcesContent":["import { identity } from '../util/identity';\n\n/**\n * The options for the toMap method which include an optional element selector and abort signal for cancellation.\n *\n * @interface ToMapOptions\n * @template TSource\n * @template TElement\n */\nexport interface ToMapOptions<TSource, TElement> {\n /**\n * The selector to get the key for the map.\n *\n * @memberof ToMapOptions\n */\n keySelector: (item: TSource) => TElement;\n /**\n * The selector used to get the element for the Map.\n *\n * @memberof ToMapOptions\n */\n elementSelector?: (item: TSource) => TElement;\n}\n\n/**\n * Converts an async-iterable to a map with a key selector and options for an element selector and cancellation.\n *\n * @export\n * @template TSource The type of elements in the source collection.\n * @template TKey The type of key used for the map.\n * @template TElement The type of element to use for the map.\n * @param {AsyncIterable<TSource>} source The source collection to turn into a map.\n * @param {ToMapOptions<TSource, TElement>} options The options for getting the key and element for the map.\n * @returns {(Map<TKey, TElement | TSource>)} A map containing the key and elements from the selector functions.\n */\nexport function toMap<TSource, TKey, TElement = TSource>(\n source: Iterable<TSource>,\n options: ToMapOptions<TSource, TElement>\n): Map<TKey, TElement | TSource> {\n const {\n ['elementSelector']: elementSelector = identity as any,\n ['keySelector']: keySelector = identity as any,\n } = options || {};\n const map = new Map<TKey, TElement | TSource>();\n for (const item of source) {\n const value = elementSelector!(item);\n const key = keySelector(item);\n map.set(key, value);\n }\n return map;\n}\n"]}
\No newline at end of file