Created
December 20, 2017 19:27
-
-
Save nick-brady/38269e57952393a042bdb3b95f726f21 to your computer and use it in GitHub Desktop.
query string parser
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
/** | |
* modified code of gist - https://gist.github.com/Manc/9409355 | |
* - newer syntax | |
* - decodeURIComponent instead of generic decoding function (unescape) | |
* keys with name name defined after will take priority | |
*/ | |
export function parseQueryString(location) { | |
const query = decodeURIComponent(location).trim(); | |
const obj = {}; | |
const qPos = query.indexOf('?'); | |
const tokens = query.substr(qPos + 1).split('&'); | |
if (qPos !== -1 || query.indexOf('=') !== -1) { | |
tokens.forEach((token) => { | |
const [key, value] = token.split('='); | |
obj[key] = value; | |
}) | |
} | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment