UNPKG

@reactivex/ix-es5-esm

Version:

The Interactive Extensions for JavaScript

121 lines (119 loc) 5 kB
import { __extends, __generator, __read, __values } from "tslib"; import { IterableX } from '../iterablex.js'; import { identity } from '../../util/identity.js'; import { createGrouping } from './_grouping.js'; /** @ignore */ /** @ignore */ var GroupedIterable = /** @class */ (function (_super) { __extends(GroupedIterable, _super); function GroupedIterable(key, source) { var _this = _super.call(this) || this; _this.key = key; _this._source = source; return _this; } GroupedIterable.prototype[Symbol.iterator] = function () { var _a, _b, item, e_1_1; var e_1, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _d.trys.push([0, 5, 6, 7]); _a = __values(this._source), _b = _a.next(); _d.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 4]; item = _b.value; return [4 /*yield*/, item]; case 2: _d.sent(); _d.label = 3; case 3: _b = _a.next(); return [3 /*break*/, 1]; case 4: return [3 /*break*/, 7]; case 5: e_1_1 = _d.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 7]; case 6: try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 7: return [2 /*return*/]; } }); }; return GroupedIterable; }(IterableX)); export { GroupedIterable }; /** @ignore */ var GroupByIterable = /** @class */ (function (_super) { __extends(GroupByIterable, _super); function GroupByIterable(source, keySelector, elementSelector) { var _this = _super.call(this) || this; _this._source = source; _this._keySelector = keySelector; _this._elementSelector = elementSelector; return _this; } GroupByIterable.prototype[Symbol.iterator] = function () { var map, map_1, map_1_1, _a, key, values, e_2_1; var e_2, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: map = createGrouping(this._source, this._keySelector, this._elementSelector); _c.label = 1; case 1: _c.trys.push([1, 6, 7, 8]); map_1 = __values(map), map_1_1 = map_1.next(); _c.label = 2; case 2: if (!!map_1_1.done) return [3 /*break*/, 5]; _a = __read(map_1_1.value, 2), key = _a[0], values = _a[1]; return [4 /*yield*/, new GroupedIterable(key, values)]; case 3: _c.sent(); _c.label = 4; case 4: map_1_1 = map_1.next(); return [3 /*break*/, 2]; case 5: return [3 /*break*/, 8]; case 6: e_2_1 = _c.sent(); e_2 = { error: e_2_1 }; return [3 /*break*/, 8]; case 7: try { if (map_1_1 && !map_1_1.done && (_b = map_1.return)) _b.call(map_1); } finally { if (e_2) throw e_2.error; } return [7 /*endfinally*/]; case 8: return [2 /*return*/]; } }); }; return GroupByIterable; }(IterableX)); export { GroupByIterable }; /** * 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) => TKey)} keySelector A function to extract the key for each element. * @param {((value: TSource) => TValue)} [elementSelector=identity] A function to map each source element to an element in an async-enumerable group. * @returns {OperatorFunction<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) { if (elementSelector === void 0) { elementSelector = identity; } return function groupByOperatorFunction(source) { return new GroupByIterable(source, keySelector, elementSelector); }; } //# sourceMappingURL=groupby.js.map