Skip to content

Instantly share code, notes, and snippets.

@jmarmolejos
Created January 12, 2018 13:19
Show Gist options
  • Save jmarmolejos/a2e46b6ffcdc484706c478f018386bbd to your computer and use it in GitHub Desktop.
Save jmarmolejos/a2e46b6ffcdc484706c478f018386bbd to your computer and use it in GitHub Desktop.
const buildUrl = function(url, params) {
let _url = url
if (url.indexOf('?') === -1) {
_url = url + '?'
}
let queries = []
Object.keys(params).forEach(function(key) {
queries.push(`${key}=${params[key]}`)
})
return _url + queries.join('&')
}
console.log(buildUrl('/booking' ,{ foo: 'bar', bar: 'baz' })) // returns /booking?foo=bar&bar=baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment