From bd271e4794c4ffa0fe5716f77a54bdef99a6af05 Mon Sep 17 00:00:00 2001 From: John Wright Date: Fri, 26 Feb 2010 21:46:58 -0700 Subject: [PATCH] added _.isBoolean function --- test/objects.js | 13 +++++++++++++ underscore.js | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/test/objects.js b/test/objects.js index e45633e8c..45ca838d9 100644 --- a/test/objects.js +++ b/test/objects.js @@ -85,6 +85,7 @@ $(document).ready(function() { parent.iRegExp = /hi/;\ parent.iNaN = NaN;\ parent.iNull = null;\ + parent.iBoolean = false;\ parent.iUndefined = undefined;\ " ); @@ -128,6 +129,18 @@ $(document).ready(function() { ok(_.isNumber(iNumber), 'even from another frame'); }); + test("objects: isBoolean", function() { + ok(!_.isBoolean(2), 'a number 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'); + ok(!_.isBoolean(undefined), 'undefined is not a boolean'); + ok(_.isBoolean(true), 'but true is'); + ok(_.isBoolean(false), 'and so is false'); + ok(_.isBoolean(iBoolean), 'even from another frame'); + }); + test("objects: isFunction", function() { ok(!_.isFunction([1, 2, 3]), 'arrays are not functions'); ok(!_.isFunction('moe'), 'strings are not functions'); diff --git a/underscore.js b/underscore.js index f6d68a690..ff956bc53 100644 --- a/underscore.js +++ b/underscore.js @@ -525,6 +525,11 @@ return (obj === +obj) || (toString.call(obj) === '[object Number]'); }; + // Is a given value a boolean? + _.isBoolean = function(obj) { + return (toString.call(obj) === '[object Boolean]'); + }; + // Is a given value a date? _.isDate = function(obj) { return !!(obj && obj.getTimezoneOffset && obj.setUTCFullYear);