import { IterableX } from '../iterablex' ; import { arrayIndexOf } from '../../util/arrayindexof' ; import { comparer as defaultComparer } from '../../util/comparer' ; import { MonoTypeOperatorFunction } from '../../interfaces' ; function arrayRemove < T > ( array , item , comparer ) { const idx = arrayIndexOf ( array , item , comparer ) ; if ( idx === - 1 ) { return false ; } array . splice ( idx , 1 ) ; return true ; } export class IntersectIterable < TSource > extends IterableX < TSource > { private _first : Iterable < TSource > ; private _second : Iterable < TSource > ; private _comparer : ( x : TSource , y : TSource ) => boolean ; constructor ( first , second , comparer ) { super ( ) ; this . _first = first ; this . _second = second ; this . _comparer = comparer ; } * [ Symbol . iterator ] ( ) { const map = [ ] as TSource [ ] ; for ( const secondItem of this . _second ) { map . push ( secondItem ) ; } for ( const firstItem of this . _first ) { if ( arrayRemove ( map , firstItem , this . _comparer ) ) { yield firstItem ; } } } } template TSource param { Iterable < TSource > } second param { ( ( x : TSource , y : TSource ) => boolean ) } comparer returns { MonoTypeOperatorFunction < TSource > } export function intersect < TSource > ( second , comparer = defaultComparer ) { return function intersectOperatorFunction ( first ) : IterableX < TSource > { return new IntersectIterable < TSource > ( first , second , comparer ) ; } ; }