Skip to content

Commit 8ec1233

Browse files
DmitryBaranovskiyry
authored andcommitted
Refactored isA, isBool, etc functions to use some of ES5 goodness.
1 parent d3f0493 commit 8ec1233

1 file changed

Lines changed: 17 additions & 30 deletions

File tree

lib/querystring.js

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ var stack = [];
2626
* @static
2727
*/
2828
QueryString.stringify = QueryString.encode = function (obj, sep, eq, munge, name) {
29-
munge = typeof(munge) == "undefined" ? true : munge;
29+
munge = typeof(munge) == "undefined" || munge;
3030
sep = sep || "&";
3131
eq = eq || "=";
32-
if (isA(obj, null) || isA(obj, undefined) || typeof(obj) === 'function') {
32+
if (obj == null || typeof(obj) === 'function') {
3333
return name ? QueryString.escape(name) + eq : '';
3434
}
3535

@@ -75,7 +75,7 @@ QueryString.parse = QueryString.decode = function (qs, sep, eq) {
7575
return (qs || '')
7676
.split(sep||"&")
7777
.map(pieceParser(eq||"="))
78-
.reduce(mergeParams)
78+
.reduce(mergeParams);
7979
};
8080

8181
// Parse a key=val string.
@@ -127,7 +127,7 @@ function mergeParams (params, addition) {
127127
// else merge them as objects, which is a little more complex
128128
: mergeObjects(params, addition)
129129
);
130-
};
130+
}
131131

132132
// Merge two *objects* together. If this is called, we've already ruled
133133
// out the simple cases, and need to do a loop.
@@ -140,34 +140,21 @@ function mergeObjects (params, addition) {
140140
}
141141
}
142142
return params;
143-
};
143+
}
144144

145-
// duck typing
146145
function isA (thing, canon) {
147-
return (
148-
// truthiness. you can feel it in your gut.
149-
(!thing === !canon)
150-
// typeof is usually "object"
151-
&& typeof(thing) === typeof(canon)
152-
// check the constructor
153-
&& Object.prototype.toString.call(thing) === Object.prototype.toString.call(canon)
154-
);
155-
};
146+
// special case for null and undefined
147+
if (thing == null || canon == null) {
148+
return thing === canon;
149+
}
150+
return Object.getPrototypeOf(Object(thing)) == Object.getPrototypeOf(Object(canon));
151+
}
156152
function isBool (thing) {
157-
return (
158-
typeof(thing) === "boolean"
159-
|| isA(thing, new Boolean(thing))
160-
);
161-
};
153+
return isA(thing, true);
154+
}
162155
function isNumber (thing) {
163-
return (
164-
typeof(thing) === "number"
165-
|| isA(thing, new Number(thing))
166-
) && isFinite(thing);
167-
};
156+
return isA(thing, 0) && isFinite(thing);
157+
}
168158
function isString (thing) {
169-
return (
170-
typeof(thing) === "string"
171-
|| isA(thing, new String(thing))
172-
);
173-
};
159+
return isA(thing, "");
160+
}

0 commit comments

Comments
 (0)