From 9903905175b1178e1c6dcf05c1ee569cee087240 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 27 Feb 2010 00:03:36 -0500 Subject: [PATCH] merging mrjjwright's isBoolean, with tests, docs, and credit --- README | 1 + index.html | 12 +++++++++++- test/objects.js | 4 +++- underscore.js | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README b/README index fefa78d74..333b13f45 100644 --- a/README +++ b/README @@ -28,3 +28,4 @@ Many thanks to our contributors: Jed Schmidt Noah Sloan Luke Sutton + John Wright diff --git a/index.html b/index.html index 1bb85a5ca..3ac282394 100644 --- a/index.html +++ b/index.html @@ -162,7 +162,7 @@ functions, extend, clone, tap, isEqual, isEmpty, isElement, isArray, isArguments, isFunction, isString, - isNumber, isDate, isRegExp + isNumber, isBoolean, isDate, isRegExp isNaN, isNull, isUndefined @@ -874,6 +874,16 @@ _.isFunction("moe");
 _.isNumber(8.4 * 5);
 => true
+
+ +

+ isBoolean_.isBoolean(object) +
+ Returns true if object is either true or false. +

+
+_.isBoolean(null);
+=> false
 

diff --git a/test/objects.js b/test/objects.js index 45ca838d9..b49563f9d 100644 --- a/test/objects.js +++ b/test/objects.js @@ -14,7 +14,7 @@ $(document).ready(function() { var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact", "compose","defer", "delay", "detect", "each", "every", "extend", "filter", "first", "flatten", "foldl", "foldr", "forEach", "functions", "head", "identity", "include", - "indexOf", "inject", "intersect", "invoke", "isArguments", "isArray", "isDate", "isElement", "isEmpty", "isEqual", + "indexOf", "inject", "intersect", "invoke", "isArguments", "isArray", "isBoolean", "isDate", "isElement", "isEmpty", "isEqual", "isFunction", "isNaN", "isNull", "isNumber", "isRegExp", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max", "methods", "min", "mixin", "noConflict", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select", "size", "some", "sortBy", "sortedIndex", "tail", "tap", "template", "times", "toArray", "uniq", @@ -136,6 +136,8 @@ $(document).ready(function() { 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(NaN), 'NaN is not a boolean'); + ok(!_.isBoolean(null), 'null is not a boolean'); ok(_.isBoolean(true), 'but true is'); ok(_.isBoolean(false), 'and so is false'); ok(_.isBoolean(iBoolean), 'even from another frame'); diff --git a/underscore.js b/underscore.js index ff956bc53..1c6f6fb20 100644 --- a/underscore.js +++ b/underscore.js @@ -527,7 +527,7 @@ // Is a given value a boolean? _.isBoolean = function(obj) { - return (toString.call(obj) === '[object Boolean]'); + return obj === true || obj === false; }; // Is a given value a date?