Better Underscore isType checking, in the presence of Internet Explorer host objects, which are a bit touchy.

This commit is contained in:
Jeremy Ashkenas
2010-12-01 10:25:55 -05:00
parent 00237103fc
commit 6b8bb0cacd
3 changed files with 24 additions and 11 deletions

View File

@@ -76,7 +76,7 @@ $(document).ready(function() {
setTimeout(throttledIncr, 70);
setTimeout(throttledIncr, 110);
setTimeout(throttledIncr, 120);
_.delay(function(){ ok(counter == 3, "incr was throttled"); start(); }, 180);
_.delay(function(){ ok(counter == 3, "incr was throttled"); start(); }, 200);
});
asyncTest("functions: debounce", 1, function() {

View File

@@ -86,7 +86,7 @@ $(document).ready(function() {
parent.iRegExp = /hi/;\
parent.iNaN = NaN;\
parent.iNull = null;\
parent.iBoolean = false;\
parent.iBoolean = false;\
parent.iUndefined = undefined;\
</script>"
);
@@ -125,7 +125,7 @@ $(document).ready(function() {
ok(!_.isNumber(arguments), 'the arguments object is not a number');
ok(!_.isNumber(undefined), 'undefined is not a number');
ok(_.isNumber(3 * 4 - 7 / 10), 'but numbers are');
ok(_.isNumber(NaN), 'NaN is a number');
ok(!_.isNumber(NaN), 'NaN is not a number');
ok(_.isNumber(Infinity), 'Infinity is a number');
ok(_.isNumber(iNumber), 'even from another frame');
});
@@ -183,11 +183,24 @@ $(document).ready(function() {
ok(!_.isUndefined(1), 'numbers are defined');
ok(!_.isUndefined(null), 'null is defined');
ok(!_.isUndefined(false), 'false is defined');
ok(!_.isUndefined(NaN), 'NaN is defined');
ok(_.isUndefined(), 'nothing is undefined');
ok(_.isUndefined(undefined), 'undefined is undefined');
ok(_.isUndefined(iUndefined), 'even from another frame');
});
if (window.ActiveXObject) {
test("objects: IE host objects", function() {
var xml = new ActiveXObject("Msxml2.DOMDocument.3.0");
ok(!_.isNumber(xml));
ok(!_.isBoolean(xml));
ok(!_.isNaN(xml));
ok(!_.isFunction(xml));
ok(!_.isNull(xml));
ok(!_.isUndefined(xml));
});
}
test("objects: tap", function() {
var intercepted = null;
var interceptor = function(obj) { intercepted = obj; };

View File

@@ -588,7 +588,13 @@
// Is a given value a number?
_.isNumber = function(obj) {
return (obj === +obj) || (toString.call(obj) === '[object Number]');
return !!(obj === 0 || (obj && obj.toExponential && obj.toFixed));
};
// Is the given value NaN -- this one is interesting. NaN != NaN, and
// isNaN(undefined) == true, so we make sure it's a number first.
_.isNaN = function(obj) {
return toString.call(obj) === '[object Number]' && isNaN(obj);
};
// Is a given value a boolean?
@@ -606,12 +612,6 @@
return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false));
};
// Is the given value NaN -- this one is interesting. NaN != NaN, and
// isNaN(undefined) == true, so we make sure it's a number first.
_.isNaN = function(obj) {
return _.isNumber(obj) && isNaN(obj);
};
// Is a given value equal to null?
_.isNull = function(obj) {
return obj === null;
@@ -619,7 +619,7 @@
// Is a given variable undefined?
_.isUndefined = function(obj) {
return typeof obj == 'undefined';
return obj === void 0;
};
// Utility Functions