Skip to content

Instantly share code, notes, and snippets.

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 Dinir/286e801a5c38135649d465d60106699d to your computer and use it in GitHub Desktop.
Save Dinir/286e801a5c38135649d465d60106699d to your computer and use it in GitHub Desktop.
Parses a query parameter string into a javascript map. This script deals with multiple values by placing them into an array.

Get query map

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&parameters'
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()
/**
* Get a query map based on a query string.
*
* The function will populate a map variable with key value pairs of the parameters.
*
* 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.
*
* Forked from {@link https://gist.github.com/MatthewDaniels/388fa1e0c02613f103f00a504ed58c55 MatthewDaniels/parse-query-parameters-into-a-map.js}.
*
* @param {?string} [query=window.location.search] The query string - the question mark is optional
* @return {map} key value pairs of the parameter / value of the parameter
*/
const getQueryMap = (query = window.location.search) => {
if (typeof query !== 'string') {
return null
}
const toType = v =>
({}).toString.call(v).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
const map = new Map()
query.replace(/([^&|\?=]+)=?([^&]*)(?:&+|$)/g, (_match, key, value) => {
if (map.has(key)) {
// the key already exists, so we need to check if it is an array,
// if not, make it an array and add the new value
if (toType(map.get(key)) !== 'array') {
// it's not an array - make it an array
map.set(key, [map.get(key)])
}
// push the new value into the array
map.get(key).push(value)
} else {
// put the value into the map
map.set(key, value)
}
})
return map
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment