Forked from nayeemzen/functional-query-string-parser.ts
Created
October 23, 2018 18:42
-
-
Save mohammedri/fe0c5461adc8f696a70debd1b1aa9acc to your computer and use it in GitHub Desktop.
Functional programming approach to parsing query strings in Typescript/ES6
This file contains 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
// Functional programming approach to parsing query strings in Typescript/ES6. | |
function parseQueryString(search: string) { | |
return (search.startsWith('?') ? search.substring(1) : search) | |
.split('&') | |
.map(str => { | |
const eqIdx = str.indexOf('='); | |
if (eqIdx <= 0 || eqIdx >= str.length - 1) { | |
return {}; | |
} | |
const key = str.substring(0, eqIdx); | |
const value = str.substring(eqIdx + 1, str.length); | |
return {[key]: value}; | |
}) | |
.reduce((prev, next) => ({...prev, ...next})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment