Created
September 22, 2023 13:49
-
-
Save crswll/99ff778860fdad1649639cce647e53e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const searchParamsToObject = (params: URLSearchParams) => { | |
const searchParamsAsObject: Record<string, string | string[]> = {} | |
Array.from(params.entries()).forEach(([key, value]) => { | |
const existingValue = searchParamsAsObject[key] | |
if (existingValue) { | |
searchParamsAsObject[key] = Array.isArray(existingValue) | |
? [...existingValue, value] | |
: [existingValue, value] | |
} else { | |
searchParamsAsObject[key] = value | |
} | |
}) | |
return searchParamsAsObject | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment