mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
querystring.stringify is now more solid
* handles NaN and Infinity * works with arrays from other contexts
This commit is contained in:
@@ -29,21 +29,22 @@ QueryString.stringify = QueryString.encode = function (obj, sep, eq, munge, name
|
||||
munge = typeof munge == "undefined" || munge;
|
||||
sep = sep || "&";
|
||||
eq = eq || "=";
|
||||
if (obj == null || typeof obj == "function") {
|
||||
var type = Object.prototype.toString.call(obj);
|
||||
if (obj == null || type == "[object Function]" || type == "[object Number]" && !isFinite(obj)) {
|
||||
return name ? QueryString.escape(name) + eq : "";
|
||||
}
|
||||
|
||||
if (isBool(obj)) {
|
||||
obj = +obj;
|
||||
}
|
||||
if (isNumber(obj) || isString(obj)) {
|
||||
return QueryString.escape(name) + eq + QueryString.escape(obj);
|
||||
}
|
||||
if (isA(obj, [])) {
|
||||
name = name + (munge ? "[]" : "");
|
||||
return obj.map(function (item) {
|
||||
return QueryString.stringify(item, sep, eq, munge, name);
|
||||
}).join(sep);
|
||||
switch (type) {
|
||||
case '[object Boolean]':
|
||||
obj = +obj; // fall through
|
||||
case '[object Number]':
|
||||
case '[object String]':
|
||||
return QueryString.escape(name) + eq + QueryString.escape(obj);
|
||||
case '[object Array]':
|
||||
name = name + (munge ? "[]" : "");
|
||||
return obj.map(function (item) {
|
||||
return QueryString.stringify(item, sep, eq, munge, name);
|
||||
}).join(sep);
|
||||
}
|
||||
// now we know it's an object.
|
||||
|
||||
@@ -109,20 +110,3 @@ QueryString.parse = QueryString.decode = function (qs, sep, eq) {
|
||||
});
|
||||
return obj;
|
||||
};
|
||||
|
||||
function isA (thing, canon) {
|
||||
// special case for null and undefined
|
||||
if (thing == null || canon == null) {
|
||||
return thing === canon;
|
||||
}
|
||||
return Object.getPrototypeOf(Object(thing)) == Object.getPrototypeOf(Object(canon));
|
||||
}
|
||||
function isBool (thing) {
|
||||
return isA(thing, true);
|
||||
}
|
||||
function isNumber (thing) {
|
||||
return isA(thing, 0) && isFinite(thing);
|
||||
}
|
||||
function isString (thing) {
|
||||
return isA(thing, "");
|
||||
}
|
||||
Reference in New Issue
Block a user