# All files / src/util query-parse.js

0% Statements 0/17
0% Branches 0/12
0% Functions 0/2
0% Lines 0/17

    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

     /* Pulled from @sindresorhus query-string module and reformatted.
    This is simply to avoid requiring the other methods in the module.

    MIT License © Sindre Sorhus
    */
    export default function(str) {
      if (typeof str !== 'string') {
        return {}
      }

      const str2 = str.trim().replace(/^(\?|#|&)/, '')

      if (!str2) {
        return {}
      }

      return str2.split('&').reduce((ret, param) => {
        const parts = param.replace(/\+/g, ' ').split('=')
        // Firefox (pre 40) decodes `%3D` to `=`
        // https://github.com/sindresorhus/query-string/pull/37
        const key = parts.shift()
        const val = parts.length > 0 ? parts.join('=') : undefined

        const key2 = decodeURIComponent(key)

        // missing `=` should be `null`:
        // http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
        const val2 = val === undefined ? null : decodeURIComponent(val)

        if (!ret.hasOwnProperty(key2)) {
          ret[key2] = val2
        } else if (Array.isArray(ret[key2])) {
          ret[key2].push(val2)
        } else {
          ret[key2] = [ ret[key2], val2 ]
        }

        return ret
      }, {})
    }