UNPKG

@reactivex/ix-es5-esm

Version:

The Interactive Extensions for JavaScript

163 lines (161 loc) 7.51 kB
import { __extends, __generator, __values } from "tslib"; import { IterableX } from '../iterablex.js'; import { sorter as defaultSorter } from '../../util/sorter.js'; /** @ignore */ var OrderedIterableBaseX = /** @class */ (function (_super) { __extends(OrderedIterableBaseX, _super); function OrderedIterableBaseX(source) { var _this = _super.call(this) || this; _this._source = source; return _this; } OrderedIterableBaseX.prototype[Symbol.iterator] = function () { var array, len, indices, i, indices_1, indices_1_1, index, e_1_1; var e_1, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: array = Array.from(this._source); len = array.length; indices = new Array(len); for (i = 0; i < len; i++) { indices[i] = i; } indices.sort(this._getSorter(array)); _b.label = 1; case 1: _b.trys.push([1, 6, 7, 8]); indices_1 = __values(indices), indices_1_1 = indices_1.next(); _b.label = 2; case 2: if (!!indices_1_1.done) return [3 /*break*/, 5]; index = indices_1_1.value; return [4 /*yield*/, array[index]]; case 3: _b.sent(); _b.label = 4; case 4: indices_1_1 = indices_1.next(); return [3 /*break*/, 2]; case 5: return [3 /*break*/, 8]; case 6: e_1_1 = _b.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 8]; case 7: try { if (indices_1_1 && !indices_1_1.done && (_a = indices_1.return)) _a.call(indices_1); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 8: return [2 /*return*/]; } }); }; OrderedIterableBaseX.prototype.thenBy = function (keySelector, comparer) { if (comparer === void 0) { comparer = defaultSorter; } return new OrderedIterableX(this._source, keySelector, comparer, false, this); }; OrderedIterableBaseX.prototype.thenByDescending = function (keySelector, comparer) { if (comparer === void 0) { comparer = defaultSorter; } return new OrderedIterableX(this._source, keySelector, comparer, true, this); }; return OrderedIterableBaseX; }(IterableX)); export { OrderedIterableBaseX }; /** @ignore */ /** @ignore */ var OrderedIterableX = /** @class */ (function (_super) { __extends(OrderedIterableX, _super); function OrderedIterableX(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; } OrderedIterableX.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 OrderedIterableX; }(OrderedIterableBaseX)); export { OrderedIterableX }; /** * 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<Iterable<TSource>, OrderedIterableX<TKey, TSource>>} An ordered 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 OrderedIterableX(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<Iterable<TSource>, OrderedIterableX<TKey, TSource>>} An ordered 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 OrderedIterableX(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<Iterable<TSource>, OrderedIterableX<TKey, TSource>>} An ordered 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 OrderedIterableX(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<Iterable<TSource>, OrderedIterableX<TKey, TSource>>} An ordered 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 OrderedIterableX(orderSource._source, keySelector, comparer, true, orderSource); }; } //# sourceMappingURL=orderby.js.map