-
-
Save vaseker/8c0cd166420b807f64de to your computer and use it in GitHub Desktop.
Добавлена сборка массива параметров. ex: ?param=foo¶m=bar -> param: ['foo', 'bar']
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 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