Skip to content

Instantly share code, notes, and snippets.

@rwz
Created June 28, 2010 11:59
Show Gist options
  • Save rwz/455752 to your computer and use it in GitHub Desktop.
Save rwz/455752 to your computer and use it in GitHub Desktop.
// Prototype toQueryString fix
(function(){
'use strict';
function typeOf(obj) {
if (obj instanceof Date) {
return 'date';
} else if (Object.isArray(obj)) {
return 'array';
} else return typeof obj;
}
function queryPair(key, value){
switch(typeOf(value)) {
case 'object': return $H(value).toQueryString(key);
case 'array': return value.map(function(val){ return queryPair(key+'[]', val); }).join('&');
default: return key+'='+encodeURIComponent(String.interpret(value));
}
}
Hash.prototype._toQueryString = Hash.prototype.toQueryString;
Hash.prototype.toQueryString = function(base){
return this.map(function(pair){
var key = pair.key, value = pair.value;
if (base) { key = base+'['+key+']'; }
if (value != undefined) { return queryPair(key, value); }
}).compact().join('&');
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment