Last active
June 8, 2021 15:04
-
-
Save themgoncalves/c11206adc2232b95c377fa4d8ca0bff3 to your computer and use it in GitHub Desktop.
Parsing URL Query String into Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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