All files take.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 2/2
100% Lines 10/10
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 2613x 13x                   7x 7x 4x 14x 2x 24x   2x           13x  
import { Collection, asIterable } from './utils'
import Sequence from './Sequence'
 
/**
 * Return a Sequence that contains the first elements of the collection. The argument specifies the
 * number of elements to take. If the length of the collection is smaller, all of the colleciton elements
 * will be present in the resulting sequence.
 * @param collection A collection to use as source of elements.
 * @param count The number of elements to take.
 */
function take<T>(collection: Collection<T>, count: number) {
  const iterable = asIterable(collection)
  return new Sequence(function* () {
    let index = 0
    for(const value of iterable) {
      if(index++ < count) {
        yield value
      } else {
        break
      }
    }
  })
}
 
export default take