Skip to content

Instantly share code, notes, and snippets.

@grantglidewell
Last active April 28, 2019 12:27
Show Gist options
  • Save grantglidewell/20012eb14f0120ab2dad886e4923c191 to your computer and use it in GitHub Desktop.
Save grantglidewell/20012eb14f0120ab2dad886e4923c191 to your computer and use it in GitHub Desktop.
query string parsing util
<div>
<pre>
export function parseUrl(url) {
if (typeof url !== "string") {
console.error(`Cannot parse url of type ${typeof url}`);
return false
}
if(url.split("?").length === 1){
// console.log(`No query params are present`);
return false
}
const noUrl = url.split("?")[1]
const keyValue = noUrl.split('&').map(kvPair => {
return { [kvPair.split("=")[0]]: kvPair.split("=")[1] };
});
return Object.assign({}, ...keyValue)
}
// takes a url and returns a string with the query params (for react router)
export function rawQS(url) {
if (typeof url !== "string") {
return ''
}
if(url.split("?").length === 1){
return ''
}
return url.split('?')[1]
}
</pre>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment