@reactivex/ix-esnext-esm
Version:
The Interactive Extensions for JavaScript
1 lines • 10 kB
Source Map (JSON)
{"version":3,"sources":["iterable/operators/orderby.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,MAAM,IAAI,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAG5D,MAAM,OAAgB,oBAA8B,SAAQ,SAAkB;IAG5E,YAAY,MAAyB;QACnC,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC;QAChB,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAU,IAAI,CAAC,OAAO,CAAC,CAAC;QAChD,MAAM,GAAG,GAAG,KAAK,CAAC,MAAM,CAAC;QACzB,MAAM,OAAO,GAAG,IAAI,KAAK,CAAS,GAAG,CAAC,CAAC;QACvC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;YAC5B,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;SAChB;QAED,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;QACrC,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE;YAC3B,MAAM,KAAK,CAAC,KAAK,CAAC,CAAC;SACpB;IACH,CAAC;IAED,MAAM,CACJ,WAAoC,EACpC,WAA6C,aAAa;QAE1D,OAAO,IAAI,gBAAgB,CAAgB,IAAI,CAAC,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IAC/F,CAAC;IAED,gBAAgB,CACd,WAAoC,EACpC,WAA6C,aAAa;QAE1D,OAAO,IAAI,gBAAgB,CAAgB,IAAI,CAAC,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,CA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AAoC,EACpC,WAA6C,aAAa;IAE1D,OAAO,SAAS,uBAAuB,CAAC,MAAyB;QAC/D,OAAO,IAAI,gBAAgB,CAAgB,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;IACnF,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,iBAAiB,CAC/B,WAAoC,EACpC,WAA6C,aAAa;IAE1D,OAAO,SAAS,iCAAiC,CAAC,MAAyB;QACzE,OAAO,IAAI,gBAAgB,CAAgB,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;IAClF,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,MAAM,CACpB,WAAoC,EACpC,WAA6C,aAAa;IAE1D,OAAO,SAAS,sBAAsB,CAAC,MAAyB;QAC9D,MAAM,WAAW,GAAkC,MAAM,CAAC;QAC1D,OAAO,IAAI,gBAAgB,CACzB,WAAW,CAAC,OAAO,EACnB,WAAW,EACX,QAAQ,EACR,KAAK,EACL,WAAW,CACZ,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAC9B,WAAoC,EACpC,WAA6C,aAAa;IAE1D,OAAO,SAAS,gCAAgC,CAAC,MAAyB;QACxE,MAAM,WAAW,GAAkC,MAAM,CAAC;QAC1D,OAAO,IAAI,gBAAgB,CACzB,WAAW,CAAC,OAAO,EACnB,WAAW,EACX,QAAQ,EACR,IAAI,EACJ,WAAW,CACZ,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC","file":"orderby.js","sourcesContent":["import { IterableX } from '../iterablex';\nimport { sorter as defaultSorter } from '../../util/sorter';\nimport { UnaryFunction } from '../../interfaces';\n\nexport abstract class OrderedIterableBaseX<TSource> extends IterableX<TSource> {\n _source: Iterable<TSource>;\n\n constructor(source: Iterable<TSource>) {\n super();\n this._source = source;\n }\n\n *[Symbol.iterator]() {\n const array = Array.from<TSource>(this._source);\n const len = array.length;\n const indices = new Array<number>(len);\n for (let i = 0; i < len; i++) {\n indices[i] = i;\n }\n\n indices.sort(this._getSorter(array));\n for (const index of indices) {\n yield array[index];\n }\n }\n\n thenBy<TKey>(\n keySelector: (item: TSource) => TKey,\n comparer: (fst: TKey, snd: TKey) => number = defaultSorter\n ): OrderedIterableBaseX<TSource> {\n return new OrderedIterableX<TKey, TSource>(this._source, keySelector, comparer, false, this);\n }\n\n thenByDescending<TKey>(\n keySelector: (item: TSource) => TKey,\n comparer: (fst: TKey, snd: TKey) => number = defaultSorter\n ): OrderedIterableBaseX<TSource> {\n return new OrderedIterableX<TKey, TSource>(this._source, keySelector, comparer, true, this);\n }\n\n abstract _getSorter(\n elements: TSource[],\n next?: (x: number, y: number) => number\n ): (x: number, y: number) => number;\n}\n\nexport class OrderedIterableX<TKey, TSource> extends OrderedIterableBaseX<TSource> {\n private _keySelector: (item: TSource) => TKey;\n private _comparer: (fst: TKey, snd: TKey) => number;\n private _descending: boolean;\n private _parent?: OrderedIterableBaseX<TSource>;\n\n constructor(\n source: Iterable<TSource>,\n keySelector: (item: TSource) => TKey,\n comparer: (fst: TKey, snd: TKey) => number,\n descending: boolean,\n parent?: OrderedIterableBaseX<TSource>\n ) {\n super(source);\n this._keySelector = keySelector;\n this._comparer = comparer;\n this._descending = descending;\n this._parent = parent;\n }\n\n _getSorter(\n elements: TSource[],\n next?: (x: number, y: number) => number\n ): (x: number, y: number) => number {\n const keys = elements.map(this._keySelector);\n const comparer = this._comparer;\n const parent = this._parent;\n const descending = this._descending;\n const sorter = (x: number, y: number): number => {\n const result = comparer(keys[x], keys[y]);\n if (result === 0) {\n return next ? next(x, y) : x - y;\n }\n\n return descending ? -result : result;\n };\n\n return parent ? parent._getSorter(elements, sorter) : sorter;\n }\n}\n\n/**\n * Sorts the elements of a sequence in ascending order according to a key by using a specified comparer.\n *\n * @export\n * @template TKey The type of the elements of source.\n * @template TSource The type of the key returned by keySelector.\n * @param {(item: TSource) => TKey} keySelector A function to extract a key from an element.\n * @param {(fst: TKey, snd: TKey) => number} [comparer=defaultSorter] A comparer to compare keys.\n * @returns {UnaryFunction<Iterable<TSource>, OrderedIterableX<TKey, TSource>>} An ordered iterable sequence whose\n * elements are sorted according to a key and comparer.\n */\nexport function orderBy<TKey, TSource>(\n keySelector: (item: TSource) => TKey,\n comparer: (fst: TKey, snd: TKey) => number = defaultSorter\n): UnaryFunction<Iterable<TSource>, OrderedIterableX<TKey, TSource>> {\n return function orderByOperatorFunction(source: Iterable<TSource>) {\n return new OrderedIterableX<TKey, TSource>(source, keySelector, comparer, false);\n };\n}\n\n/**\n * Sorts the elements of a sequence in descending order according to a key by using a specified comparer.\n *\n * @export\n * @template TKey The type of the elements of source.\n * @template TSource The type of the key returned by keySelector.\n * @param {(item: TSource) => TKey} keySelector A function to extract a key from an element.\n * @param {(fst: TKey, snd: TKey) => number} [comparer=defaultSorter] A comparer to compare keys.\n * @returns {UnaryFunction<Iterable<TSource>, OrderedIterableX<TKey, TSource>>} An ordered iterable sequence whose\n * elements are sorted in descending order according to a key and comparer.\n */\nexport function orderByDescending<TKey, TSource>(\n keySelector: (item: TSource) => TKey,\n comparer: (fst: TKey, snd: TKey) => number = defaultSorter\n): UnaryFunction<Iterable<TSource>, OrderedIterableX<TKey, TSource>> {\n return function orderByDescendingOperatorFunction(source: Iterable<TSource>) {\n return new OrderedIterableX<TKey, TSource>(source, keySelector, comparer, true);\n };\n}\n\n/**\n * Performs a subsequent ordering of the elements in a sequence in ascending order according to a key using a specified comparer.\n *\n * @export\n * @template TKey The type of the elements of source.\n * @template TSource The type of the key returned by keySelector.\n * @param {(item: TSource) => TKey} keySelector A function to extract a key from an element.\n * @param {(fst: TKey, snd: TKey) => number} [comparer=defaultSorter] A comparer to compare keys.\n * @returns {UnaryFunction<Iterable<TSource>, OrderedIterableX<TKey, TSource>>} An ordered iterable whose elements are\n * sorted according to a key and comparer.\n */\nexport function thenBy<TKey, TSource>(\n keySelector: (item: TSource) => TKey,\n comparer: (fst: TKey, snd: TKey) => number = defaultSorter\n): UnaryFunction<Iterable<TSource>, OrderedIterableX<TKey, TSource>> {\n return function thenByOperatorFunction(source: Iterable<TSource>) {\n const orderSource = <OrderedIterableBaseX<TSource>>source;\n return new OrderedIterableX<TKey, TSource>(\n orderSource._source,\n keySelector,\n comparer,\n false,\n orderSource\n );\n };\n}\n\n/**\n * Performs a subsequent ordering of the elements in a sequence in descending order according to a key using a specified comparer.\n *\n * @export\n * @template TKey The type of the elements of source.\n * @template TSource The type of the key returned by keySelector.\n * @param {(item: TSource) => TKey} keySelector A function to extract a key from an element.\n * @param {(fst: TKey, snd: TKey) => number} [comparer=defaultSorter] A comparer to compare keys.\n * @returns {UnaryFunction<Iterable<TSource>, OrderedIterableX<TKey, TSource>>} An ordered iterable whose elements are\n * sorted in descending order according to a key and comparer.\n */\nexport function thenByDescending<TKey, TSource>(\n keySelector: (item: TSource) => TKey,\n comparer: (fst: TKey, snd: TKey) => number = defaultSorter\n): UnaryFunction<Iterable<TSource>, OrderedIterableX<TKey, TSource>> {\n return function thenByDescendingOperatorFunction(source: Iterable<TSource>) {\n const orderSource = <OrderedIterableBaseX<TSource>>source;\n return new OrderedIterableX<TKey, TSource>(\n orderSource._source,\n keySelector,\n comparer,\n true,\n orderSource\n );\n };\n}\n"]}