adding breakLoop

This commit is contained in:
Jeremy Ashkenas
2009-11-08 12:07:10 -05:00
parent cda4612a00
commit 5eec4e5d22
3 changed files with 146 additions and 118 deletions

View File

@@ -1,7 +1,7 @@
<!DOCTYPE HTML> <!DOCTYPE HTML>
<html> <html>
<head> <head>
<title>Underscore.js</title> <title>Underscore.js</title>
<style> <style>
body { body {
font-size: 16px; font-size: 16px;
@@ -65,45 +65,45 @@
</style> </style>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<h1>Underscore.js</h1> <h1>Underscore.js</h1>
<p> <p>
<a href="http://github.com/documentcloud/underscore/">Underscore</a> is a <a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
utility-belt library for JavaScript that provides a lot of the utility-belt library for JavaScript that provides a lot of the
functional programming support that you would expect in functional programming support that you would expect in
<a href="http://prototypejs.org/api">Prototype.js</a> <a href="http://prototypejs.org/api">Prototype.js</a>
(or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>), (or <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Ruby</a>),
but without extending any of the built-in JavaScript objects. It's the but without extending any of the built-in JavaScript objects. It's the
tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux. tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux.
</p> </p>
<p> <p>
Underscore provides 45-odd functions that support both the usual Underscore provides 45-odd functions that support both the usual
functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash; functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> &mdash;
as well as more specialized helpers: function binding, javascript as well as more specialized helpers: function binding, javascript
templating, deep equality testing, and so on. It delegates to built-in templating, deep equality testing, and so on. It delegates to built-in
functions, if present, so functions, if present, so
<a href="https://developer.mozilla.org/en/New_in_JavaScript_1.6">JavaScript 1.6</a> <a href="https://developer.mozilla.org/en/New_in_JavaScript_1.6">JavaScript 1.6</a>
compliant browsers will use the compliant browsers will use the
native implementations of <b>forEach</b>, <b>map</b>, <b>filter</b>, native implementations of <b>forEach</b>, <b>map</b>, <b>filter</b>,
<b>every</b>, <b>some</b> and <b>indexOf</b>. <b>every</b>, <b>some</b> and <b>indexOf</b>.
</p> </p>
<p> <p>
Underscore includes a complete <a href="test/test.html">Test &amp; Benchmark Suite</a> Underscore includes a complete <a href="test/test.html">Test &amp; Benchmark Suite</a>
for your perusal. for your perusal.
</p> </p>
<p> <p>
The unabridged source code is The unabridged source code is
<a href="http://github.com/documentcloud/underscore/">available on GitHub</a>. <a href="http://github.com/documentcloud/underscore/">available on GitHub</a>.
</p> </p>
<h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2> <h2>Downloads <i style="padding-left: 12px; font-size:12px;">(Right-click, and use "Save As")</i></h2>
<p> <p>
<table> <table>
<tr> <tr>
@@ -116,28 +116,28 @@
</tr> </tr>
</table> </table>
</p> </p>
<h2 id="styles">Object-Oriented and Functional Styles</h2> <h2 id="styles">Object-Oriented and Functional Styles</h2>
<p> <p>
You can use Underscore in either an object-oriented or a functional style, You can use Underscore in either an object-oriented or a functional style,
depending on your preference. The following two lines of code are depending on your preference. The following two lines of code are
identical ways to double a list of numbers. identical ways to double a list of numbers.
</p> </p>
<pre> <pre>
_.map([1, 2, 3], function(n){ return n * 2; }); _.map([1, 2, 3], function(n){ return n * 2; });
_([1, 2, 3]).map(function(n){ return n * 2; });</pre> _([1, 2, 3]).map(function(n){ return n * 2; });</pre>
<p> <p>
Using the object-oriented style allows you to chain together methods. Calling Using the object-oriented style allows you to chain together methods. Calling
<tt>chain</tt> on a wrapped object will cause all future method calls to <tt>chain</tt> on a wrapped object will cause all future method calls to
return wrapped objects as well. When you've finished the computation, return wrapped objects as well. When you've finished the computation,
use <tt>get</tt> to retrieve the final value. Here's an example of chaining use <tt>get</tt> to retrieve the final value. Here's an example of chaining
together a <b>map/flatten/reduce</b>, in order to get the word count of together a <b>map/flatten/reduce</b>, in order to get the word count of
every word in a song. every word in a song.
</p> </p>
<pre> <pre>
var lyrics = [ var lyrics = [
{line : 1, words : "I'm a lumberjack and I'm okay"}, {line : 1, words : "I'm a lumberjack and I'm okay"},
@@ -149,73 +149,73 @@ var lyrics = [
_(lyrics).chain() _(lyrics).chain()
.map(function(line) { return line.words.split(' '); }) .map(function(line) { return line.words.split(' '); })
.flatten() .flatten()
.reduce({}, function(counts, word) { .reduce({}, function(counts, word) {
counts[word] = (counts[word] || 0) + 1; counts[word] = (counts[word] || 0) + 1;
return counts; return counts;
}).get(); }).get();
=&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre> =&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>
<h2>Table of Contents</h2> <h2>Table of Contents</h2>
<p> <p>
<b>Collections</b> <b>Collections</b>
<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="#detect">detect</a>, <a href="#select">select</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>,
<a href="#max">max</a>, <a href="#min">min</a>, <a href="#max">max</a>, <a href="#min">min</a>,
<a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>, <a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>,
<a href="#toArray">toArray</a>, <a href="#size">size</a></span> <a href="#toArray">toArray</a>, <a href="#size">size</a></span>
</p> </p>
<p> <p>
<b>Arrays</b> <b>Arrays</b>
<br /> <br />
<span class="methods"><a href="#first">first</a>, <a href="#last">last</a>, <span class="methods"><a href="#first">first</a>, <a href="#last">last</a>,
<a href="#compact">compact</a>, <a href="#flatten">flatten</a>, <a href="#without">without</a>, <a href="#uniq">uniq</a>, <a href="#compact">compact</a>, <a href="#flatten">flatten</a>, <a href="#without">without</a>, <a href="#uniq">uniq</a>,
<a href="#intersect">intersect</a>, <a href="#zip">zip</a>, <a href="#indexOf">indexOf</a></span>, <a href="#intersect">intersect</a>, <a href="#zip">zip</a>, <a href="#indexOf">indexOf</a></span>,
<a href="#lastIndexOf">lastIndexOf</a></span> <a href="#lastIndexOf">lastIndexOf</a></span>
</p> </p>
<p> <p>
<b>Functions</b> <b>Functions</b>
<br /> <br />
<span class="methods"><a href="#bind">bind</a>, <a href="#bindAll">bindAll</a>, <a href="#delay">delay</a>, <span class="methods"><a href="#bind">bind</a>, <a href="#bindAll">bindAll</a>, <a href="#delay">delay</a>,
<a href="#defer">defer</a>, <a href="#wrap">wrap</a></span>, <a href="#compose">compose</a></span> <a href="#defer">defer</a>, <a href="#wrap">wrap</a></span>, <a href="#compose">compose</a></span>
</p> </p>
<p> <p>
<b>Objects</b> <b>Objects</b>
<br /> <br />
<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="#isUndefined">isUndefined</a>
</span> </span>
</p> </p>
<p> <p>
<b>Utility</b> <b>Utility</b>
<br /> <br />
<span class="methods"><a href="#noConflict">noConflict</a>, <span class="methods"><a href="#noConflict">noConflict</a>,
<a href="#identity">identity</a>, <a href="#uniqueId">uniqueId</a>, <a href="#identity">identity</a>, <a href="#breakLoop">breakLoop</a></span>,
<a href="#template">template</a></span> <a href="#uniqueId">uniqueId</a>, <a href="#template">template</a></span>
</p> </p>
<p> <p>
<b>Chaining</b> <b>Chaining</b>
<br /> <br />
<span class="methods"><a href="#chain">chain</a>, <a href="#get">get</a> <span class="methods"><a href="#chain">chain</a>, <a href="#get">get</a>
</p> </p>
<div id="documentation"> <div id="documentation">
<h2>Collection Functions (Arrays or Objects)</h2> <h2>Collection Functions (Arrays or Objects)</h2>
<p id="each"> <p id="each">
<b class="header">each</b><code>_.each(list, iterator, [context])</code> <b class="header">each</b><code>_.each(list, iterator, [context])</code>
<span class="alias">Alias: <b>forEach</b></span> <span class="alias">Alias: <b>forEach</b></span>
@@ -224,19 +224,19 @@ _(lyrics).chain()
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. Each invocation of <b>iterator</b> is called with three arguments: passed. Each invocation of <b>iterator</b> is called with three arguments:
<tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s <tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
arguments will be <tt>(value, key, list)</tt>. If the <b>list</b> has an <b>each</b> arguments will be <tt>(value, key, list)</tt>. If the <b>list</b> has an <b>each</b>
method of its own, it will be used instead. 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>
_.each([1, 2, 3], function(num){ alert(num); }); _.each([1, 2, 3], function(num){ alert(num); });
=&gt; alerts each number in turn...</pre> =&gt; alerts each number in turn...</pre>
<p id="map"> <p id="map">
<b class="header">map</b><code>_.map(list, iterator, [context])</code> <b class="header">map</b><code>_.map(list, iterator, [context])</code>
<br /> <br />
Produces a new array of values by mapping each value in <b>list</b> Produces a new array of values by mapping each value in <b>list</b>
through a transformation function (<b>iterator</b>). If the native through a transformation function (<b>iterator</b>). If the native
<b>map</b> method exists, it will be used instead. <b>map</b> method exists, it will be used instead.
</p> </p>
<pre> <pre>
@@ -247,7 +247,7 @@ _.map([1, 2, 3], function(num){ return num * 3 });
<b class="header">reduce</b><code>_.reduce(list, memo, iterator, [context])</code> <b class="header">reduce</b><code>_.reduce(list, memo, iterator, [context])</code>
<span class="alias">Aliases: <b>inject, foldl</b></span> <span class="alias">Aliases: <b>inject, foldl</b></span>
<br /> <br />
Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
<b>list</b> of values into a single value. <b>Memo</b> is the initial state <b>list</b> of values into a single value. <b>Memo</b> is the initial state
of the reduction, and each successive step of it should be returned by of the reduction, and each successive step of it should be returned by
<b>iterator</b>. <b>iterator</b>.
@@ -261,7 +261,7 @@ var sum = _.reduce([1, 2, 3], 0, function(memo, num){ return memo + num });
<b class="header">reduceRight</b><code>_.reduceRight(list, memo, iterator, [context])</code> <b class="header">reduceRight</b><code>_.reduceRight(list, memo, iterator, [context])</code>
<span class="alias">Alias: <b>foldr</b></span> <span class="alias">Alias: <b>foldr</b></span>
<br /> <br />
The right-associative version of <b>reduce</b>. Delegates to the The right-associative version of <b>reduce</b>. Delegates to the
JavaScript 1.8 version of <b>reduceRight</b>, if it exists. <b>Foldr</b> JavaScript 1.8 version of <b>reduceRight</b>, if it exists. <b>Foldr</b>
is not as useful in JavaScript as it would be in a language with lazy is not as useful in JavaScript as it would be in a language with lazy
evaluation. evaluation.
@@ -275,8 +275,8 @@ var flat = _.reduceRight(list, [], function(a, b) { return a.concat(b); });
<p id="detect"> <p id="detect">
<b class="header">detect</b><code>_.detect(list, iterator, [context])</code> <b class="header">detect</b><code>_.detect(list, iterator, [context])</code>
<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 an acceptable element, and doesn't traverse the soon as it finds an acceptable element, and doesn't traverse the
entire list. entire list.
</p> </p>
@@ -314,7 +314,7 @@ var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
<span class="alias">Alias: <b>every</b></span> <span class="alias">Alias: <b>every</b></span>
<br /> <br />
Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b> Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b>
truth test. If an <b>iterator</b> is not provided, the truthy value of truth test. If an <b>iterator</b> is not provided, the truthy value of
the element will be used instead. Delegates to the native method <b>every</b>, if the element will be used instead. Delegates to the native method <b>every</b>, if
present. present.
</p> </p>
@@ -364,7 +364,7 @@ _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
<p id="pluck"> <p id="pluck">
<b class="header">pluck</b><code>_.pluck(list, propertyName)</code> <b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
<br /> <br />
An convenient version of what is perhaps the most common use-case for An convenient version of what is perhaps the most common use-case for
<b>map</b>: extracting a list of property values. <b>map</b>: extracting a list of property values.
</p> </p>
<pre> <pre>
@@ -443,9 +443,9 @@ _.sortedIndex([10, 20, 30, 40, 50], 35);
_.size({one : 1, two : 2, three : 3}); _.size({one : 1, two : 2, three : 3});
=&gt; 3 =&gt; 3
</pre> </pre>
<h2>Array Functions</h2> <h2>Array Functions</h2>
<p id="first"> <p id="first">
<b class="header">first</b><code>_.first(array)</code> <b class="header">first</b><code>_.first(array)</code>
<br /> <br />
@@ -469,8 +469,8 @@ _.last([3, 2, 1]);
<p id="compact"> <p id="compact">
<b class="header">compact</b><code>_.compact(array)</code> <b class="header">compact</b><code>_.compact(array)</code>
<br /> <br />
Returns a copy of the <b>array</b> with all falsy values removed. Returns a copy of the <b>array</b> with all falsy values removed.
In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>, In JavaScript, <i>false</i>, <i>null</i>, <i>0</i>, <i>""</i>,
<i>undefined</i> and <i>NaN</i> are all falsy. <i>undefined</i> and <i>NaN</i> are all falsy.
</p> </p>
<pre> <pre>
@@ -537,7 +537,7 @@ _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
<p id="indexOf"> <p id="indexOf">
<b class="header">indexOf</b><code>_.indexOf(array, value)</code> <b class="header">indexOf</b><code>_.indexOf(array, value)</code>
<br /> <br />
Returns the index at which <b>value</b> can be found in the <b>array</b>, Returns the index at which <b>value</b> can be found in the <b>array</b>,
or <i>-1</i> if value is not present in the <b>array</b>. Uses the native or <i>-1</i> if value is not present in the <b>array</b>. Uses the native
<b>indexOf</b> function unless it's missing. <b>indexOf</b> function unless it's missing.
</p> </p>
@@ -549,8 +549,8 @@ _.indexOf([1, 2, 3], 2);
<p id="lastIndexOf"> <p id="lastIndexOf">
<b class="header">lastIndexOf</b><code>_.lastIndexOf(array, value)</code> <b class="header">lastIndexOf</b><code>_.lastIndexOf(array, value)</code>
<br /> <br />
Returns the index of the last occurrence of <b>value</b> in the <b>array</b>, Returns the index of the last occurrence of <b>value</b> in the <b>array</b>,
or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b> or <i>-1</i> if value is not present. Uses the native <b>lastIndexOf</b>
function if possible. function if possible.
</p> </p>
<pre> <pre>
@@ -566,7 +566,7 @@ _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
Bind a <b>function</b> to a <b>context</b> object, meaning that whenever Bind a <b>function</b> to a <b>context</b> object, meaning that whenever
the function is called, the value of <i>this</i> will be the <b>context</b>. the function is called, the value of <i>this</i> will be the <b>context</b>.
Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them, Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them,
also known as <b>currying</b>. also known as <b>currying</b>.
</p> </p>
<pre> <pre>
var func = function(greeting){ return greeting + ': ' + this.name }; var func = function(greeting){ return greeting + ': ' + this.name };
@@ -578,15 +578,15 @@ func();
<p id="bindAll"> <p id="bindAll">
<b class="header">bindAll</b><code>_.bindAll(*methodNames, context)</code> <b class="header">bindAll</b><code>_.bindAll(*methodNames, context)</code>
<br /> <br />
Binds a number of methods on the <b>context</b> object, specified by Binds a number of methods on the <b>context</b> object, specified by
<b>methodNames</b>, to be run in the context of that object whenever they <b>methodNames</b>, to be run in the context of that object whenever they
are invoked. Very handy for binding functions that are going to be used are invoked. Very handy for binding functions that are going to be used
as event handlers, which would otherwise be invoked with a fairly useless as event handlers, which would otherwise be invoked with a fairly useless
<i>this</i>. <i>this</i>.
</p> </p>
<pre> <pre>
var buttonView = { var buttonView = {
label : 'underscore', label : 'underscore',
onClick : function(){ alert('clicked: ' + this.label); }, onClick : function(){ alert('clicked: ' + this.label); },
onHover : function(){ console.log('hovering: ' + this.label); } onHover : function(){ console.log('hovering: ' + this.label); }
}; };
@@ -624,8 +624,8 @@ _.defer(function(){ alert('deferred'); });
<p id="wrap"> <p id="wrap">
<b class="header">wrap</b><code>_.wrap(function, wrapper)</code> <b class="header">wrap</b><code>_.wrap(function, wrapper)</code>
<br /> <br />
Wraps the first <b>function</b> inside of the <b>wrapper</b> function, Wraps the first <b>function</b> inside of the <b>wrapper</b> function,
passing it as the first argument. This allows the <b>wrapper</b> to passing it as the first argument. This allows the <b>wrapper</b> to
execute code before and after the <b>function</b> runs, adjust the arguments, execute code before and after the <b>function</b> runs, adjust the arguments,
and execute it conditionally. and execute it conditionally.
</p> </p>
@@ -641,7 +641,7 @@ hello();
<p id="compose"> <p id="compose">
<b class="header">compose</b><code>_.compose(*functions)</code> <b class="header">compose</b><code>_.compose(*functions)</code>
<br /> <br />
Returns the composition of a list of <b>functions</b>, where each function Returns the composition of a list of <b>functions</b>, where each function
consumes the return value of the function that follows. In math terms, consumes the return value of the function that follows. In math terms,
composing the functions <i>f()</i>, <i>g()</i>, and <i>h()</i> produces composing the functions <i>f()</i>, <i>g()</i>, and <i>h()</i> produces
<i>f(g(h()))</i>. <i>f(g(h()))</i>.
@@ -679,7 +679,7 @@ _.values({one : 1, two : 2, three : 3});
<p id="extend"> <p id="extend">
<b class="header">extend</b><code>_.extend(destination, source)</code> <b class="header">extend</b><code>_.extend(destination, source)</code>
<br /> <br />
Copy all of the properties in the <b>source</b> object over to the Copy all of the properties in the <b>source</b> object over to the
<b>destination</b> object. <b>destination</b> object.
</p> </p>
<pre> <pre>
@@ -768,7 +768,7 @@ _.isUndefined(window.missingVariable);
</pre> </pre>
<h2>Utility Functions</h2> <h2>Utility Functions</h2>
<p id="noConflict"> <p id="noConflict">
<b class="header">noConflict</b><code>_.noConflict()</code> <b class="header">noConflict</b><code>_.noConflict()</code>
<br /> <br />
@@ -781,20 +781,34 @@ var underscore = _.noConflict();</pre>
<p id="identity"> <p id="identity">
<b class="header">identity</b><code>_.identity(value)</code> <b class="header">identity</b><code>_.identity(value)</code>
<br /> <br />
Returns the same value that is used as the argument. In math: Returns the same value that is used as the argument. In math:
<tt>f(x) = x</tt><br /> <tt>f(x) = x</tt><br />
This function looks useless, but is used throughout Underscore as This function looks useless, but is used throughout Underscore as
a default iterator. a default iterator.
</p> </p>
<pre> <pre>
var moe = {name : 'moe'}; var moe = {name : 'moe'};
moe === _.identity(moe); moe === _.identity(moe);
=> true</pre> =&gt; true</pre>
<p id="breakLoop">
<b class="header">breakLoop</b><code>_.breakLoop()</code>
<br />
Breaks out of the current loop iteration. Similar to the <tt>break</tt>
keyword in regular "for" loop, but works within an iterator function.
</p>
<pre>
var result = null;
_.each([1, 2, 3], function(num) {
if ((result = num) == 2) _.breakLoop();
});
result;
=&gt; 2</pre>
<p id="uniqueId"> <p id="uniqueId">
<b class="header">uniqueId</b><code>_.uniqueId([prefix])</code> <b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>
<br /> <br />
Generate a globally-unique id for client-side models or DOM elements Generate a globally-unique id for client-side models or DOM elements
that need one. If <b>prefix</b> is passed, the id will be appended to it. that need one. If <b>prefix</b> is passed, the id will be appended to it.
</p> </p>
<pre> <pre>
@@ -819,7 +833,7 @@ _.functions();
Compiles JavaScript templates into functions that can be evaluated Compiles JavaScript templates into functions that can be evaluated
for rendering. Useful for rendering complicated bits of HTML from JSON for rendering. Useful for rendering complicated bits of HTML from JSON
data sources. Template functions can both interpolate variables, using<br /> data sources. Template functions can both interpolate variables, using<br />
<i>&lt;%= &hellip; %&gt;</i>, as well as execute arbitrary JavaScript code, with <i>&lt;%= &hellip; %&gt;</i>, as well as execute arbitrary JavaScript code, with
<i>&lt;% &hellip; %&gt;</i>. When you evaluate a template function, pass in a <i>&lt;% &hellip; %&gt;</i>. When you evaluate a template function, pass in a
<b>context</b> object that has properties corresponding to the template's free <b>context</b> object that has properties corresponding to the template's free
variables. If you're writing a one-off, you can pass the <b>context</b> variables. If you're writing a one-off, you can pass the <b>context</b>
@@ -834,10 +848,10 @@ 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>
<h2>Chaining</h2> <h2>Chaining</h2>
<p id="chain"> <p id="chain">
<b class="header">chain</b><code>_(obj).chain()</code> <b class="header">chain</b><code>_(obj).chain()</code>
<br /> <br />
@@ -861,30 +875,30 @@ _([1, 2, 3]).get();
</pre> </pre>
<h2>Change Log</h2> <h2>Change Log</h2>
<p> <p>
<b class="header">0.4.0</b><br /> <b class="header">0.4.0</b><br />
All Underscore functions can now be called in an object-oriented style, All Underscore functions can now be called in an object-oriented style,
like so: <tt>_([1, 2, 3]).map(...);</tt>. Original patch provided by like so: <tt>_([1, 2, 3]).map(...);</tt>. Original patch provided by
<a href="http://macournoyer.com/">Marc-André Cournoyer</a>. <a href="http://macournoyer.com/">Marc-André Cournoyer</a>.
Wrapped objects can be chained through multiple Wrapped objects can be chained through multiple
method invocations. A <a href="#functions"><tt>functions</tt></a> method method invocations. A <a href="#functions"><tt>functions</tt></a> method
was added, providing a sorted list of all the functions in Underscore. was added, providing a sorted list of all the functions in Underscore.
</p> </p>
<p> <p>
<b class="header">0.3.3</b><br /> <b class="header">0.3.3</b><br />
Added the JavaScript 1.8 function <tt>reduceRight</tt>. Aliased it Added the JavaScript 1.8 function <tt>reduceRight</tt>. Aliased it
as <tt>foldr</tt>, and aliased <tt>reduce</tt> as <tt>foldl</tt>. as <tt>foldr</tt>, and aliased <tt>reduce</tt> as <tt>foldl</tt>.
</p> </p>
<p> <p>
<b class="header">0.3.2</b><br /> <b class="header">0.3.2</b><br />
Now runs on stock <a href="http://www.mozilla.org/rhino/">Rhino</a> Now runs on stock <a href="http://www.mozilla.org/rhino/">Rhino</a>
interpreters with: <tt>load("underscore.js")</tt>. interpreters with: <tt>load("underscore.js")</tt>.
Added <a href="#identity"><tt>identity</tt></a> as a utility function. Added <a href="#identity"><tt>identity</tt></a> as a utility function.
</p> </p>
<p> <p>
<b class="header">0.3.1</b><br /> <b class="header">0.3.1</b><br />
All iterators are now passed in the original collection as their third All iterators are now passed in the original collection as their third
@@ -892,29 +906,29 @@ _([1, 2, 3]).get();
objects is now called with <tt>(value, key, collection)</tt>, for details objects is now called with <tt>(value, key, collection)</tt>, for details
see <a href="#each"><tt>_.each</tt></a>. see <a href="#each"><tt>_.each</tt></a>.
</p> </p>
<p> <p>
<b class="header">0.3.0</b><br /> <b class="header">0.3.0</b><br />
Added <a href="http://github.com/dmitryBaranovskiy">Dmitry Baranovskiy</a>'s Added <a href="http://github.com/dmitryBaranovskiy">Dmitry Baranovskiy</a>'s
comprehensive optimizations, merged in comprehensive optimizations, merged in
<a href="http://github.com/kriskowal/">Kris Kowal</a>'s patches to make Underscore <a href="http://github.com/kriskowal/">Kris Kowal</a>'s patches to make Underscore
<a href="http://wiki.commonjs.org/wiki/CommonJS">CommonJS</a> and <a href="http://wiki.commonjs.org/wiki/CommonJS">CommonJS</a> and
<a href="http://narwhaljs.org/">Narwhal</a> compliant. <a href="http://narwhaljs.org/">Narwhal</a> compliant.
</p> </p>
<p> <p>
<b class="header">0.2.0</b><br /> <b class="header">0.2.0</b><br />
Added <tt>compose</tt> and <tt>lastIndexOf</tt>, renamed <tt>inject</tt> to Added <tt>compose</tt> and <tt>lastIndexOf</tt>, renamed <tt>inject</tt> to
<tt>reduce</tt>, added aliases for <tt>inject</tt>, <tt>filter</tt>, <tt>reduce</tt>, added aliases for <tt>inject</tt>, <tt>filter</tt>,
<tt>every</tt>, <tt>some</tt>, and <tt>forEach</tt>. <tt>every</tt>, <tt>some</tt>, and <tt>forEach</tt>.
</p> </p>
<p> <p>
<b class="header">0.1.1</b><br /> <b class="header">0.1.1</b><br />
Added <tt>noConflict</tt>, so that the "Underscore" object can be assigned to Added <tt>noConflict</tt>, so that the "Underscore" object can be assigned to
other variables. other variables.
</p> </p>
<p> <p>
<b class="header">0.1.0</b><br /> <b class="header">0.1.0</b><br />
Initial release of Underscore.js. Initial release of Underscore.js.
@@ -925,9 +939,9 @@ _([1, 2, 3]).get();
<img src="http://jashkenas.s3.amazonaws.com/images/a_documentcloud_project.png" alt="A DocumentCloud Project" /> <img src="http://jashkenas.s3.amazonaws.com/images/a_documentcloud_project.png" alt="A DocumentCloud Project" />
</a> </a>
</p> </p>
</div> </div>
</div> </div>
</body> </body>

View File

@@ -14,6 +14,15 @@ $(document).ready(function() {
var moe = {name : 'moe'}; var moe = {name : 'moe'};
equals(_.identity(moe), moe, 'moe is the same as his identity'); 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) {
result = num;
if (num == 3) _.breakLoop();
});
equals(result, 3, 'broke out of a loop');
});
test("utility: uniqueId", function() { test("utility: uniqueId", function() {
var ids = [], i = 0; var ids = [], i = 0;
@@ -22,7 +31,7 @@ $(document).ready(function() {
}); });
test("utility: functions", function() { test("utility: functions", function() {
var expected = ["all", "any", "bind", "bindAll", "clone", "compact", "compose", var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact", "compose",
"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",

View File

@@ -90,7 +90,7 @@
_.each(obj, function(value, index, list) { _.each(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) { if (iterator.call(context, value, index, list)) {
result = value; result = value;
throw '__break__'; _.breakLoop();
} }
}); });
return result; return result;
@@ -123,7 +123,7 @@
if (obj.every) return obj.every(iterator, context); if (obj.every) return obj.every(iterator, context);
var result = true; var result = true;
_.each(obj, function(value, index, list) { _.each(obj, function(value, index, list) {
if (!(result = result && iterator.call(context, value, index, list))) throw '__break__'; if (!(result = result && iterator.call(context, value, index, list))) _.breakLoop();
}); });
return result; return result;
}; };
@@ -135,7 +135,7 @@
if (obj.some) return obj.some(iterator, context); if (obj.some) return obj.some(iterator, context);
var result = false; var result = false;
_.each(obj, function(value, index, list) { _.each(obj, function(value, index, list) {
if (result = iterator.call(context, value, index, list)) throw '__break__'; if (result = iterator.call(context, value, index, list)) _.breakLoop();
}); });
return result; return result;
}; };
@@ -146,7 +146,7 @@
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(value) { _.each(obj, function(value) {
if (found = value === target) throw '__break__'; if (found = value === target) _.breakLoop();
}); });
return found; return found;
}; };
@@ -446,6 +446,11 @@
return value; return value;
}; };
// Break out of the middle of an iteration.
_.breakLoop = function() {
throw "__break__";
};
// Generate a unique integer id (unique within the entire client session). // Generate a unique integer id (unique within the entire client session).
// Useful for temporary DOM ids. // Useful for temporary DOM ids.
var idCounter = 0; var idCounter = 0;