From: Dmitry Baranovskiy Date: Mon, 28 Jun 2010 01:57:11 +0000 (+1000) Subject: Refactored isA, isBool, etc functions to use some of ES5 goodness. X-Git-Tag: v0.1.100~18 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8ec12339f556e5ccf7172abf7397e349fa78c37b;p=platform%2Fupstream%2Fnodejs.git Refactored isA, isBool, etc functions to use some of ES5 goodness. --- diff --git a/lib/querystring.js b/lib/querystring.js index eb415bc..e3b75ba 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -26,10 +26,10 @@ var stack = []; * @static */ QueryString.stringify = QueryString.encode = function (obj, sep, eq, munge, name) { - munge = typeof(munge) == "undefined" ? true : munge; + munge = typeof(munge) == "undefined" || munge; sep = sep || "&"; eq = eq || "="; - if (isA(obj, null) || isA(obj, undefined) || typeof(obj) === 'function') { + if (obj == null || typeof(obj) === 'function') { return name ? QueryString.escape(name) + eq : ''; } @@ -75,7 +75,7 @@ QueryString.parse = QueryString.decode = function (qs, sep, eq) { return (qs || '') .split(sep||"&") .map(pieceParser(eq||"=")) - .reduce(mergeParams) + .reduce(mergeParams); }; // Parse a key=val string. @@ -127,7 +127,7 @@ function mergeParams (params, addition) { // else merge them as objects, which is a little more complex : mergeObjects(params, addition) ); -}; +} // Merge two *objects* together. If this is called, we've already ruled // out the simple cases, and need to do a loop. @@ -140,34 +140,21 @@ function mergeObjects (params, addition) { } } return params; -}; +} -// duck typing function isA (thing, canon) { - return ( - // truthiness. you can feel it in your gut. - (!thing === !canon) - // typeof is usually "object" - && typeof(thing) === typeof(canon) - // check the constructor - && Object.prototype.toString.call(thing) === Object.prototype.toString.call(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 ( - typeof(thing) === "boolean" - || isA(thing, new Boolean(thing)) - ); -}; + return isA(thing, true); +} function isNumber (thing) { - return ( - typeof(thing) === "number" - || isA(thing, new Number(thing)) - ) && isFinite(thing); -}; + return isA(thing, 0) && isFinite(thing); +} function isString (thing) { - return ( - typeof(thing) === "string" - || isA(thing, new String(thing)) - ); -}; + return isA(thing, ""); +}