Skip to content

Instantly share code, notes, and snippets.

@nick-brady
Created December 20, 2017 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nick-brady/38269e57952393a042bdb3b95f726f21 to your computer and use it in GitHub Desktop.
Save nick-brady/38269e57952393a042bdb3b95f726f21 to your computer and use it in GitHub Desktop.
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