ix
Version:
The Interactive Extensions for JavaScript
68 lines (66 loc) • 3.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.groupBy = exports.GroupByAsyncIterable = exports.GroupedAsyncIterable = void 0;
const tslib_1 = require("tslib");
const asynciterablex_js_1 = require("../asynciterablex.js");
const identity_js_1 = require("../../util/identity.js");
const _grouping_js_1 = require("./_grouping.js");
const aborterror_js_1 = require("../../aborterror.js");
/** @ignore */
/** @ignore */
class GroupedAsyncIterable extends asynciterablex_js_1.AsyncIterableX {
constructor(key, source) {
super();
this.key = key;
this._source = source;
}
[Symbol.asyncIterator](signal) {
return tslib_1.__asyncGenerator(this, arguments, function* _a() {
(0, aborterror_js_1.throwIfAborted)(signal);
for (const item of this._source) {
yield yield tslib_1.__await(item);
}
});
}
}
exports.GroupedAsyncIterable = GroupedAsyncIterable;
/** @ignore */
class GroupByAsyncIterable extends asynciterablex_js_1.AsyncIterableX {
constructor(source, keySelector, elementSelector) {
super();
this._source = source;
this._keySelector = keySelector;
this._elementSelector = elementSelector;
}
[Symbol.asyncIterator](signal) {
return tslib_1.__asyncGenerator(this, arguments, function* _a() {
(0, aborterror_js_1.throwIfAborted)(signal);
const map = yield tslib_1.__await((0, _grouping_js_1.createGrouping)(this._source, this._keySelector, this._elementSelector, signal));
for (const [key, values] of map) {
yield yield tslib_1.__await(new GroupedAsyncIterable(key, values));
}
});
}
}
exports.GroupByAsyncIterable = GroupByAsyncIterable;
/**
* 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.
*/
function groupBy(keySelector, elementSelector = identity_js_1.identityAsync) {
return function groupByOperatorFunction(source) {
return new GroupByAsyncIterable(source, keySelector, elementSelector);
};
}
exports.groupBy = groupBy;
//# sourceMappingURL=groupby.js.map