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>
<table>
<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>
</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>
</tr>
</table>
@@ -202,7 +202,8 @@ _(lyrics).chain()
<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="#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>
</p>
@@ -763,6 +764,26 @@ _.isArray([1,2,3]);
<pre>
_.isFunction(alert);
=&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>
<p id="isUndefined">
@@ -891,6 +912,12 @@ _([1, 2, 3]).value();
<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>
<b class="header">0.4.3</b><br />
Started using the native StopIteration object in browsers that support it.

View File

@@ -1,68 +1,80 @@
$(document).ready(function() {
module("Object functions (values, extend, isEqual, and so on...)");
test("objects: keys", function() {
equals(_.keys({one : 1, two : 2}).join(', '), 'one, two', 'can extract the keys from an object');
});
test("objects: values", function() {
equals(_.values({one : 1, two : 2}).join(', '), '1, 2', 'can extract the values from an object');
});
test("objects: extend", function() {
var source = {name : 'moe'}, dest = {age : 50};
_.extend(dest, source);
equals(dest.name, 'moe', 'can extend an object with the attributes of another');
});
test("objects: clone", function() {
var moe = {name : 'moe', lucky : [13, 27, 34]};
var clone = _.clone(moe);
equals(clone.name, 'moe', 'the clone as the attributes of the original');
clone.name = 'curly';
ok(clone.name == 'curly' && moe.name == 'moe', 'clones can change shallow attributes without affecting the original');
clone.lucky.push(101);
equals(_.last(moe.lucky), 101, 'changes to deep attributes are shared with the original');
});
test("objects: isEqual", function() {
var moe = {name : 'moe', lucky : [13, 27, 34]};
var clone = {name : 'moe', lucky : [13, 27, 34]};
ok(moe != clone, 'basic equality between objects is false');
ok(_.isEqual(moe, clone), 'deep equality is true');
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() {
ok(!_([1]).isEmpty(), '[1] is not empty');
ok(_.isEmpty([]), '[] is empty');
ok(!_.isEmpty({one : 1}), '{one : 1} is not empty');
ok(_.isEmpty({}), '{} is empty');
var obj = {one : 1};
delete obj.one;
ok(_.isEmpty(obj), 'deleting all the keys from an object empties it');
});
test("objects: isElement", function() {
ok(!_.isElement('div'), 'strings are not dom elements');
ok(_.isElement($('html')[0]), 'the html tag is a DOM element');
});
test("objects: isArray", function() {
ok(!_.isArray(arguments), 'the arguments object is not an array');
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() {
ok(!_.isFunction([1, 2, 3]), 'arrays are not functions');
ok(!_.isFunction('moe'), 'strings are not functions');
ok(_.isFunction(_.isFunction), 'but functions are');
});
test("objects: isUndefined", function() {
ok(!_.isUndefined(1), 'numbers are defined');
ok(!_.isUndefined(null), 'null is defined');

View File

@@ -13,7 +13,7 @@
<script type="text/javascript" src="objects.js"></script>
<script type="text/javascript" src="utility.js"></script>
<script type="text/javascript" src="chaining.js"></script>
<script type="text/javascript" src="speed.js"></script>
<script type="text/javascript" src="speed.js"></script>
</head>
<body>
<h1 id="qunit-header">Underscore Test Suite</h1>

View File

@@ -1,7 +1,7 @@
$(document).ready(function() {
module("Utility functions (uniqueId, template)");
test("utility: noConflict", function() {
var underscore = _.noConflict();
ok(underscore.isUndefined(_), "The '_' variable has been returned to its previous state.");
@@ -9,12 +9,12 @@ $(document).ready(function() {
equals(intersection.join(', '), '1, 2', 'but the intersection function still works');
window._ = underscore;
});
test("utility: identity", function() {
var moe = {name : 'moe'};
equals(_.identity(moe), moe, 'moe is the same as his identity');
});
test('utility: breakLoop', function() {
var result = null;
_([1,2,3,4,5,6]).each(function(num) {
@@ -23,25 +23,25 @@ $(document).ready(function() {
});
equals(result, 3, 'broke out of a loop');
});
test("utility: uniqueId", function() {
var ids = [], i = 0;
while(i++ < 100) ids.push(_.uniqueId());
equals(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids');
});
test("utility: functions", function() {
var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact", "compose",
"defer", "delay", "detect", "each", "every", "extend", "filter", "first",
"flatten", "foldl", "foldr", "forEach", "functions", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual",
"isFunction", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
"methods", "min", "pluck", "reduce", "reduceRight", "reject", "select",
"size", "some", "sortBy", "sortedIndex", "template", "toArray", "uniq",
var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact", "compose",
"defer", "delay", "detect", "each", "every", "extend", "filter", "first",
"flatten", "foldl", "foldr", "forEach", "functions", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual",
"isFunction", "isNumber", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
"methods", "min", "pluck", "reduce", "reduceRight", "reject", "select",
"size", "some", "sortBy", "sortedIndex", "template", "toArray", "uniq",
"uniqueId", "values", "without", "wrap", "zip"];
ok(_(expected).isEqual(_.methods()), 'provides a sorted list of functions');
});
test("utility: template", function() {
var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");
var result = basicTemplate({thing : 'This'});
@@ -50,5 +50,5 @@ $(document).ready(function() {
result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
equals(result, "<li>Moe</li><li>Larry</li><li>Curly</li>", 'can run arbitrary javascript in templates');
});
});

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._ = _;
// Current version.
_.VERSION = '0.4.3';
_.VERSION = '0.4.4';
/*------------------------ Collection Functions: ---------------------------*/
@@ -399,6 +399,8 @@
if (a == b) return true;
// 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 a is not an object by this point, we can't handle it.
if (atype !== 'object') return false;
// Nothing else worked, deep compare the contents.
@@ -430,6 +432,16 @@
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?
_.isUndefined = function(obj) {
return typeof obj == 'undefined';