Last active
April 28, 2019 12:27
-
-
Save grantglidewell/20012eb14f0120ab2dad886e4923c191 to your computer and use it in GitHub Desktop.
query string parsing util
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
<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