Created
September 14, 2015 19:50
-
-
Save mponizil/b77c637308c53f910fc6 to your computer and use it in GitHub Desktop.
parse/build querystring
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
function parseQuery(qs) { | |
return qs.replace('?', '').split('&').reduce(function(obj, pair) { | |
var i = pair.indexOf('=') | |
, key = pair.slice(0, i) | |
, val = pair.slice(++i); | |
obj[key] = decodeURIComponent(val); | |
return obj; | |
}, {}); | |
} | |
function buildQuery(url, obj) { | |
var result = url || ''; | |
if (result.indexOf('?') > -1) result += '&'; | |
else result += '?'; | |
var pairs = []; | |
for (key in obj) pairs.push(key + '=' + obj[key]); | |
result += pairs.join('&'); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment