import { IEnumerable } from "../../types" ; import { BasicEnumerable } from "../BasicEnumerable" ; type SelectManyFunc = { param source param selector returns < TSource , TResult > ( source , selector ) : IEnumerable < TResult > ; param source param selector returns < TSource extends { [ key ] : Iterable < TResult > ; } , TResult > ( source , selector ) : IEnumerable < TResult > ; } ; export const selectMany = < TSource extends { [ key ] : Iterable < TResult > ; } , TResult > ( source , selector ) => { if ( typeof selector === "function" ) { if ( selector . length === 1 ) { return selectMany1 ( source , selector as ( x : TSource ) => Iterable < TResult > ) ; } else { return selectMany2 ( source , selector ) ; } } else { return selectMany3 ( source , selector ) ; } } ; const selectMany1 = < TSource , TResult > ( source , selector ) => { function * iterator ( ) { for ( const value of source ) { for ( const selectorValue of selector ( value ) ) { yield selectorValue ; } } } return new BasicEnumerable ( iterator ) ; } ; const selectMany2 = < TSource , TResult > ( source , selector ) => { function * iterator ( ) { let index = 0 ; for ( const value of source ) { for ( const selectorValue of selector ( value , index ) ) { yield selectorValue ; } index ++ ; } } return new BasicEnumerable ( iterator ) ; } ; const selectMany3 = < TSource extends { [ key ] : Iterable < TResult > ; } , TResult > ( source , selector ) => { function * iterator ( ) { for ( const value of source ) { for ( const selectorValue of value [ selector ] ) { yield selectorValue ; } } } return new BasicEnumerable ( iterator ) ; } ;