Skip to content

Instantly share code, notes, and snippets.

@themgoncalves
Last active June 8, 2021 15:04
Show Gist options
  • Save themgoncalves/c11206adc2232b95c377fa4d8ca0bff3 to your computer and use it in GitHub Desktop.
Save themgoncalves/c11206adc2232b95c377fa4d8ca0bff3 to your computer and use it in GitHub Desktop.
Parsing URL Query String into Object
/**
* Parse Query String
* @param {string} search
* @returns {Object.<string, string>}
*/
const parseQueryString = (search: string): Record<string, string> =>
(search || '')
.replace(/^\?/g, '')
.split('&')
.reduce((acc, query) => {
const [key, value] = query.split('=');
if (key) {
acc[key] = decodeURIComponent(value);
}
return acc;
}, {} as Record<string, string>);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment