All files zip.ts

100% Statements 14/14
100% Branches 2/2
100% Functions 2/2
100% Lines 14/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2813x 13x                 12x 12x   12x 3x 3x 3x 3x 3x 16x 8x 8x         13x  
import { Collection, asIterable } from './utils'
import Sequence from './Sequence'
 
/**
 * Return a Sequence whose elements are two element arrays created from the elements of the collections
 * passed as arguments. The length of the sequence is equal to the length of the shorter collection.
 * @param a A Collection to zip
 * @param b A Collection to zip
 */
function zip<T, U>(a: Collection<T>, b: Collection<U>) {
  const aIterable = asIterable(a)
  const bIterable = asIterable(b)
 
  return new Sequence(function* (): Iterator<[T, U]> {
    const aIterator = aIterable[Symbol.iterator]()
    const bIterator = bIterable[Symbol.iterator]()
    let aValue = aIterator.next()
    let bValue = bIterator.next()
    while(!aValue.done && !bValue.done) {
      yield [aValue.value, bValue.value]
      aValue = aIterator.next()
      bValue = bIterator.next()
    }
  })
}
 
export default zip