Skip to content

Instantly share code, notes, and snippets.

@THEtheChad
Last active December 21, 2016 22:45
Show Gist options
  • Save THEtheChad/09b0a4808c0f7a15016c to your computer and use it in GitHub Desktop.
Save THEtheChad/09b0a4808c0f7a15016c to your computer and use it in GitHub Desktop.
Correctly Parse Query String
function parseQuery(q) {
var dec = window.decodeURIComponent;
var hash = {};
//if `q` is `null` or `undefined`, there are no keys
if (q == null) {
return hash;
}
//if the string starts with a `?` character, strip it off
if (/^\?/.test(q)) {
q = q.substr(1);
}
//if the string is empty, there are no keys
if(q === ''){
return hash;
}
//split the string on key-value pair separators
//`&` and `;` are used to separate key-value pairs
q = q.split(/[&;]/g);
//iterate through each key-value pair
for (i = 0; i < q.length; i += 1) {
var key, value, data = q[i];
//split the key-value pair on the first key-value separator
//`=` is used to separate keys from values
var index = data.indexOf('=');
if (index < 0) {
//if no `=` is present
// - the entire key-value pair is used as the key
// - the value is null
key = dec(data);
value = null;
} else {
//if `=` is present
// - rawKey is the part of the string before the `=`
// - rawValue is the part of the string after the `=`
var rawKey = data.slice(0, index);
var rawValue = data.slice(index + 1);
key = dec(rawKey);
value = dec(rawValue);
}
//check if the key is present in the hash
if (hash.hasOwnProperty(key)) {
//if it is, add the new value to the collection of values
hash[key].push(value);
} else {
//if it isn't, create a new collection of values
hash[key] = [value];
}
}
return hash;
}
function parseQuery(a){var b=window.decodeURIComponent,c={};if(null==a)return c;if(/^\?/.test(a)&&(a=a.substr(1)),""===a)return c;for(a=a.split(/[&;]/g),i=0;i<a.length;i+=1){var d,e,f=a[i],g=f.indexOf("=");if(g<0)d=b(f),e=null;else{var h=f.slice(0,g),j=f.slice(g+1);d=b(h),e=b(j)}c.hasOwnProperty(d)?c[d].push(e):c[d]=[e]}return c}
(function(win){
var dec = win.decodeURIComponent;
var parser = document.createElement('a');
function Uniform(){}
var u = Uniform.prototype = {};
u.parseQuery = function(q) {
var hash = {};
var collector = {};
//if `q` is `null` or `undefined`, there are no keys
if (q == null) {
return hash;
}
//if the string starts with a `?` character, strip it off
if (/^\?/.test(q)) {
q = q.substr(1);
}
//if the string is empty, there are no keys
if(q === ''){
return hash;
}
//split the string on key-value pair separators
//`&` and `;` are used to separate key-value pairs
q = q.split(/[&;]/g);
//iterate through each key-value pair
for (i = 0; i < q.length; i += 1) {
var key, value, data = q[i];
//split the key-value pair on the first key-value separator
//`=` is used to separate keys from values
var index = data.indexOf('=');
if (index < 0) {
//if no `=` is present
// - the entire key-value pair is used as the key
// - the value is null
key = dec(data);
value = null;
} else {
//if `=` is present
// - rawKey is the part of the string before the `=`
// - rawValue is the part of the string after the `=`
var rawKey = data.slice(0, index);
var rawValue = data.slice(index + 1);
key = dec(rawKey);
value = dec(rawValue);
}
(collector.hasOwnProperty(key)) ? collector.push(value) : (collector[key] = [value]);
hash[key] = (hash.hasOwnProperty(key)) ? collector[key] : value;
}
return hash;
};
u.parse = function(url){
parser.href = url;
return parser;
};
u.new = function(opts){return new Uniform(opts)};
win.uniform = new Uniform();
})(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment