Last active
August 13, 2018 18:08
-
-
Save L1fescape/1b005b498b3178613a2d54eaea044ccc to your computer and use it in GitHub Desktop.
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
// use query-string package? | |
type QueryMap = {[key: string]: string}; | |
const parseQuery = (queryString: string, param: string): string => { | |
if (queryString.charAt(0) === '?') { | |
queryString = queryString.substr(1); | |
} | |
const parts = queryString.split('&'); | |
const partsMap = parts.reduce((acc, part) => { | |
const splitIndex = part.indexOf('='); | |
if (splitIndex === -1) { | |
return acc; | |
} | |
const queryKey = part.substring(0, splitIndex); | |
const queryVal = part.substring(splitIndex + 1); | |
acc[queryKey] = decodeURIComponent(queryVal); | |
return acc; | |
}, {} as QueryMap); | |
return partsMap[param] ? partsMap[param] : ''; | |
}; | |
// navigate to http://[domain]?term=counter%20strike&device=ios | |
console.log(parseQuery(window.location.search, 'term')) | |
// => 'counter strike' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment