import { OrderedAsyncEnumerable } from "../async/OrderedAsyncEnumerable" ; import { IComparer , IOrderedAsyncEnumerable , IOrderedEnumerable } from "../types" ; import { asSortedKeyValues } from "./_ordered/asSortedKeyValues" ; import { asSortedKeyValuesAsync } from "./_ordered/asSortedKeyValuesAsync" ; import { BasicEnumerable } from "./BasicEnumerable" ; private export class OrderedEnumerable < T > extends BasicEnumerable < T > implements IOrderedEnumerable < T > { private constructor ( private readonly orderedPairs ) { super ( function * ( ) { for ( const orderedPair of orderedPairs ( ) ) { yield * orderedPair ; } } ) ; } public static generate < TSource , TKey > ( source , keySelector , ascending , comparer ? ) { let orderedPairs ; if ( source instanceof OrderedEnumerable ) { orderedPairs = function * ( ) { for ( const pair of source . orderedPairs ( ) ) { yield * asSortedKeyValues ( pair , keySelector , ascending , comparer ) ; } } ; } else { orderedPairs = ( ) => asSortedKeyValues ( source , keySelector , ascending , comparer ) ; } return new OrderedEnumerable ( orderedPairs ) ; } public static generateAsync < TSource , TKey > ( source , keySelector , ascending , comparer ? ) { let orderedPairs ; if ( source instanceof OrderedEnumerable ) { orderedPairs = async function * ( ) { for ( const pair of source . orderedPairs ( ) ) { yield * asSortedKeyValuesAsync ( pair , keySelector , ascending , comparer ) ; } } ; } else { orderedPairs = ( ) => asSortedKeyValuesAsync ( source , keySelector , ascending , comparer ) ; } return new OrderedAsyncEnumerable ( orderedPairs ) ; } public thenBy < TKey > ( keySelector , comparer ? ) { return OrderedEnumerable . generate ( this , keySelector , true , comparer ) ; } public thenByAsync < TKey > ( keySelector , comparer ? ) { return OrderedEnumerable . generateAsync ( this , keySelector , true , comparer ) ; } public thenByDescending < TKey > ( keySelector , comparer ? ) { return OrderedEnumerable . generate ( this , keySelector , false , comparer ) ; } public thenByDescendingAsync < TKey > ( keySelector , comparer ? ) { return OrderedEnumerable . generateAsync ( this , keySelector , false , comparer ) ; } }