import { ErrorString , InvalidOperationException } from "../../shared" ; type AverageFunc = { param source throws returns ( source ) : number ; param source param selector throws returns < TSource > ( source , selector ) : number ; } ; export const average = < TSource > ( source , selector ? ) : number => { if ( selector ) { return average2 ( source as Iterable < TSource > , selector ) ; } else { return average1 ( source as Iterable < number > ) ; } } ; const average1 = ( source ) : number => { let value ; let count ; for ( const item of source ) { value = ( value || 0 ) + item ; count = ( count || 0 ) + 1 ; } if ( value === undefined ) { throw new InvalidOperationException ( ErrorString . NoElements ) ; } return value / ( count as number ) ; } ; const average2 = < TSource > ( source , func ) : number => { let value ; let count ; for ( const item of source ) { value = ( value || 0 ) + func ( item ) ; count = ( count || 0 ) + 1 ; } if ( value === undefined ) { throw new InvalidOperationException ( ErrorString . NoElements ) ; } return value / ( count as number ) ; } ;