Skip to content

Make parseSearch and serializeSearch compliant with the WHATWG specification #9804

@glowcloud

Description

@glowcloud

Right now the parseSearch and serializeSearch functions in utils use custom string parsing:

export const parseSearch = () => {
let map = {}
let search = win.location.search
if(!search)
return {}
if ( search != "" ) {
let params = search.substr(1).split("&")
for (let i in params) {
if (!Object.prototype.hasOwnProperty.call(params, i)) {
continue
}
i = params[i].split("=")
map[decodeURIComponent(i[0])] = (i[1] && decodeURIComponent(i[1])) || ""
}
}
return map
}
export const serializeSearch = (searchMap) => {
return Object.keys(searchMap).map(k => {
return encodeURIComponent(k) + "=" + encodeURIComponent(searchMap[k])
}).join("&")
}

Instead, we should move to using URLSearchParams which will make these functions compliant with the WHATWG specification.

The functions should be rewritten into:

export const parseSearch = () => {
  const searchParams = new URLSearchParams(win.location.search)
  return Object.fromEntries(searchParams)
}

export const serializeSearch = (searchMap) => {
  const searchParams = new URLSearchParams(Object.entries(searchMap))
  return String(searchParams)
}

Based on: #9722 (comment)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions