import { IterableActivity , Source , isSource } from "./activity" ; export type GroupFn < T > = ( row : T , index : number ) => number | string ; export type GroupRow < T > = { key : string ; value : T [ ] ; } ; function groupGen < T = any > ( groupFn ) { return function * ( source ) { let i = - 1 ; const group = { } ; for ( const row of source ) { const key = groupFn ( row , ++ i ) ; if ( ! group [ key ] ) { group [ key ] = [ ] ; } group [ key ] . push ( row ) ; } for ( const key in group ) { yield { key , value : group [ key ] } ; } } ; } export function group < T > ( groupByFn ) ; export function group < T > ( source , groupByFn ) ; export function group < T > ( s_or_gbf , groupByFn ? ) { return isSource ( s_or_gbf ) ? groupGen < T > ( groupByFn ! ) ( s_or_gbf ) : groupGen < T > ( s_or_gbf ) ; }