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 davelnewton/e7459daf0d5390481973ef23f1ef215a to your computer and use it in GitHub Desktop.
Save davelnewton/e7459daf0d5390481973ef23f1ef215a to your computer and use it in GitHub Desktop.
function extractParamStrings(s) {
if (s[0] === '?') s = s.slice(1, s.length)
return s.split('&')
}
const extractParamKeyValPair = paramString =>
paramString.split('=').map(decodeURIComponent)
const addParam = (toObj, [key, val]) => {
toObj[key] = val
return toObj
}
export function fromUrlQuery(str) {
if (!str) return {}
return extractParamStrings(str)
.map(extractParamKeyValPair)
.reduce(addParam, {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment