Skip to content

Instantly share code, notes, and snippets.

@montmanu
Created September 5, 2017 16:59
Show Gist options
  • Save montmanu/e577756017293a8960520027333c8cca to your computer and use it in GitHub Desktop.
Save montmanu/e577756017293a8960520027333c8cca to your computer and use it in GitHub Desktop.
naive query string parser
function parseQueryString(qs) {
return (
qs.slice(1).split('&')
.map((pair) => {
const [key, value] = pair.split('=');
let transformedValue = decodeURIComponent(value);
if (/^[{|\]]/.test(transformedValue)) {
transformedValue = JSON.parse(transformedValue);
}
return { key: key, value: transformedValue };
})
.reduce((prev, curr) => {
const { key, value } = curr;
const next = {};
next[key] = value;
return Object.assign({}, prev, next);
}, {})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment