Created
October 16, 2019 19:25
-
-
Save victornpb/10a8bcdb581dd25481d783da13df9c16 to your computer and use it in GitHub Desktop.
Simple query string parser
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
/* | |
Example: | |
parseQueryString('?foo=hi&bar=World%20Hello') | |
{ | |
foo: "hi", | |
bar: "Hello World" | |
} | |
*/ | |
/** | |
* parseQueryString Very simplified querystring parser | |
* @param {string} [queryString] Query string to be parsed. Defaults to current location.search | |
* @return {object} returns a key:value object | |
* @author Victor N <victornunes@lett.digital> | |
*/ | |
function parseQueryString(queryString) { | |
var qs = queryString || window.location.search; | |
var params = {}; | |
qs.replace(/^\?/, '').split('&').forEach(text => { | |
var p = text.split('='); | |
var key = decodeURIComponent(p[0]); | |
var value = decodeURIComponent(p[1]); | |
if(key) params[key] = value; | |
}); | |
return params; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment