Skip to content

Instantly share code, notes, and snippets.

@crswll
Created September 22, 2023 13:49
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 crswll/99ff778860fdad1649639cce647e53e7 to your computer and use it in GitHub Desktop.
Save crswll/99ff778860fdad1649639cce647e53e7 to your computer and use it in GitHub Desktop.
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