All files flatMap.ts

25% Statements 3/12
100% Branches 0/0
0% Functions 0/2
30% Lines 3/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 185x 5x                             5x  
import { Collection, asIterable } from './utils'
import Sequence from './Sequence'
 
function flatMap<T, U>(collection: Collection<T>, fn: (value: T, index: number) => Collection<U>) {
  const iterable = asIterable(collection)
  return new Sequence(function* () {
    let index = 0
    for(const value of iterable) {
      const result = asIterable(fn(value, index++))
      for(const innerValue of result) {
        yield innerValue
      }
    }
  })
}
 
export default flatMap