This commit is contained in:
jrburke
2011-10-18 16:28:28 -07:00
3 changed files with 40 additions and 48 deletions

View File

@@ -144,7 +144,7 @@
<br /> <br />
<span class="methods"><a href="#each">each</a>, <a href="#map">map</a>, <span class="methods"><a href="#each">each</a>, <a href="#map">map</a>,
<a href="#reduce">reduce</a>, <a href="#reduceRight">reduceRight</a>, <a href="#reduce">reduce</a>, <a href="#reduceRight">reduceRight</a>,
<a href="#detect">detect</a>, <a href="#select">select</a>, <a href="#find">find</a>, <a href="#filter">filter</a>,
<a href="#reject">reject</a>, <a href="#all">all</a>, <a href="#reject">reject</a>, <a href="#all">all</a>,
<a href="#any">any</a>, <a href="#include">include</a>, <a href="#any">any</a>, <a href="#include">include</a>,
<a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>, <a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>,
@@ -312,8 +312,9 @@ var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
=&gt; [4, 5, 2, 3, 0, 1] =&gt; [4, 5, 2, 3, 0, 1]
</pre> </pre>
<p id="detect"> <p id="find">
<b class="header">detect</b><code>_.detect(list, iterator, [context])</code> <b class="header">find</b><code>_.find(list, iterator, [context])</code>
<span class="alias">Alias: <b>detect</b></span>
<br /> <br />
Looks through each value in the <b>list</b>, returning the first one that Looks through each value in the <b>list</b>, returning the first one that
passes a truth test (<b>iterator</b>). The function returns as passes a truth test (<b>iterator</b>). The function returns as
@@ -321,20 +322,20 @@ var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
entire list. entire list.
</p> </p>
<pre> <pre>
var even = _.detect([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
=&gt; 2 =&gt; 2
</pre> </pre>
<p id="select"> <p id="filter">
<b class="header">select</b><code>_.select(list, iterator, [context])</code> <b class="header">filter</b><code>_.filter(list, iterator, [context])</code>
<span class="alias">Alias: <b>filter</b></span> <span class="alias">Alias: <b>select</b></span>
<br /> <br />
Looks through each value in the <b>list</b>, returning an array of all Looks through each value in the <b>list</b>, returning an array of all
the values that pass a truth test (<b>iterator</b>). Delegates to the the values that pass a truth test (<b>iterator</b>). Delegates to the
native <b>filter</b> method, if it exists. native <b>filter</b> method, if it exists.
</p> </p>
<pre> <pre>
var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
=&gt; [2, 4, 6] =&gt; [2, 4, 6]
</pre> </pre>
@@ -342,7 +343,7 @@ var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
<b class="header">reject</b><code>_.reject(list, iterator, [context])</code> <b class="header">reject</b><code>_.reject(list, iterator, [context])</code>
<br /> <br />
Returns the values in <b>list</b> without the elements that the truth Returns the values in <b>list</b> without the elements that the truth
test (<b>iterator</b>) passes. The opposite of <b>select</b>. test (<b>iterator</b>) passes. The opposite of <b>filter</b>.
</p> </p>
<pre> <pre>
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
@@ -951,7 +952,7 @@ _.clone({name : 'moe'});
</p> </p>
<pre> <pre>
_([1,2,3,200]).chain(). _([1,2,3,200]).chain().
select(function(num) { return num % 2 == 0; }). filter(function(num) { return num % 2 == 0; }).
tap(console.log). tap(console.log).
map(function(num) { return num * num }). map(function(num) { return num * num }).
value(); value();

View File

@@ -154,8 +154,8 @@ $(document).ready(function() {
ok(_.isEqual({}, {}), "Empty object literals are equal"); ok(_.isEqual({}, {}), "Empty object literals are equal");
ok(_.isEqual([], []), "Empty array literals are equal"); ok(_.isEqual([], []), "Empty array literals are equal");
ok(_.isEqual([{}], [{}]), "Empty nested arrays and objects are equal"); ok(_.isEqual([{}], [{}]), "Empty nested arrays and objects are equal");
ok(_.isEqual({length: 0}, []), "Array-like objects and arrays are equal"); ok(!_.isEqual({length: 0}, []), "Array-like objects and arrays are not equal.");
ok(_.isEqual([], {length: 0}), "Commutative equality is implemented for array-like objects"); ok(!_.isEqual([], {length: 0}), "Commutative equality is implemented for array-like objects");
ok(!_.isEqual({}, []), "Object literals and array literals are not equal"); ok(!_.isEqual({}, []), "Object literals and array literals are not equal");
ok(!_.isEqual([], {}), "Commutative equality is implemented for objects and arrays"); ok(!_.isEqual([], {}), "Commutative equality is implemented for objects and arrays");
@@ -174,7 +174,7 @@ $(document).ready(function() {
b.join = b.pop = b.reverse = b.shift = b.slice = b.splice = b.concat = b.sort = b.unshift = null; b.join = b.pop = b.reverse = b.shift = b.slice = b.splice = b.concat = b.sort = b.unshift = null;
// Array elements and properties. // Array elements and properties.
ok(_.isEqual(a, b), "Arrays containing equivalent elements and different non-numeric properties are equal"); ok(!_.isEqual(a, b), "Arrays containing equivalent elements and different non-numeric properties are not equal");
a.push("White Rocks"); a.push("White Rocks");
ok(!_.isEqual(a, b), "Arrays of different lengths are not equal"); ok(!_.isEqual(a, b), "Arrays of different lengths are not equal");
a.push("East Boulder"); a.push("East Boulder");
@@ -183,7 +183,7 @@ $(document).ready(function() {
// Sparse arrays. // Sparse arrays.
ok(_.isEqual(Array(3), Array(3)), "Sparse arrays of identical lengths are equal"); ok(_.isEqual(Array(3), Array(3)), "Sparse arrays of identical lengths are equal");
ok(!_.isEqual(Array(3), Array(6)), "Sparse arrays of different lengths are not equal"); ok(_.isEqual(Array(3), Array(6)), "Sparse arrays of different lengths are equal when both are empty");
// According to the Microsoft deviations spec, section 2.1.26, JScript 5.x treats `undefined` // According to the Microsoft deviations spec, section 2.1.26, JScript 5.x treats `undefined`
// elements in arrays as elisions. Thus, sparse arrays and dense arrays containing `undefined` // elements in arrays as elisions. Thus, sparse arrays and dense arrays containing `undefined`
@@ -235,8 +235,8 @@ $(document).ready(function() {
// Instances. // Instances.
ok(_.isEqual(new First, new First), "Object instances are equal"); ok(_.isEqual(new First, new First), "Object instances are equal");
ok(_.isEqual(new First, new Second), "Objects with different constructors and identical own properties are equal"); ok(!_.isEqual(new First, new Second), "Objects with different constructors and identical own properties are not equal");
ok(_.isEqual({value: 1}, new First), "Object instances and objects sharing equivalent properties are identical"); ok(!_.isEqual({value: 1}, new First), "Object instances and objects sharing equivalent properties are not identical");
ok(!_.isEqual({value: 2}, new Second), "The prototype chain of objects should not be examined"); ok(!_.isEqual({value: 2}, new Second), "The prototype chain of objects should not be examined");
// Circular Arrays. // Circular Arrays.

View File

@@ -48,12 +48,14 @@
// Create a safe reference to the Underscore object for use below. // Create a safe reference to the Underscore object for use below.
var _ = function(obj) { return new wrapper(obj); }; var _ = function(obj) { return new wrapper(obj); };
// Export the Underscore object for **CommonJS**, with backwards-compatibility // Export the Underscore object for **Node.js** and **"CommonJS"**, with
// for the old `require()` API. If we're not in CommonJS, add `_` to the // backwards-compatibility for the old `require()` API. If we're not in
// global object. // CommonJS, add `_` to the global object.
if (typeof module !== 'undefined' && module.exports) { if (typeof exports !== 'undefined') {
module.exports = _; if (typeof module !== 'undefined' && module.exports) {
_._ = _; exports = module.exports = _;
}
exports._ = _;
} else { } else {
// Exported as a string, for Closure Compiler "advanced" mode. // Exported as a string, for Closure Compiler "advanced" mode.
root['_'] = _; root['_'] = _;
@@ -686,6 +688,8 @@
} }
// Ensure that both values are objects. // Ensure that both values are objects.
if (typeA != 'object') return false; if (typeA != 'object') return false;
// Objects with different constructors are not equal.
if (a.constructor !== b.constructor) return false;
// Assume equality for cyclic structures. The algorithm for detecting cyclic structures is // Assume equality for cyclic structures. The algorithm for detecting cyclic structures is
// adapted from ES 5.1 section 15.12.3, abstract operation `JO`. // adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
var length = stack.length; var length = stack.length;
@@ -697,34 +701,21 @@
// Add the first object to the stack of traversed objects. // Add the first object to the stack of traversed objects.
stack.push(a); stack.push(a);
var size = 0, result = true; var size = 0, result = true;
if (a.length === +a.length || b.length === +b.length) { // Deep compare objects.
// Compare object lengths to determine if a deep comparison is necessary. for (var key in a) {
size = a.length; if (hasOwnProperty.call(a, key)) {
result = size == b.length; // Count the expected number of properties.
if (result) { size++;
// Deep compare array-like object contents, ignoring non-numeric properties. // Deep compare each member.
while (size--) { if (!(result = hasOwnProperty.call(b, key) && eq(a[key], b[key], stack))) break;
// Ensure commutative equality for sparse arrays.
if (!(result = size in a == size in b && eq(a[size], b[size], stack))) break;
}
} }
} else { }
// Deep compare objects. // Ensure that both objects contain the same number of properties.
for (var key in a) { if (result) {
if (hasOwnProperty.call(a, key)) { for (key in b) {
// Count the expected number of properties. if (hasOwnProperty.call(b, key) && !size--) break;
size++;
// Deep compare each member.
if (!(result = hasOwnProperty.call(b, key) && eq(a[key], b[key], stack))) break;
}
}
// Ensure that both objects contain the same number of properties.
if (result) {
for (key in b) {
if (hasOwnProperty.call(b, key) && !size--) break;
}
result = !size;
} }
result = !size;
} }
// Remove the first object from the stack of traversed objects. // Remove the first object from the stack of traversed objects.
stack.pop(); stack.pop();