Created
July 17, 2020 19:27
-
-
Save davelnewton/e7459daf0d5390481973ef23f1ef215a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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