UNPKG

ix

Version:

The Interactive Extensions for JavaScript

62 lines (60 loc) 2.63 kB
import { __asyncGenerator, __await } from "tslib"; import { AsyncIterableX } from '../asynciterablex.mjs'; import { identityAsync } from '../../util/identity.mjs'; import { createGrouping } from './_grouping.mjs'; import { throwIfAborted } from '../../aborterror.mjs'; /** @ignore */ /** @ignore */ export class GroupedAsyncIterable extends AsyncIterableX { constructor(key, source) { super(); this.key = key; this._source = source; } [Symbol.asyncIterator](signal) { return __asyncGenerator(this, arguments, function* _a() { throwIfAborted(signal); for (const item of this._source) { yield yield __await(item); } }); } } /** @ignore */ export class GroupByAsyncIterable extends AsyncIterableX { constructor(source, keySelector, elementSelector) { super(); this._source = source; this._keySelector = keySelector; this._elementSelector = elementSelector; } [Symbol.asyncIterator](signal) { return __asyncGenerator(this, arguments, function* _a() { throwIfAborted(signal); const map = yield __await(createGrouping(this._source, this._keySelector, this._elementSelector, signal)); for (const [key, values] of map) { yield yield __await(new GroupedAsyncIterable(key, values)); } }); } } /** * Groups the elements of an async-iterable sequence and selects the resulting elements by using a specified function. * * @template TSource The type of the elements in the source sequence. * @template TKey The type of the grouping key computed for each element in the source sequence. * @template TValue The type of the elements within the groups computed for each element in the source sequence. * @param {((value: TSource, signal?: AbortSignal) => TKey | Promise<TKey>)} keySelector A function to extract the key for each element. * @param {(( * value: TSource, * signal?: AbortSignal * ) => TValue | Promise<TValue>)} [elementSelector=identityAsync] A function to map each source element to an element in an async-enumerable group. * @returns {OperatorAsyncFunction<TSource, TResult>} A sequence of async-iterable groups, each of which corresponds to a unique key value, * containing all elements that share that same key value. */ export function groupBy(keySelector, elementSelector = identityAsync) { return function groupByOperatorFunction(source) { return new GroupByAsyncIterable(source, keySelector, elementSelector); }; } //# sourceMappingURL=groupby.mjs.map