Created
June 28, 2010 11:59
-
-
Save rwz/455752 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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