import { IterableX } from '../iterablex' ; import { isIterable } from '../../util/isiterable' ; import { MonoTypeOperatorFunction } from '../../interfaces' ; export class FlattenIterable < TSource > extends IterableX < TSource > { private _source : Iterable < TSource > ; private _depth : number ; constructor ( source , depth ) { super ( ) ; this . _source = source ; this . _depth = depth ; } private * _flatten ( source , depth ) { if ( depth === 0 ) { for ( const item of source ) { yield item ; } return undefined ; } for ( const item of source ) { if ( isIterable ( item ) ) { for ( const innerItem of this . _flatten ( item , depth - 1 ) ) { yield innerItem ; } } else { yield item ; } } } [ Symbol . iterator ] ( ) { return this . _flatten ( this . _source , this . _depth ) [ Symbol . iterator ] ( ) ; } } template T param { number } depth returns { MonoTypeOperatorFunction < T > } export function flat < T > ( depth = Infinity ) { return function flattenOperatorFunction ( source ) : IterableX < T > { return new FlattenIterable < T > ( source , depth ) ; } ; }