All files range.ts

100% Statements 23/23
100% Branches 6/6
100% Functions 3/3
100% Lines 19/19
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 5613x                                             4x   4x 4x 3x 2x 2x 2x   1x 1x     1x     4x 3x 11x 22x       1x 5x 10x           13x  
import Sequence from './Sequence'
 
/**
 * Return a Sequence of consecutive integers smaller than the value of the first parameter starting with 0
 * @param end Upper limit of the sequence
 */
function range(end: number): Sequence<number>
/**
 * Return a Sequence of consecutive integers smaller than the value of the first parameter starting with the value
 * of the second parameter
 * @param start First element of the sequence
 * @param end Upper limit of the sequence
 */
function range(start: number, end: number): Sequence<number>
/**
 * Return a Sequence of integers smaller than the value of the first parameter starting with the value
 * of the second parameter. The value of the third parameter dictates the step.
 * @param start First element of the sequence
 * @param end Upper limit of the sequence
 * @param step Difference between two consecutive elements of the Sequence
 */
function range(start: number, end: number, step: number): Sequence<number>
function range(a: number, b?: number, c?: number) {
  let start = 0
  let end: number
  let step = 1
  if(b !== undefined) {
    if(c !== undefined) {
      start = a
      end = b
      step = c
    } else {
      start = a
      end = b
    }
  } else {
    end = a
  }
 
  if(step > 0) {
    return new Sequence(function* () {
      for(let i = start; i < end; i += step) {
        yield i
      }
    })
  } else {
    return new Sequence(function* () {
      for(let i = start; i > end; i += step) {
        yield i
      }
    })
  }
}
 
export default range