UNPKG

@reactivex/ix-es5-esm

Version:

The Interactive Extensions for JavaScript

172 lines (170 loc) 8.29 kB
import { __asyncGenerator, __await, __extends, __generator, __values } from "tslib"; import { AsyncIterableX } from '../asynciterablex.js'; import { toArray } from '../toarray.js'; import { sorter as defaultSorter } from '../../util/sorter.js'; import { throwIfAborted } from '../../aborterror.js'; /** @ignore */ var OrderedAsyncIterableBaseX = /** @class */ (function (_super) { __extends(OrderedAsyncIterableBaseX, _super); function OrderedAsyncIterableBaseX(source) { var _this = _super.call(this) || this; _this._source = source; return _this; } OrderedAsyncIterableBaseX.prototype[Symbol.asyncIterator] = function (signal) { return __asyncGenerator(this, arguments, function _a() { var array, len, indices, i, indices_1, indices_1_1, index, e_1_1; var e_1, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: throwIfAborted(signal); return [4 /*yield*/, __await(toArray(this._source, signal))]; case 1: array = _c.sent(); len = array.length; indices = new Array(len); for (i = 0; i < len; i++) { indices[i] = i; } indices.sort(this._getSorter(array)); _c.label = 2; case 2: _c.trys.push([2, 8, 9, 10]); indices_1 = __values(indices), indices_1_1 = indices_1.next(); _c.label = 3; case 3: if (!!indices_1_1.done) return [3 /*break*/, 7]; index = indices_1_1.value; return [4 /*yield*/, __await(array[index])]; case 4: return [4 /*yield*/, _c.sent()]; case 5: _c.sent(); _c.label = 6; case 6: indices_1_1 = indices_1.next(); return [3 /*break*/, 3]; case 7: return [3 /*break*/, 10]; case 8: e_1_1 = _c.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 10]; case 9: try { if (indices_1_1 && !indices_1_1.done && (_b = indices_1.return)) _b.call(indices_1); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 10: return [2 /*return*/]; } }); }); }; OrderedAsyncIterableBaseX.prototype.thenBy = function (keySelector, comparer) { if (comparer === void 0) { comparer = defaultSorter; } return new OrderedAsyncIterableX(this._source, keySelector, comparer, false, this); }; OrderedAsyncIterableBaseX.prototype.thenByDescending = function (keySelector, comparer) { if (comparer === void 0) { comparer = defaultSorter; } return new OrderedAsyncIterableX(this._source, keySelector, comparer, true, this); }; return OrderedAsyncIterableBaseX; }(AsyncIterableX)); export { OrderedAsyncIterableBaseX }; /** @ignore */ /** @ignore */ var OrderedAsyncIterableX = /** @class */ (function (_super) { __extends(OrderedAsyncIterableX, _super); function OrderedAsyncIterableX(source, keySelector, comparer, descending, parent) { var _this = _super.call(this, source) || this; _this._keySelector = keySelector; _this._comparer = comparer; _this._descending = descending; _this._parent = parent; return _this; } OrderedAsyncIterableX.prototype._getSorter = function (elements, next) { var keys = elements.map(this._keySelector); var comparer = this._comparer; var parent = this._parent; var descending = this._descending; var sorter = function (x, y) { var result = comparer(keys[x], keys[y]); if (result === 0) { return next ? next(x, y) : x - y; } return descending ? -result : result; }; return parent ? parent._getSorter(elements, sorter) : sorter; }; return OrderedAsyncIterableX; }(OrderedAsyncIterableBaseX)); export { OrderedAsyncIterableX }; /** /** * Sorts the elements of a sequence in ascending order according to a key by using a specified comparer. * * @template TKey The type of the elements of source. * @template TSource The type of the key returned by keySelector. * @param {(item: TSource) => TKey} keySelector A function to extract a key from an element. * @param {(fst: TKey, snd: TKey) => number} [comparer=defaultSorter] A comparer to compare keys. * @returns {UnaryFunction<AsyncIterable<TSource>, OrderedAsyncIterableX<TKey, TSource>>} An ordered async-iterable sequence whose * elements are sorted according to a key and comparer. */ export function orderBy(keySelector, comparer) { if (comparer === void 0) { comparer = defaultSorter; } return function orderByOperatorFunction(source) { return new OrderedAsyncIterableX(source, keySelector, comparer, false); }; } /** * Sorts the elements of a sequence in descending order according to a key by using a specified comparer. * * @template TKey The type of the elements of source. * @template TSource The type of the key returned by keySelector. * @param {(item: TSource) => TKey} keySelector A function to extract a key from an element. * @param {(fst: TKey, snd: TKey) => number} [comparer=defaultSorter] A comparer to compare keys. * @returns {UnaryFunction<AsyncIterable<TSource>, OrderedAsyncIterableX<TKey, TSource>>} An ordered async-iterable sequence whose * elements are sorted in descending order according to a key and comparer. */ export function orderByDescending(keySelector, comparer) { if (comparer === void 0) { comparer = defaultSorter; } return function orderByDescendingOperatorFunction(source) { return new OrderedAsyncIterableX(source, keySelector, comparer, true); }; } /** * Performs a subsequent ordering of the elements in a sequence in ascending order according to a key using a specified comparer. * * @template TKey The type of the elements of source. * @template TSource The type of the key returned by keySelector. * @param {(item: TSource) => TKey} keySelector A function to extract a key from an element. * @param {(fst: TKey, snd: TKey) => number} [comparer=defaultSorter] A comparer to compare keys. * @returns {UnaryFunction<AsyncIterable<TSource>, OrderedAsyncIterableX<TKey, TSource>>} An ordered async-iterable whose elements are * sorted according to a key and comparer. */ export function thenBy(keySelector, comparer) { if (comparer === void 0) { comparer = defaultSorter; } return function thenByOperatorFunction(source) { var orderSource = source; return new OrderedAsyncIterableX(orderSource._source, keySelector, comparer, false, orderSource); }; } /** * Performs a subsequent ordering of the elements in a sequence in descending order according to a key using a specified comparer. * * @template TKey The type of the elements of source. * @template TSource The type of the key returned by keySelector. * @param {(item: TSource) => TKey} keySelector A function to extract a key from an element. * @param {(fst: TKey, snd: TKey) => number} [comparer=defaultSorter] A comparer to compare keys. * @returns {UnaryFunction<AsyncIterable<TSource>, OrderedAsyncIterableX<TKey, TSource>>} An ordered async-iterable whose elements are * sorted in descending order according to a key and comparer. */ export function thenByDescending(keySelector, comparer) { if (comparer === void 0) { comparer = defaultSorter; } return function thenByDescendingOperatorFunction(source) { var orderSource = source; return new OrderedAsyncIterableX(orderSource._source, keySelector, comparer, true, orderSource); }; } //# sourceMappingURL=orderby.js.map