# All files / utils range.ts

29.63% Statements 16/54
0% Branches 0/47
16.67% Functions 1/6
30.19% Lines 16/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    821x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

    1x

    1x

    1x
    1x

    1x
     import {isSymbol} from './isSymbol'

    const INFINITY = 1 / 0
    const MAX_INTEGER = 1.7976931348623157e308
    const NotANumber = 0 / 0
    const reTrim = /^\s+|\s+$/g
    const reIsBadHex = /^[-+]0x[0-9a-f]+$/i
    const reIsBinary = /^0b[01]+$/i
    const reIsOctal = /^0o[0-7]+$/i
    const freeParseInt = parseInt

    const isObject = (value: any): boolean => {
      const type = typeof value
      return value != null && (type === 'object' || type === 'function')
    }

    const toNumber = (value: any): number => {
      if (typeof value === 'number') {
        return value
      }
      if (isSymbol(value)) {
        return NotANumber
      }
      if (isObject(value)) {
        const other = typeof value.valueOf === 'function' ? value.valueOf() : value
        value = isObject(other) ? `${other}` : other
      }
      if (typeof value !== 'string') {
        return value === 0 ? value : +value
      }
      value = value.replace(reTrim, '')
      const isBinary = reIsBinary.test(value)
      return isBinary || reIsOctal.test(value)
        ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
        : reIsBadHex.test(value)
        ? NotANumber
        : +value
    }

    const toFinite = (value: any): number => {
      if (!value) {
        return value === 0 ? value : 0
      }
      value = toNumber(value)
      if (value === INFINITY || value === -INFINITY) {
        const sign = value < 0 ? -1 : 1
        return sign * MAX_INTEGER
      }
      return value === value ? value : 0
    }

    const baseRange = (start: number, end: number, step: number): Array<number> => {
      let index = 0
      let length = Math.max(Math.ceil((end - start) / (step || 1)), 0)
      const result = new Array(length)

      while (length) {
        length -= 1
        result[index] = start
        index += 1
        start += step
      }
      return result
    }

    const createRange = () => {
      return (start: number, end: number, step = 1) => {
        // Ensure the sign of `-0` is preserved.
        start = toFinite(start)
        if (end === undefined) {
          end = start
          start = 0
        } else {
          end = toFinite(end)
        }
        step = step === undefined ? (start < end ? 1 : -1) : toFinite(step)
        return baseRange(start, end, step)
      }
    }

    export const range = createRange()