import { IAsyncEnumerable } from "../../types" ; import { BasicAsyncEnumerable } from "../BasicAsyncEnumerable" ; type Zip = { param first param second returns < T , Y > ( first , second ) : IAsyncEnumerable < [ T , Y ] > ; param first param second param resultSelector returns < TFirst , TSecond , TResult > ( first , second , resultSelector ) : IAsyncEnumerable < TResult > ; } ; export const zip = < TFirst , TSecond , TResult > ( first , second , resultSelector ? ) : IAsyncEnumerable < TResult > | IAsyncEnumerable < [ TFirst , TSecond ] > => { if ( resultSelector ) { return zip2 ( first , second , resultSelector ) ; } else { return zip1 ( first , second ) ; } } ; const zip1 = < T , Y > ( source , second ) : IAsyncEnumerable < [ T , Y ] > => { async function * iterator ( ) { const firstIterator = source [ Symbol . asyncIterator ] ( ) ; const secondIterator = second [ Symbol . asyncIterator ] ( ) ; while ( true ) { const result = await Promise . all ( [ firstIterator . next ( ) , secondIterator . next ( ) ] ) ; const a = result [ 0 ] ; const b = result [ 1 ] ; if ( a . done && b . done ) { break ; } else { yield [ a . value , b . value ] ; } } } return new BasicAsyncEnumerable ( iterator ) ; } ; const zip2 = < T , Y , OUT > ( source , second , resultSelector ) : IAsyncEnumerable < OUT > => { async function * iterator ( ) { const firstIterator = source [ Symbol . asyncIterator ] ( ) ; const secondIterator = second [ Symbol . asyncIterator ] ( ) ; while ( true ) { const result = await Promise . all ( [ firstIterator . next ( ) , secondIterator . next ( ) ] ) ; const a = result [ 0 ] ; const b = result [ 1 ] ; if ( a . done && b . done ) { break ; } else { yield resultSelector ( a . value , b . value ) ; } } } return new BasicAsyncEnumerable ( iterator ) ; } ;