All files repeat.ts

100% Statements 19/19
100% Branches 4/4
100% Functions 4/4
100% Lines 13/13
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 28 29 30 31 32 33 3413x 13x             12x 7x 7x 4x 12x 24x                     13x 2x 5x 10x         13x  
import { Collection, asIterable } from './utils'
import Sequence from './Sequence'
 
/**
 * Return a Sequence whose elements are the elements of the passed collection repeated the specified number of times.
 * @param collection A Collection whose elements will be repeated in the resulting Sequence
 * @param times The number of times the elements are repeated. Defaults to Infinity
 */
function repeat<T>(collection: Collection<T>, times: number = Infinity) {
  const iterable = asIterable(collection)
  return new Sequence(function* () {
    for(let i = 0; i < times; i++) {
      for(const value of iterable) {
        yield value
      }
    }
  })
}
 
/**
 * Return a Sequence consisting of the supplied value repeated the specified number of times.
 * @param value A value to repeat
 * @param times The number of times the value is repeated. Defaults to Infinity
 */
export function repeatValue<T>(value: T, times: number = Infinity) {
  return new Sequence(function* () {
    for(let i = 0; i < times; i++) {
      yield value
    }
  })
}
 
export default repeat