Browse Source

Refactored isA, isBool, etc functions to use some of ES5 goodness.

v0.7.4-release
Dmitry Baranovskiy 15 years ago
committed by Ryan Dahl
parent
commit
8ec12339f5
  1. 47
      lib/querystring.js

47
lib/querystring.js

@ -26,10 +26,10 @@ var stack = [];
* @static * @static
*/ */
QueryString.stringify = QueryString.encode = function (obj, sep, eq, munge, name) { QueryString.stringify = QueryString.encode = function (obj, sep, eq, munge, name) {
munge = typeof(munge) == "undefined" ? true : munge; munge = typeof(munge) == "undefined" || munge;
sep = sep || "&"; sep = sep || "&";
eq = eq || "="; eq = eq || "=";
if (isA(obj, null) || isA(obj, undefined) || typeof(obj) === 'function') { if (obj == null || typeof(obj) === 'function') {
return name ? QueryString.escape(name) + eq : ''; return name ? QueryString.escape(name) + eq : '';
} }
@ -75,7 +75,7 @@ QueryString.parse = QueryString.decode = function (qs, sep, eq) {
return (qs || '') return (qs || '')
.split(sep||"&") .split(sep||"&")
.map(pieceParser(eq||"=")) .map(pieceParser(eq||"="))
.reduce(mergeParams) .reduce(mergeParams);
}; };
// Parse a key=val string. // Parse a key=val string.
@ -127,7 +127,7 @@ function mergeParams (params, addition) {
// else merge them as objects, which is a little more complex // else merge them as objects, which is a little more complex
: mergeObjects(params, addition) : mergeObjects(params, addition)
); );
}; }
// Merge two *objects* together. If this is called, we've already ruled // Merge two *objects* together. If this is called, we've already ruled
// out the simple cases, and need to do a loop. // out the simple cases, and need to do a loop.
@ -140,34 +140,21 @@ function mergeObjects (params, addition) {
} }
} }
return params; return params;
}; }
// duck typing
function isA (thing, canon) { function isA (thing, canon) {
return ( // special case for null and undefined
// truthiness. you can feel it in your gut. if (thing == null || canon == null) {
(!thing === !canon) return thing === canon;
// typeof is usually "object" }
&& typeof(thing) === typeof(canon) return Object.getPrototypeOf(Object(thing)) == Object.getPrototypeOf(Object(canon));
// check the constructor }
&& Object.prototype.toString.call(thing) === Object.prototype.toString.call(canon)
);
};
function isBool (thing) { function isBool (thing) {
return ( return isA(thing, true);
typeof(thing) === "boolean" }
|| isA(thing, new Boolean(thing))
);
};
function isNumber (thing) { function isNumber (thing) {
return ( return isA(thing, 0) && isFinite(thing);
typeof(thing) === "number" }
|| isA(thing, new Number(thing))
) && isFinite(thing);
};
function isString (thing) { function isString (thing) {
return ( return isA(thing, "");
typeof(thing) === "string" }
|| isA(thing, new String(thing))
);
};

Loading…
Cancel
Save