Skip to content

Instantly share code, notes, and snippets.

@selbekk
Last active August 10, 2016 10:46
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 selbekk/3b00775c2b99cfd06c83bd560fda9ff3 to your computer and use it in GitHub Desktop.
Save selbekk/3b00775c2b99cfd06c83bd560fda9ff3 to your computer and use it in GitHub Desktop.
Simple query string parsing
export function parse(input) {
if (!input || typeof input !== 'string') {
return input;
}
const qs = input.startsWith('?') ? input.substring(1) : input;
return qs.split('&')
.filter(item => item)
.reduce((prev, item) => {
const [key, value] = item.split('=');
const next = { ...prev };
next[key] = value;
return next;
}, {});
}
export function stringify(obj) {
if(!obj || typeof obj !== 'object') {
return obj;
}
return Object.keys(obj)
.map(key => `${key}=${obj[key]}`)
.join('&');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment