diff options
author | Jan Kassens <jan@kassens.net> | 2010-07-18 22:16:43 +0200 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2010-07-19 10:53:32 -0700 |
commit | bb2acd5e757cd37a94dc7f3bfab134a49963eb24 (patch) | |
tree | bd79d7851a36214c9bace524d0565aef6903fca1 /lib | |
parent | eda1edd07f03dea04d30445308813af273b57454 (diff) | |
download | node-new-bb2acd5e757cd37a94dc7f3bfab134a49963eb24.tar.gz |
querystring.stringify is now more solid
* handles NaN and Infinity
* works with arrays from other contexts
Diffstat (limited to 'lib')
-rw-r--r-- | lib/querystring.js | 42 |
1 files changed, 13 insertions, 29 deletions
diff --git a/lib/querystring.js b/lib/querystring.js index efd2969229..f48ee5802f 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -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, ""); -}
\ No newline at end of file |