UNPKG

ix

Version:

The Interactive Extensions for JavaScript

47 lines (45 loc) 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toMap = void 0; const tslib_1 = require("tslib"); const identity_js_1 = require("../util/identity.js"); const withabort_js_1 = require("./operators/withabort.js"); const aborterror_js_1 = require("../aborterror.js"); /** * 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>>)} */ function toMap(source, options) { var _a, e_1, _b, _c; return tslib_1.__awaiter(this, void 0, void 0, function* () { const { ['signal']: signal, ['elementSelector']: elementSelector = identity_js_1.identityAsync, ['keySelector']: keySelector = identity_js_1.identityAsync, } = options || {}; (0, aborterror_js_1.throwIfAborted)(signal); const map = new Map(); try { for (var _d = true, _e = tslib_1.__asyncValues((0, withabort_js_1.wrapWithAbort)(source, signal)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const item = _c; const value = yield elementSelector(item, signal); const key = yield keySelector(item, signal); map.set(key, value); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_1) throw e_1.error; } } return map; }); } exports.toMap = toMap; //# sourceMappingURL=tomap.js.map