Underscore 0.4.4, with isNumber, isString, and isEqual(NaN, NaN)

This commit is contained in:
Jeremy Ashkenas
2009-11-18 16:09:55 -05:00
parent b932867dec
commit c9e46262ab
6 changed files with 99 additions and 35 deletions

View File

@@ -107,11 +107,11 @@
<p> <p>
<table> <table>
<tr> <tr>
<td><a href="underscore.js">Development Version (0.4.3)</a></td> <td><a href="underscore.js">Development Version (0.4.4)</a></td>
<td><i>18kb, Uncompressed with Comments</i></td> <td><i>18kb, Uncompressed with Comments</i></td>
</tr> </tr>
<tr> <tr>
<td><a href="underscore-min.js">Production Version (0.4.3)</a></td> <td><a href="underscore-min.js">Production Version (0.4.4)</a></td>
<td><i>2kb, Packed and Gzipped</i></td> <td><i>2kb, Packed and Gzipped</i></td>
</tr> </tr>
</table> </table>
@@ -202,7 +202,8 @@ _(lyrics).chain()
<span class="methods"><a href="#keys">keys</a>, <a href="#values">values</a>, <span class="methods"><a href="#keys">keys</a>, <a href="#values">values</a>,
<a href="#extend">extend</a>, <a href="#clone">clone</a>, <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="#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="#isUndefined">isUndefined</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>
</span> </span>
</p> </p>
@@ -763,6 +764,26 @@ _.isArray([1,2,3]);
<pre> <pre>
_.isFunction(alert); _.isFunction(alert);
=&gt; true =&gt; true
</pre>
<p id="isString">
<b class="header">isString</b><code>_.isString(object)</code>
<br />
Returns <i>true</i> if <b>object</b> is a String.
</p>
<pre>
_.isFunction("moe");
=&gt; true
</pre>
<p id="isNumber">
<b class="header">isNumber</b><code>_.isNumber(object)</code>
<br />
Returns <i>true</i> if <b>object</b> is a Number.
</p>
<pre>
_.isNumber(8.4 * 5);
=&gt; true
</pre> </pre>
<p id="isUndefined"> <p id="isUndefined">
@@ -891,6 +912,12 @@ _([1, 2, 3]).value();
<h2>Change Log</h2> <h2>Change Log</h2>
<p>
<b class="header">0.4.4</b><br />
Added <tt>isString</tt>, and <tt>isNumber</tt>, for consistency. Fixed
<tt>_.isEqual(NaN, NaN)</tt> to return <i>true</i> (which is debatable).
</p>
<p> <p>
<b class="header">0.4.3</b><br /> <b class="header">0.4.3</b><br />
Started using the native StopIteration object in browsers that support it. Started using the native StopIteration object in browsers that support it.

View File

@@ -34,6 +34,8 @@ $(document).ready(function() {
ok(moe != clone, 'basic equality between objects is false'); ok(moe != clone, 'basic equality between objects is false');
ok(_.isEqual(moe, clone), 'deep equality is true'); ok(_.isEqual(moe, clone), 'deep equality is true');
ok(_(moe).isEqual(clone), 'OO-style deep equality works'); 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');
}); });
test("objects: isEmpty", function() { test("objects: isEmpty", function() {
@@ -57,6 +59,16 @@ $(document).ready(function() {
ok(_.isArray([1, 2, 3]), 'but arrays are'); ok(_.isArray([1, 2, 3]), 'but arrays are');
}); });
test("objects: isString", function() {
ok(!_.isString(document.body), 'the document body is not a string');
ok(_.isString([1, 2, 3].join(', ')), 'but strings are');
});
test("objects: isNumber", function() {
ok(!_.isNumber(arguments), 'the arguments object is not a number');
ok(_.isNumber(3 * 4 - 7 / 10), 'but numbers are');
});
test("objects: isFunction", function() { test("objects: isFunction", function() {
ok(!_.isFunction([1, 2, 3]), 'arrays are not functions'); ok(!_.isFunction([1, 2, 3]), 'arrays are not functions');
ok(!_.isFunction('moe'), 'strings are not functions'); ok(!_.isFunction('moe'), 'strings are not functions');

View File

@@ -35,7 +35,7 @@ $(document).ready(function() {
"defer", "delay", "detect", "each", "every", "extend", "filter", "first", "defer", "delay", "detect", "each", "every", "extend", "filter", "first",
"flatten", "foldl", "foldr", "forEach", "functions", "identity", "include", "flatten", "foldl", "foldr", "forEach", "functions", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual", "indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual",
"isFunction", "isUndefined", "keys", "last", "lastIndexOf", "map", "max", "isFunction", "isNumber", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
"methods", "min", "pluck", "reduce", "reduceRight", "reject", "select", "methods", "min", "pluck", "reduce", "reduceRight", "reject", "select",
"size", "some", "sortBy", "sortedIndex", "template", "toArray", "uniq", "size", "some", "sortBy", "sortedIndex", "template", "toArray", "uniq",
"uniqueId", "values", "without", "wrap", "zip"]; "uniqueId", "values", "without", "wrap", "zip"];

15
underscore-min.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -31,7 +31,7 @@
if (typeof exports !== 'undefined') exports._ = _; if (typeof exports !== 'undefined') exports._ = _;
// Current version. // Current version.
_.VERSION = '0.4.3'; _.VERSION = '0.4.4';
/*------------------------ Collection Functions: ---------------------------*/ /*------------------------ Collection Functions: ---------------------------*/
@@ -399,6 +399,8 @@
if (a == b) return true; if (a == b) return true;
// One of them implements an isEqual()? // One of them implements an isEqual()?
if (a.isEqual) return a.isEqual(b); if (a.isEqual) return a.isEqual(b);
// Both are NaN?
if (_.isNumber(a) && _.isNumber(b) && isNaN(a) && isNaN(b)) return true;
// If a is not an object by this point, we can't handle it. // If a is not an object by this point, we can't handle it.
if (atype !== 'object') return false; if (atype !== 'object') return false;
// Nothing else worked, deep compare the contents. // Nothing else worked, deep compare the contents.
@@ -430,6 +432,16 @@
return Object.prototype.toString.call(obj) == '[object Function]'; return Object.prototype.toString.call(obj) == '[object Function]';
}; };
// Is a given value a String?
_.isString = function(obj) {
return Object.prototype.toString.call(obj) == '[object String]';
};
// Is a given value a Number?
_.isNumber = function(obj) {
return Object.prototype.toString.call(obj) == '[object Number]';
};
// Is a given variable undefined? // Is a given variable undefined?
_.isUndefined = function(obj) { _.isUndefined = function(obj) {
return typeof obj == 'undefined'; return typeof obj == 'undefined';