Underscore.js 1.2.0

This commit is contained in:
Jeremy Ashkenas
2011-10-05 17:30:20 -04:00
parent cc6a9d494d
commit 7304084e94
5 changed files with 248 additions and 131 deletions

View File

@@ -49,6 +49,14 @@
td {
padding: 2px 12px 2px 0;
}
ul {
list-style-type: circle;
padding: 0 0 0 20px;
}
li {
width: 500px;
margin-bottom: 10px;
}
code, pre, tt {
font-family: Monaco, Consolas, "Lucida Console", monospace;
font-size: 12px;
@@ -118,11 +126,11 @@
<table>
<tr>
<td><a href="underscore.js">Development Version (1.1.7)</a></td>
<td><a href="underscore.js">Development Version (1.2.0)</a></td>
<td><i>28kb, Uncompressed with Comments</i></td>
</tr>
<tr>
<td><a href="underscore-min.js">Production Version (1.1.7)</a></td>
<td><a href="underscore-min.js">Production Version (1.2.0)</a></td>
<td><i>3kb, Minified and Gzipped</i></td>
</tr>
</table>
@@ -149,7 +157,7 @@
<p>
<b>Arrays</b>
<br />
<span class="methods"><a href="#first">first</a>, <a href="#rest">rest</a>, <a href="#last">last</a>,
<span class="methods"><a href="#first">first</a>, <a href="#initial">initial</a>, <a href="#last">last</a>, <a href="#rest">rest</a>,
<a href="#compact">compact</a>, <a href="#flatten">flatten</a>, <a href="#without">without</a>,
<a href="#union">union</a>, <a href="#intersection">intersection</a>, <a href="#difference">difference</a>,
<a href="#uniq">uniq</a>, <a href="#zip">zip</a>, <a href="#indexOf">indexOf</a>,
@@ -468,7 +476,8 @@ _.sortedIndex([10, 20, 30, 40, 50], 35);
<p id="shuffle">
<b class="header">shuffle</b><code>_.shuffle(list)</code>
<br />
Returns a shuffled copy of <b>list</b>.
Returns a shuffled copy of the <b>list</b>, using a version of the
<a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle</a>.
</p>
<pre>
_.shuffle([1, 2, 3, 4, 5, 6]);
@@ -512,6 +521,29 @@ _.size({one : 1, two : 2, three : 3});
<pre>
_.first([5, 4, 3, 2, 1]);
=&gt; 5
</pre>
<p id="initial">
<b class="header">initial</b><code>_.initial(array, [n])</code>
<br />
Returns everything but the last entry of the array. Especially useful on
the arguments object. Pass <b>n</b> to exclude the last <b>n</b> elements
from the result.
</p>
<pre>
_.initial([5, 4, 3, 2, 1]);
=&gt; [5, 4, 3, 2]
</pre>
<p id="last">
<b class="header">last</b><code>_.last(array, [n])</code>
<br />
Returns the last element of an <b>array</b>. Passing <b>n</b> will return
the last <b>n</b> elements of the array.
</p>
<pre>
_.last([5, 4, 3, 2, 1]);
=&gt; 1
</pre>
<p id="rest">
@@ -524,16 +556,6 @@ _.first([5, 4, 3, 2, 1]);
<pre>
_.rest([5, 4, 3, 2, 1]);
=&gt; [4, 3, 2, 1]
</pre>
<p id="last">
<b class="header">last</b><code>_.last(array)</code>
<br />
Returns the last element of an <b>array</b>.
</p>
<pre>
_.last([5, 4, 3, 2, 1]);
=&gt; 1
</pre>
<p id="compact">
@@ -1162,7 +1184,8 @@ _.uniqueId('contact_');
for rendering. Useful for rendering complicated bits of HTML from JSON
data sources. Template functions can both interpolate variables, using<br />
<tt>&lt;%= &hellip; %&gt;</tt>, as well as execute arbitrary JavaScript code, with
<tt>&lt;% &hellip; %&gt;</tt>. When you evaluate a template function, pass in a
<tt>&lt;% &hellip; %&gt;</tt>. If you wish to interpolate a value, and have
it be HTML-escaped, use <tt>&lt;%- &hellip; %&gt;</tt> When you evaluate a template function, pass in a
<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>
object as the second parameter to <b>template</b> in order to render
@@ -1175,7 +1198,11 @@ compiled({name : 'moe'});
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']});
=&gt; "&lt;li&gt;moe&lt;/li&gt;&lt;li&gt;curly&lt;/li&gt;&lt;li&gt;larry&lt;/li&gt;"</pre>
=&gt; "&lt;li&gt;moe&lt;/li&gt;&lt;li&gt;curly&lt;/li&gt;&lt;li&gt;larry&lt;/li&gt;"
var template = _.template("&lt;b&gt;&lt;%- value %&gt;&lt;/b&gt;");
template({value : '&lt;script&gt;'});
=&gt; "&lt;b&gt;&amp;lt;script&amp;gt;&lt;/b&gt;"</pre>
<p>
You can also use <tt>print</tt> from within JavaScript code. This is
@@ -1315,6 +1342,34 @@ _([1, 2, 3]).value();
<h2 id="changelog">Change Log</h2>
<p>
<b class="header">1.2.0</b> &mdash; <small><i>Oct. 5, 2011</i></small><br />
<ul>
<li>
Underscore templates now support HTML escaping interpolations, using
<tt>&lt;%- ... %&gt;</tt> syntax. The <tt>_.isEqual</tt> function now
supports true deep equality comparisons, with checks for cyclic structures,
thanks to Kit Cambridge.
</li>
<li>
Ryan Tenney contributed <tt>_.shuffle</tt>, which uses a modified
Fisher-Yates to give you a shuffled copy of an array.
</li>
<li>
<tt>_.uniq</tt> can now be passed an optional iterator, to determine by
what criteria an object should be considered unique.
</li>
<li>
<tt>_.last</tt> now takes an optional argument which will return the last
N elements of the list.
</li>
<li>
A new <tt>_.initial</tt> function was added, as a mirror of <tt>_.rest</tt>,
which returns all the initial values of a list (except the last N).
</li>
</ul>
</p>
<p>
<b class="header">1.1.7</b> &mdash; <small><i>July 13, 2011</i></small><br />
Added <tt>_.groupBy</tt>, which aggregates a collection into groups of like items.