Created
April 23, 2015 07:33
-
-
Save maxvipon/780f7a94d2bbb006bb42 to your computer and use it in GitHub Desktop.
Deserialize URL query params
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
var args = location.search.substr(1) | |
.split('&') | |
.map(function (pair) { | |
pair = pair.split('='); | |
try { | |
pair[1] = pair[1] !== undefined ? decodeURIComponent(pair[1]) : true; | |
if (typeof pair[1] === 'string') { | |
pair[1] = pair[1].replace(/\+/g, ' '); | |
if (pair[1].indexOf(',') !== -1) { | |
pair[1] = pair[1].split(','); | |
} | |
} | |
} catch (err) { | |
console.error('Query parameter ' + pair[0] + ' is not correct'); | |
pair = null; | |
} | |
return pair; | |
}) | |
.filter(Boolean) | |
.reduce(function (prev, next) { | |
prev[next[0]] = next[1]; | |
return prev; | |
}, {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment