From 221161ceccd224f9b0f871426ddd656f358a2f5b Mon Sep 17 00:00:00 2001 From: Michael Ficarra Date: Tue, 4 Oct 2011 22:24:36 -0400 Subject: [PATCH] better isXXX checks; the current tests have too many false positives --- test/objects.js | 5 +++-- underscore.js | 17 +++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/test/objects.js b/test/objects.js index e05c0ddc5..0e1f3e4f3 100644 --- a/test/objects.js +++ b/test/objects.js @@ -383,14 +383,15 @@ $(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 not a number'); + ok(_.isNumber(NaN), 'NaN *is* a number'); ok(_.isNumber(Infinity), 'Infinity is a number'); ok(_.isNumber(iNumber), 'even from another frame'); + ok(!_.isNumber('1'), 'numeric strings are not numbers'); }); test("objects: isBoolean", function() { ok(!_.isBoolean(2), 'a number is not a boolean'); - ok(!_.isBoolean("string"), 'a string is not a boolean'); + ok(!_.isBoolean("string"), 'a string is not a boolean'); ok(!_.isBoolean("false"), 'the string "false" is not a boolean'); ok(!_.isBoolean("true"), 'the string "true" is not a boolean'); ok(!_.isBoolean(arguments), 'the arguments object is not a boolean'); diff --git a/underscore.js b/underscore.js index 0db66d0b6..182070eb3 100644 --- a/underscore.js +++ b/underscore.js @@ -714,7 +714,8 @@ return eq(a, b, []); }; - // Is a given array or object empty? + // Is a given array, string, or object empty? + // An "empty" object has no enumerable own-properties. _.isEmpty = function(obj) { if (_.isArray(obj) || _.isString(obj)) return obj.length === 0; for (var key in obj) if (hasOwnProperty.call(obj, key)) return false; @@ -729,7 +730,7 @@ // Is a given value an array? // Delegates to ECMA5's native Array.isArray _.isArray = nativeIsArray || function(obj) { - return toString.call(obj) === '[object Array]'; + return toString.call(obj) == '[object Array]'; }; // Is a given variable an object? @@ -739,22 +740,22 @@ // Is a given variable an arguments object? _.isArguments = function(obj) { - return !!(obj && hasOwnProperty.call(obj, 'callee')); + return toString.call(obj) == '[object Arguments]' || !!(obj && hasOwnProperty.call(obj, 'callee')); }; // Is a given value a function? _.isFunction = function(obj) { - return !!(obj && obj.constructor && obj.call && obj.apply); + return toString.call(obj) == '[object Function]'; }; // Is a given value a string? _.isString = function(obj) { - return !!(obj === '' || (obj && obj.charCodeAt && obj.substr)); + return toString.call(obj) == '[object String]'; }; // Is a given value a number? _.isNumber = function(obj) { - return !!(obj === 0 || (obj && obj.toExponential && obj.toFixed)); + return toString.call(obj) == '[object Number]'; }; // Is the given value `NaN`? `NaN` happens to be the only value in JavaScript @@ -770,12 +771,12 @@ // Is a given value a date? _.isDate = function(obj) { - return !!(obj && obj.getTimezoneOffset && obj.setUTCFullYear); + return toString.call(obj) == '[object Date]'; }; // Is the given value a regular expression? _.isRegExp = function(obj) { - return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false)); + return toString.call(obj) == '[object RegExp]'; }; // Is a given value equal to null?