Created
September 5, 2017 16:59
-
-
Save montmanu/e577756017293a8960520027333c8cca to your computer and use it in GitHub Desktop.
naive query string parser
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 parseQueryString(qs) { | |
return ( | |
qs.slice(1).split('&') | |
.map((pair) => { | |
const [key, value] = pair.split('='); | |
let transformedValue = decodeURIComponent(value); | |
if (/^[{|\]]/.test(transformedValue)) { | |
transformedValue = JSON.parse(transformedValue); | |
} | |
return { key: key, value: transformedValue }; | |
}) | |
.reduce((prev, curr) => { | |
const { key, value } = curr; | |
const next = {}; | |
next[key] = value; | |
return Object.assign({}, prev, next); | |
}, {}) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment