All files toQueryString.js

100% Statements 5/5
80% Branches 4/5
100% Functions 3/3
100% Lines 4/4

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            20x 23x 15x 15x              
import sort from 'lodash/sortBy'
 
/**
 * @param {object} o
 * @param {string} prefix
 */
const toQueryString = (o = {}, prefix) => {
  return sort(Object.entries(o), (e) => e[0]).map(([prop, value]) => {
    const key = (prefix) ? `${prefix}[${prop}]` : prop
    return (typeof value === 'object')
      ? toQueryString(value, key)
      : `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
  }).join('&')
}
 
export default toQueryString