documentation for Underscore 0.4.7, with isDate, isNaN, and isNull

This commit is contained in:
Jeremy Ashkenas
2009-12-06 13:43:16 -05:00
parent 86c2ad2b1f
commit 7a1f92a8c5
4 changed files with 90 additions and 9 deletions

View File

@@ -81,7 +81,7 @@
</p>
<p>
Underscore provides 50-odd functions that support both the usual
Underscore provides 60-odd functions that support both the usual
functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
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 @@
<p>
<table>
<tr>
<td><a href="underscore.js">Development Version (0.4.6)</a></td>
<td><a href="underscore.js">Development Version (0.4.7)</a></td>
<td><i>20kb, Uncompressed with Comments</i></td>
</tr>
<tr>
<td><a href="underscore-min.js">Production Version (0.4.6)</a></td>
<td><a href="underscore-min.js">Production Version (0.4.7)</a></td>
<td><i>2kb, Packed and Gzipped</i></td>
</tr>
</table>
@@ -207,7 +207,9 @@ _(lyrics).chain()
<a href="#extend">extend</a>, <a href="#clone">clone</a>,
<a href="#isEqual">isEqual</a>, <a href="#isEmpty">isEmpty</a>, <a href="#isElement">isElement</a>,
<a href="#isArray">isArray</a>, <a href="#isFunction">isFunction</a>, <a href="#isString">isString</a>,
<a href="#isNumber">isNumber</a>, <a href="#isUndefined">isUndefined</a>
<a href="#isNumber">isNumber</a>, <a href="#isDate">isDate</a>
<a href="#isNaN">isNaN</a>, <a href="#isNull">isNull</a>,
<a href="#isUndefined">isUndefined</a>
</span>
</p>
@@ -828,6 +830,44 @@ _.isFunction("moe");
<pre>
_.isNumber(8.4 * 5);
=&gt; true
</pre>
<p id="isDate">
<b class="header">isDate</b><code>_.isDate(object)</code>
<br />
Returns <i>true</i> if <b>object</b> is a Date.
</p>
<pre>
_.isDate(new Date());
=&gt; true
</pre>
<p id="isNaN">
<b class="header">isNaN</b><code>_.isNaN(object)</code>
<br />
Returns <i>true</i> if <b>object</b> is <i>NaN</i>.<br /> Note: this is not
the same as the native <b>isNaN</b> function, which will also return
true if the variable is <i>undefined</i>.
</p>
<pre>
_.isNaN(NaN);
=&gt; true
isNaN(undefined);
=&gt; true
_.isNaN(undefined);
=&gt; false
</pre>
<p id="isNull">
<b class="header">isNull</b><code>_.isNull(object)</code>
<br />
Returns <i>true</i> if the value of <b>object</b> is <i>null</i>.
</p>
<pre>
_.isNull(null);
=&gt; true
_.isNull(undefined);
=&gt; false
</pre>
<p id="isUndefined">
@@ -956,6 +996,11 @@ _([1, 2, 3]).value();
<h2>Change Log</h2>
<p>
<b class="header">0.4.7</b><br />
</p>
<p>
<b class="header">0.4.6</b><br />
Added the <tt>range</tt> function, a port of the

View File

@@ -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');

View File

@@ -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"];

View File

@@ -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';