From 7a1f92a8c5a9c6b0933abd88e84098dc6d5c582f Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 6 Dec 2009 13:43:16 -0500 Subject: [PATCH] documentation for Underscore 0.4.7, with isDate, isNaN, and isNull --- index.html | 53 +++++++++++++++++++++++++++++++++++++++++++++---- test/objects.js | 20 +++++++++++++++++++ test/utility.js | 4 ++-- underscore.js | 22 +++++++++++++++++--- 4 files changed, 90 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index 68bdb3701..0c48b1c99 100644 --- a/index.html +++ b/index.html @@ -81,7 +81,7 @@

- Underscore provides 50-odd functions that support both the usual + Underscore provides 60-odd functions that support both the usual functional suspects: map, select, invoke — as well as more specialized helpers: function binding, javascript templating, deep equality testing, and so on. It delegates to built-in @@ -111,11 +111,11 @@

- + - +
Development Version (0.4.6)Development Version (0.4.7) 20kb, Uncompressed with Comments
Production Version (0.4.6)Production Version (0.4.7) 2kb, Packed and Gzipped
@@ -207,7 +207,9 @@ _(lyrics).chain() extend, clone, isEqual, isEmpty, isElement, isArray, isFunction, isString, - isNumber, isUndefined + isNumber, isDate + isNaN, isNull, + isUndefined

@@ -828,6 +830,44 @@ _.isFunction("moe");
 _.isNumber(8.4 * 5);
 => true
+
+ +

+ isDate_.isDate(object) +
+ Returns true if object is a Date. +

+
+_.isDate(new Date());
+=> true
+
+ +

+ isNaN_.isNaN(object) +
+ Returns true if object is NaN.
Note: this is not + the same as the native isNaN function, which will also return + true if the variable is undefined. +

+
+_.isNaN(NaN);
+=> true
+isNaN(undefined);
+=> true
+_.isNaN(undefined);
+=> false
+
+ +

+ isNull_.isNull(object) +
+ Returns true if the value of object is null. +

+
+_.isNull(null);
+=> true
+_.isNull(undefined);
+=> false
 

@@ -956,6 +996,11 @@ _([1, 2, 3]).value();

Change Log

+

+ 0.4.7
+ +

+

0.4.6
Added the range function, a port of the diff --git a/test/objects.js b/test/objects.js index d76d3e8e2..355b2937b 100644 --- a/test/objects.js +++ b/test/objects.js @@ -36,6 +36,7 @@ $(document).ready(function() { ok(_(moe).isEqual(clone), 'OO-style deep equality works'); ok(!_.isEqual(5, NaN), '5 is not equal to NaN'); ok(_.isEqual(NaN, NaN), 'NaN is equal to NaN'); + ok(_.isEqual(new Date(100), new Date(100)), 'identical dates are equal'); }); test("objects: isEmpty", function() { @@ -75,6 +76,25 @@ $(document).ready(function() { ok(_.isFunction(_.isFunction), 'but functions are'); }); + test("objects: isDate", function() { + ok(!_.isDate(100), 'numbers are not dates'); + ok(!_.isDate({}), 'objects are not dates'); + ok(_.isDate(new Date()), 'but dates are'); + }); + + test("objects: isNaN", function() { + ok(!_.isNaN(undefined), 'undefined is not NaN'); + ok(!_.isNaN(null), 'null is not NaN'); + ok(!_.isNaN(0), '0 is not NaN'); + ok(_.isNaN(NaN), 'but NaN is'); + }); + + test("objects: isNull", function() { + ok(!_.isNull(undefined), 'undefined is not null'); + ok(!_.isNull(NaN), 'NaN is not null'); + ok(_.isNull(null), 'but null is'); + }); + test("objects: isUndefined", function() { ok(!_.isUndefined(1), 'numbers are defined'); ok(!_.isUndefined(null), 'null is defined'); diff --git a/test/utility.js b/test/utility.js index 9f6c855e9..0d02b3041 100644 --- a/test/utility.js +++ b/test/utility.js @@ -34,8 +34,8 @@ $(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", "isArray", "isElement", "isEmpty", "isEqual", - "isFunction", "isNumber", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max", + "indexOf", "inject", "intersect", "invoke", "isArray", "isDate", "isElement", "isEmpty", "isEqual", + "isFunction", "isNaN", "isNull", "isNumber", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max", "methods", "min", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select", "size", "some", "sortBy", "sortedIndex", "tail", "template", "toArray", "uniq", "uniqueId", "values", "without", "wrap", "zip"]; diff --git a/underscore.js b/underscore.js index 09e781a42..cbdf3e2e7 100644 --- a/underscore.js +++ b/underscore.js @@ -34,7 +34,7 @@ var oproto = Object.prototype; // Current version. - _.VERSION = '0.4.6'; + _.VERSION = '0.4.7'; /*------------------------ Collection Functions: ---------------------------*/ @@ -426,11 +426,11 @@ // Basic equality test (watch out for coercions). if (a == b) return true; // Check dates' integer values. - if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime(); + if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime(); // One of them implements an isEqual()? if (a.isEqual) return a.isEqual(b); // Both are NaN? - if (_.isNumber(a) && _.isNumber(b) && isNaN(a) && isNaN(b)) return true; + if (_.isNaN(a) && _.isNaN(b)) return true; // If a is not an object by this point, we can't handle it. if (atype !== 'object') return false; // Check for different array lengths before comparing contents. @@ -474,6 +474,22 @@ return oproto.toString.call(obj) == '[object Number]'; }; + // Is a given value a Date? + _.isDate = function(obj) { + return oproto.toString.call(obj) == '[object Date]'; + }; + + // 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; + }; + // Is a given variable undefined? _.isUndefined = function(obj) { return typeof obj == 'undefined';