import { IEnumerable } from "../../types" ; import { BasicEnumerable } from "../BasicEnumerable" ; export type SelectFunc = { param source param selector returns < TSource , TResult > ( source , selector ) : IEnumerable < TResult > ; param source param selector returns < TSource , TKey extends keyof TSource > ( source , key ) : IEnumerable < TSource [ TKey ] > ; } ; param source param selector returns export const select = < TSource , TKey extends keyof TSource , TResult > ( source , selector ) : IEnumerable < TSource [ TKey ] > | IEnumerable < TResult > => { if ( typeof selector === "function" ) { const { length } = selector ; if ( length === 1 ) { return select1 ( source , selector as ( x : TSource ) => TResult ) ; } else { return select2 ( source , selector ) ; } } else { return select3 ( source , selector ) ; } } ; const select1 = < TSource , TResult > ( source , selector ) => { function * iterator ( ) { for ( const value of source ) { yield selector ( value ) ; } } return new BasicEnumerable ( iterator ) ; } ; const select2 = < TSource , TResult > ( source , selector ) => { function * iterator ( ) { let index = 0 ; for ( const value of source ) { yield selector ( value , index ) ; index ++ ; } } return new BasicEnumerable ( iterator ) ; } ; const select3 = < TSource , TKey extends keyof TSource > ( source , key ) => { function * iterator ( ) { for ( const value of source ) { yield value [ key ] ; } } return new BasicEnumerable ( iterator ) ; } ;