comment edits

This commit is contained in:
Jeremy Ashkenas
2009-10-27 14:45:05 -04:00
parent 26a9175419
commit 90e34e1a74
2 changed files with 15 additions and 11 deletions

View File

@@ -160,7 +160,7 @@
function. The <b>iterator</b> is bound to the <b>context</b> object, if one is function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
passed. If <b>list</b> is a Javascript object, a pair with <b>key</b> passed. If <b>list</b> is a Javascript object, a pair with <b>key</b>
and <b>value</b> properties will be yielded. If the list has an <b>each</b> and <b>value</b> properties will be yielded. If the list has an <b>each</b>
method of its own defined, it will be used. Delegates to the native method of its own, it will be used instead. Delegates to the native
<b>forEach</b> function if it exists. <b>forEach</b> function if it exists.
</p> </p>
<pre> <pre>
@@ -196,8 +196,8 @@ var sum = _.inject([1, 2, 3], 0, function(memo, num){ return memo + num });
<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
soon as it finds the true element, and doesn't continue to traverse soon as it finds the first acceptable element, and doesn't continue to
the list. traverse the list.
</p> </p>
<pre> <pre>
var even = _.detect([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); var even = _.detect([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
@@ -257,7 +257,7 @@ _.any([null, 0, 'yes', false]);
<b class="method_name">include</b><code>_.include(list, value)</code> <b class="method_name">include</b><code>_.include(list, value)</code>
<br /> <br />
Returns <i>true</i> if the <b>value</b> is present in the <b>list</b>, using Returns <i>true</i> if the <b>value</b> is present in the <b>list</b>, using
<i>==</i> to test equality. Uses <b>indexOf</b> internally, if <b>list</b> <i>===</i> to test equality. Uses <b>indexOf</b> internally, if <b>list</b>
is an Array. is an Array.
</p> </p>
<pre> <pre>
@@ -408,7 +408,7 @@ _.flatten([1, [2], [3, [[[4]]]]]);
<b class="method_name">without</b><code>_.without(array, [*values])</code> <b class="method_name">without</b><code>_.without(array, [*values])</code>
<br /> <br />
Returns a copy of the <b>array</b> with all instances of the <b>values</b> Returns a copy of the <b>array</b> with all instances of the <b>values</b>
removed. <i>==</i> is used for the equality test. removed. <i>===</i> is used for the equality test.
</p> </p>
<pre> <pre>
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
@@ -418,7 +418,7 @@ _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
<p id="uniq"> <p id="uniq">
<b class="method_name">uniq</b><code>_.uniq(array, [isSorted])</code> <b class="method_name">uniq</b><code>_.uniq(array, [isSorted])</code>
<br /> <br />
Produces a duplicate-free version of the <b>array</b>, using <i>==</i> to test Produces a duplicate-free version of the <b>array</b>, using <i>===</i> to test
object equality. If you know in advance that the <b>array</b> is sorted, object equality. If you know in advance that the <b>array</b> is sorted,
passing <i>true</i> for <b>isSorted</b> will run a much faster algorithm. passing <i>true</i> for <b>isSorted</b> will run a much faster algorithm.
</p> </p>
@@ -677,7 +677,11 @@ compiled({name : 'moe'});
var list = "&lt;% _.each(people, function(name) { %&gt; &lt;li&gt;&lt;%= name %&gt;&lt;/li&gt; &lt;% }); %&gt;"; var list = "&lt;% _.each(people, function(name) { %&gt; &lt;li&gt;&lt;%= name %&gt;&lt;/li&gt; &lt;% }); %&gt;";
_.template(list, {people : ['moe', 'curly', 'larry']}); _.template(list, {people : ['moe', 'curly', 'larry']});
=&gt; "&lt;li&gt;moe&lt;/li&gt;&lt;li&gt;curly&lt;/li&gt;&lt;li&gt;larry&lt;/li&gt;" =&gt; "&lt;li&gt;moe&lt;/li&gt;&lt;li&gt;curly&lt;/li&gt;&lt;li&gt;larry&lt;/li&gt;"
</pre> </pre>
<p>
<i><b>Underscore</b> is a <a href="http://documentcloud.org/">DocumentCloud</a> production.</i>
</p>
</div> </div>

View File

@@ -10,7 +10,7 @@ window._ = {
VERSION : '0.1.0', VERSION : '0.1.0',
// The cornerstone, an each implementation. // The cornerstone, an each implementation.
// Handles objects implementing forEach, _each, arrays, and raw objects. // Handles objects implementing forEach, each, arrays, and raw objects.
each : function(obj, iterator, context) { each : function(obj, iterator, context) {
var index = 0; var index = 0;
try { try {
@@ -47,7 +47,7 @@ window._ = {
}, },
// Inject builds up a single result from a list of values. Also known as // Inject builds up a single result from a list of values. Also known as
// reduce, and foldl. // reduce, or foldl.
inject : function(obj, memo, iterator, context) { inject : function(obj, memo, iterator, context) {
_.each(obj, function(value, index) { _.each(obj, function(value, index) {
memo = iterator.call(context, memo, value, index); memo = iterator.call(context, memo, value, index);
@@ -113,12 +113,12 @@ window._ = {
}, },
// Determine if a given value is included in the array or object, // Determine if a given value is included in the array or object,
// based on '=='. // based on '==='.
include : function(obj, target) { include : function(obj, target) {
if (_.isArray(obj)) return _.indexOf(obj, target) != -1; if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
var found = false; var found = false;
_.each(obj, function(pair) { _.each(obj, function(pair) {
if (pair.value == target) { if (pair.value === target) {
found = true; found = true;
throw '__break__'; throw '__break__';
} }