Created
July 9, 2018 18:46
-
-
Save blrobin2/42c3786605ee9df894bf77b72d3372e5 to your computer and use it in GitHub Desktop.
URL string building helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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