# All files / internalHelpers _pxto.js

100% Statements 17/17
100% Branches 13/13
100% Functions 2/2
100% Lines 16/16

    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

    3x
    12x
    12x
    12x
    8x
    1x

    7x

    11x
    10x
    1x

    9x

    10x
    1x

    9x
    1x

    8x

     // @flow

    import endsWith from '../internalHelpers/_endsWith'
    import stripUnit from '../helpers/stripUnit'

    /**
     * Factory function that creates pixel-to-x converters
     * @private
     */
    const pxtoFactory = (to: string) => (pxval: string|number, base?: string|number = '16px') => {
      let newPxval = pxval
      let newBase = base
      if (typeof pxval === 'string') {
        if (!endsWith(pxval, 'px')) {
          throw new Error(`Expected a string ending in "px" or a number passed as the first argument to ${to}(), got "${pxval}" instead.`)
        }
        newPxval = stripUnit(pxval)
      }

      if (typeof base === 'string') {
        if (!endsWith(base, 'px')) {
          throw new Error(`Expected a string ending in "px" or a number passed as the second argument to ${to}(), got "${base}" instead.`)
        }
        newBase = stripUnit(base)
      }

      if (typeof newPxval === 'string') {
        throw new Error(`Passed invalid pixel value ("${pxval}") to ${to}(), please pass a value like "12px" or 12.`)
      }

      if (typeof newBase === 'string') {
        throw new Error(`Passed invalid base value ("${base}") to ${to}(), please pass a value like "12px" or 12.`)
      }

      return `${newPxval / newBase}${to}`
    }

    export default pxtoFactory