Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
query string parser
/**
* 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
You can’t perform that action at this time.