import { IterableX } from './iterablex' ; import { identity } from '../util/identity' ; import { bindCallback } from '../util/bindcallback' ; import { isIterable , isArrayLike , isIterator } from '../util/isiterable' ; import { toLength } from '../util/tolength' ; nocollapse export let from ; nocollapse export let FromIterable ; export function _initialize ( Ctor ) { nocollapse from = function < TSource , TResult = TSource > ( source , selector = identity , thisArg ? ) : IterableX < TResult > { const fn = bindCallback ( selector , thisArg , 2 ) ; if ( isIterable ( source ) ) { return new FromIterable < TSource , TResult > ( source , fn ) ; } if ( isArrayLike ( source ) ) { return new FromIterable < TSource , TResult > ( source , fn ) ; } if ( isIterator ( source ) ) { return new FromIterable < TSource , TResult > ( { [ Symbol . iterator ] : ( ) => source } , fn ) ; } throw new TypeError ( 'Input type not supported' ) ; } ; FromIterable = class FromIterable < TSource , TResult = TSource > extends Ctor < TResult > { private _source : Iterable < TSource > | ArrayLike < TSource > ; private _fn : ( value : TSource , index : number ) => TResult ; constructor ( source , fn ) { super ( ) ; this . _source = source ; this . _fn = fn ; } * [ Symbol . iterator ] ( ) { const iterable = isIterable ( this . _source ) ; let i = 0 ; if ( iterable ) { for ( const item of < Iterable < TSource > > this . _source ) { yield this . _fn ( item , i ++ ) ; } } else { const length = toLength ( ( < ArrayLike < TSource > > this . _source ) . length ) ; while ( i < length ) { const val = ( < ArrayLike < TSource > > this . _source ) [ i ] ; yield this . _fn ( val , i ++ ) ; } } } } ; }