Skip to content

Instantly share code, notes, and snippets.

@vaseker
Forked from maxvipon/parse_query
Last active August 29, 2015 14:19
Show Gist options
  • Save vaseker/8c0cd166420b807f64de to your computer and use it in GitHub Desktop.
Save vaseker/8c0cd166420b807f64de to your computer and use it in GitHub Desktop.
Добавлена сборка массива параметров. ex: ?param=foo&param=bar -> param: ['foo', 'bar']
var deserializeQuery = function () {
return 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, ' ');
}
} catch (err) {
console.error('Query parameter ' + pair[0] + ' is not correct');
pair = null;
}
return pair;
})
.filter(Boolean)
.reduce(function (prev, next) {
var key = next[0],
value = next[1];
prev[key] = prev[key] ?
prev[key] instanceof Array ? prev[key].concat([value]) : [prev[key], value]
: value;
return prev;
}, {});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment