import { IterableX } from '../iterablex' ; import { identity } from '../../util/identity' ; import { createGrouping } from './_grouping' ; import { OperatorFunction } from '../../interfaces' ; export class GroupedIterable < TKey , TValue > extends IterableX < TValue > { public readonly key : TKey ; private _source : Iterable < TValue > ; constructor ( key , source ) { super ( ) ; this . key = key ; this . _source = source ; } * [ Symbol . iterator ] ( ) { for ( const item of this . _source ) { yield item ; } } } export class GroupByIterable < TSource , TKey , TValue > extends IterableX < GroupedIterable < TKey , TValue > > { private _source : Iterable < TSource > ; private _keySelector : ( value : TSource ) => TKey ; private _elementSelector : ( value : TSource ) => TValue ; constructor ( source , keySelector , elementSelector ) { super ( ) ; this . _source = source ; this . _keySelector = keySelector ; this . _elementSelector = elementSelector ; } * [ Symbol . iterator ] ( ) { const map = createGrouping ( this . _source , this . _keySelector , this . _elementSelector ) ; for ( const [ key , values ] of map ) { yield new GroupedIterable < TKey , TValue > ( key , values ) ; } } } export function groupBy < TSource , TKey > ( keySelector ) ; export function groupBy < TSource , TKey , TValue > ( keySelector , elementSelector ? ) ; template TSource template TKey template TValue param { ( ( value : TSource ) => TKey ) } keySelector param { ( ( value : TSource ) => TValue ) } elementSelector returns { OperatorFunction < TSource , TResult > } export function groupBy < TSource , TKey , TValue > ( keySelector , elementSelector = identity ) { return function groupByOperatorFunction ( source ) : IterableX < GroupedIterable < TKey , TValue > > { return new GroupByIterable < TSource , TKey , TValue > ( source , keySelector , elementSelector ) ; } ; }