Skip to content

Instantly share code, notes, and snippets.

@isaacs
Forked from lsmith/toQueryString.js
Created April 2, 2009 17:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save isaacs/89311 to your computer and use it in GitHub Desktop.
Save isaacs/89311 to your computer and use it in GitHub Desktop.
YUI('lang', function (Y) {
YAP.toQueryString = function (obj, name) {
switch (Y.Lang.type(obj)) {
case 'undefined':
case 'null':
return name ? encodeURIComponent(name) + '=' : '';
case 'boolean': obj = +obj; // intentional fallthrough
case 'number':
case 'string':
return encodeURIComponent(name) + '=' + encodeURIComponent(obj);
case 'array':
var s = [], i, l;
name = name + '[]';
for (i = 0, l = obj.length; i < l; i++) {
s[i] = arguments.callee(obj[i], name);
}
return s.join('&');
case 'object':
var s = [],
begin = name ? name + '[' : '',
end = name ? ']' : '',
key;
for (key in obj) {
// the '___' business is for caja.
if (obj.hasOwnProperty(key) && p.slice(-3) !== '___') {
s.push(arguments.callee(obj[key], begin + key + end));
}
}
return s.join("&");
default: return ''; // functions, regex, errors not acceptable
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment