@reactivex/ix-es5-esm
Version:
The Interactive Extensions for JavaScript
137 lines (135 loc) • 6.14 kB
JavaScript
import { __asyncGenerator, __await, __extends, __generator, __read, __values } from "tslib";
import { AsyncIterableX } from '../asynciterablex.js';
import { identityAsync } from '../../util/identity.js';
import { createGrouping } from './_grouping.js';
import { throwIfAborted } from '../../aborterror.js';
/** @ignore */
/** @ignore */
var GroupedAsyncIterable = /** @class */ (function (_super) {
__extends(GroupedAsyncIterable, _super);
function GroupedAsyncIterable(key, source) {
var _this = _super.call(this) || this;
_this.key = key;
_this._source = source;
return _this;
}
GroupedAsyncIterable.prototype[Symbol.asyncIterator] = function (signal) {
return __asyncGenerator(this, arguments, function _a() {
var _b, _c, item, e_1_1;
var e_1, _d;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
throwIfAborted(signal);
_e.label = 1;
case 1:
_e.trys.push([1, 7, 8, 9]);
_b = __values(this._source), _c = _b.next();
_e.label = 2;
case 2:
if (!!_c.done) return [3 /*break*/, 6];
item = _c.value;
return [4 /*yield*/, __await(item)];
case 3: return [4 /*yield*/, _e.sent()];
case 4:
_e.sent();
_e.label = 5;
case 5:
_c = _b.next();
return [3 /*break*/, 2];
case 6: return [3 /*break*/, 9];
case 7:
e_1_1 = _e.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 9];
case 8:
try {
if (_c && !_c.done && (_d = _b.return)) _d.call(_b);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 9: return [2 /*return*/];
}
});
});
};
return GroupedAsyncIterable;
}(AsyncIterableX));
export { GroupedAsyncIterable };
/** @ignore */
var GroupByAsyncIterable = /** @class */ (function (_super) {
__extends(GroupByAsyncIterable, _super);
function GroupByAsyncIterable(source, keySelector, elementSelector) {
var _this = _super.call(this) || this;
_this._source = source;
_this._keySelector = keySelector;
_this._elementSelector = elementSelector;
return _this;
}
GroupByAsyncIterable.prototype[Symbol.asyncIterator] = function (signal) {
return __asyncGenerator(this, arguments, function _a() {
var map, map_1, map_1_1, _b, key, values, e_2_1;
var e_2, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
throwIfAborted(signal);
return [4 /*yield*/, __await(createGrouping(this._source, this._keySelector, this._elementSelector, signal))];
case 1:
map = _d.sent();
_d.label = 2;
case 2:
_d.trys.push([2, 8, 9, 10]);
map_1 = __values(map), map_1_1 = map_1.next();
_d.label = 3;
case 3:
if (!!map_1_1.done) return [3 /*break*/, 7];
_b = __read(map_1_1.value, 2), key = _b[0], values = _b[1];
return [4 /*yield*/, __await(new GroupedAsyncIterable(key, values))];
case 4: return [4 /*yield*/, _d.sent()];
case 5:
_d.sent();
_d.label = 6;
case 6:
map_1_1 = map_1.next();
return [3 /*break*/, 3];
case 7: return [3 /*break*/, 10];
case 8:
e_2_1 = _d.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 10];
case 9:
try {
if (map_1_1 && !map_1_1.done && (_c = map_1.return)) _c.call(map_1);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 10: return [2 /*return*/];
}
});
});
};
return GroupByAsyncIterable;
}(AsyncIterableX));
export { 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.
*/
export function groupBy(keySelector, elementSelector) {
if (elementSelector === void 0) { elementSelector = identityAsync; }
return function groupByOperatorFunction(source) {
return new GroupByAsyncIterable(source, keySelector, elementSelector);
};
}
//# sourceMappingURL=groupby.js.map