This processes a query string into a map object for easier usage.
The query string can contain a ? or not.
If there is more than one of the same key, the function will populate an array in the map with the multiple values within it.
Usage:
const query = '?some=string&of=query¶meters'
const queryMap = getQueryMap(query)
console.log(queryMap.get('some')) // 'string'
console.log(queryMap.get('of')) // 'query'
console.log(queryMap.get('parameters')) // ''
The queryMap
variable in the above will resolve to:
{
"some" => "string",
"of" => "query",
"parameters" => ""
}
A common use case would be to send in the url parameters:
// uses `window.location.search` when argument is not given
const queryMap = getQueryMap()