Methods for dealing with URI query strings.
String.implement({
parseQueryString: function(){
var vars = this.split(/[&;]/), res = {};
if (vars.length){
vars.each(function(val){
var index = val.indexOf('='),
keys = index < 0 ? [''] : val.substr(0, index).match(/[^\]\[]+/g),
value = decodeURIComponent(val.substr(index + 1)),
obj = res;
keys.each(function(key, i){
var current = obj[key];
if (i < keys.length - 1){
obj = obj[key] = current || {};
} else if (typeOf(current) == 'array'){
current.push(value);
} else {
obj[key] = current != null ? [current, value] : value;
}
});
});
}
return res;
},
cleanQueryString: function(method){
return this.split('&').filter(function(val){
var index = val.indexOf('='),
key = index < 0 ? '' : val.substr(0, index),
value = val.substr(index + 1);
return method ? method.run([key, value]) : (value || value === 0);
}).join('&');
}
});