Skip to content

Instantly share code, notes, and snippets.

@blrobin2
Created July 9, 2018 18:46
Show Gist options
  • Save blrobin2/42c3786605ee9df894bf77b72d3372e5 to your computer and use it in GitHub Desktop.
Save blrobin2/42c3786605ee9df894bf77b72d3372e5 to your computer and use it in GitHub Desktop.
URL string building helpers
function createUrl(base, paramObj) {
return [base, createUrlParams(paramObj)].join(base.includes('?') ? '&' : '?')
}
function createUrlParams(paraObj) {
return Object.entries(paramObj).map(([key, val]) => `${key}=${val}`).join('&')
}
// Sample usage
const url = createUrl('http://localhost:3000', { redirect: true })
console.log(url) // http://localhost:3000?redirect=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment