Skip to content

Instantly share code, notes, and snippets.

@mitsuruog
Created July 22, 2013 10:18
Show Gist options
  • Save mitsuruog/6052821 to your computer and use it in GitHub Desktop.
Save mitsuruog/6052821 to your computer and use it in GitHub Desktop.
git diff 1.4.3...HEAD index.htmlした結果
diff --git a/index.html b/index.html
index e62aa92..0845ce5 100644
--- a/index.html
+++ b/index.html
@@ -3,7 +3,9 @@
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
- <meta name="viewport" content="width=device-width" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+ <meta name="viewport" content="target-densitydpi=device-dpi" />
+ <meta name="HandheldFriendly" content="true"/>
<link rel="canonical" href="http://underscorejs.org" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<title>Underscore.js</title>
@@ -179,8 +181,12 @@
<div id="sidebar" class="interface">
<a class="toc_title" href="#">
- Underscore.js <span class="version">(1.4.3)</span>
+ Underscore.js <span class="version">(1.5.1)</span>
</a>
+ <ul class="toc_section">
+ <li>&raquo; <a href="http://github.com/jashkenas/underscore">GitHub Repository</a></li>
+ <li>&raquo; <a href="docs/underscore.html">Annotated Source</a></li>
+ </ul>
<a class="toc_title" href="#">
Introduction
@@ -197,6 +203,7 @@
<li>- <a href="#find">find</a></li>
<li>- <a href="#filter">filter</a></li>
<li>- <a href="#where">where</a></li>
+ <li>- <a href="#findWhere">findWhere</a></li>
<li>- <a href="#reject">reject</a></li>
<li>- <a href="#every">every</a></li>
<li>- <a href="#some">some</a></li>
@@ -242,6 +249,7 @@
<ul class="toc_section">
<li>- <a href="#bind">bind</a></li>
<li>- <a href="#bindAll">bindAll</a></li>
+ <li>- <a href="#partial">partial</a></li>
<li>- <a href="#memoize">memoize</a></li>
<li>- <a href="#delay">delay</a></li>
<li>- <a href="#defer">defer</a></li>
@@ -328,13 +336,13 @@
</p>
<p>
- <a href="http://github.com/documentcloud/underscore/">Underscore</a> is a
+ <a href="http://github.com/jashkenas/underscore/">Underscore</a> is a
utility-belt library for JavaScript that provides a lot of the
functional programming support that you would expect in
<a href="http://prototypejs.org/doc/latest/">Prototype.js</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
- tie to go along with <a href="http://docs.jquery.com">jQuery</a>'s tux,
+ tie to go along with <a href="http://jquery.com">jQuery</a>'s tux,
and <a href="http://backbonejs.org">Backbone.js</a>'s suspenders.
</p>
@@ -359,9 +367,9 @@
<p>
The project is
- <a href="http://github.com/documentcloud/underscore/">hosted on GitHub</a>.
+ <a href="http://github.com/jashkenas/underscore/">hosted on GitHub</a>.
You can report bugs and discuss features on the
- <a href="http://github.com/documentcloud/underscore/issues">issues page</a>,
+ <a href="http://github.com/jashkenas/underscore/issues">issues page</a>,
on Freenode in the <tt>#documentcloud</tt> channel,
or send tweets to <a href="http://twitter.com/documentcloud">@documentcloud</a>.
</p>
@@ -374,18 +382,21 @@
<table>
<tr>
- <td><a href="underscore.js">Development Version (1.4.3)</a></td>
- <td><i>40kb, Uncompressed with Plentiful Comments</i></td>
+ <td><a href="underscore.js">Development Version (1.5.1)</a></td>
+ <td><i>41kb, Uncompressed with Plentiful Comments</i></td>
</tr>
<tr>
- <td><a href="underscore-min.js">Production Version (1.4.3)</a></td>
- <td><i>4kb, Minified and Gzipped</i></td>
+ <td><a href="underscore-min.js">Production Version (1.5.1)</a></td>
+ <td>
+ <i>5kb, Minified and Gzipped</i>
+ &nbsp;<small>(<a href="underscore-min.map">Source Map</a>)</small>
+ </td>
</tr>
<tr>
<td colspan="2"><div class="rule"></div></td>
</tr>
<tr>
- <td><a href="https://raw.github.com/documentcloud/underscore/master/underscore.js">Edge Version</a></td>
+ <td><a href="https://raw.github.com/jashkenas/underscore/master/underscore.js">Edge Version</a></td>
<td><i>Unreleased, current <tt>master</tt>, use at your own risk</i></td>
</tr>
</table>
@@ -411,6 +422,15 @@ _.each([1, 2, 3], alert);
_.each({one : 1, two : 2, three : 3}, alert);
=&gt; alerts each number value in turn...</pre>
+ <p>
+ <i>
+ Note: Collection functions work on arrays, objects, and
+ array-like objects such as</i> <tt>arguments</tt>, <tt>NodeList</tt><i>
+ and similar. But it works by duck-typing, so avoid passing objects with
+ a numeric <tt>length</tt> property.
+ </i>
+ </p>
+
<p id="map">
<b class="header">map</b><code>_.map(list, iterator, [context])</code>
<span class="alias">Alias: <b>collect</b></span>
@@ -462,7 +482,8 @@ var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
<span class="alias">Alias: <b>detect</b></span>
<br />
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>), or <code>undefined</code> if no value
+ passes the test. The function returns as
soon as it finds an acceptable element, and doesn't traverse the
entire list.
</p>
@@ -496,6 +517,20 @@ _.where(listOfPlays, {author: "Shakespeare", year: 1611});
{title: "The Tempest", author: "Shakespeare", year: 1611}]
</pre>
+ <p id="findWhere">
+ <b class="header">findWhere</b><code>_.findWhere(list, properties)</code>
+ <br />
+ Looks through the <b>list</b> and returns the <i>first</i> value that matches
+ all of the key-value pairs listed in <b>properties</b>.
+ </p>
+ <pre>
+_.findWhere(publicServicePulitzers, {newsroom: "The New York Times"});
+=&gt; {year: 1918, newsroom: "The New York Times",
+ reason: "For its public service in publishing in full so many official reports,
+ documents and speeches by European statesmen relating to the progress and
+ conduct of the war."}
+</pre>
+
<p id="reject">
<b class="header">reject</b><code>_.reject(list, iterator, [context])</code>
<br />
@@ -508,7 +543,7 @@ var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
</pre>
<p id="every">
- <b class="header">every</b><code>_.every(list, iterator, [context])</code>
+ <b class="header">every</b><code>_.every(list, [iterator], [context])</code>
<span class="alias">Alias: <b>all</b></span>
<br />
Returns <i>true</i> if all of the values in the <b>list</b> pass the <b>iterator</b>
@@ -598,9 +633,10 @@ _.min(numbers);
<p id="sortBy">
<b class="header">sortBy</b><code>_.sortBy(list, iterator, [context])</code>
<br />
- Returns a sorted copy of <b>list</b>, ranked in ascending order by the
- results of running each value through <b>iterator</b>. Iterator may
- also be the string name of the property to sort by (eg. <tt>length</tt>).
+ Returns a (stably) sorted copy of <b>list</b>, ranked in ascending
+ order by the results of running each value through <b>iterator</b>.
+ Iterator may also be the string name of the property to sort by (eg.
+ <tt>length</tt>).
</p>
<pre>
_.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
@@ -608,7 +644,7 @@ _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
</pre>
<p id="groupBy">
- <b class="header">groupBy</b><code>_.groupBy(list, iterator)</code>
+ <b class="header">groupBy</b><code>_.groupBy(list, iterator, [context])</code>
<br />
Splits a collection into sets, grouped by the result of running each
value through <b>iterator</b>. If <b>iterator</b> is a string instead of
@@ -624,7 +660,7 @@ _.groupBy(['one', 'two', 'three'], 'length');
</pre>
<p id="countBy">
- <b class="header">countBy</b><code>_.countBy(list, iterator)</code>
+ <b class="header">countBy</b><code>_.countBy(list, iterator, [context])</code>
<br />
Sorts a list into groups and returns a count for the number of objects
in each group.
@@ -652,8 +688,8 @@ _.shuffle([1, 2, 3, 4, 5, 6]);
<p id="toArray">
<b class="header">toArray</b><code>_.toArray(list)</code>
<br />
- Converts the <b>list</b> (anything that can be iterated over), into a
- real Array. Useful for transmuting the <b>arguments</b> object.
+ Creates a real Array from the <b>list</b> (anything that can be
+ iterated over). Useful for transmuting the <b>arguments</b> object.
</p>
<pre>
(function(){ return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
@@ -817,12 +853,15 @@ _.uniq([1, 2, 1, 3, 1, 4]);
Merges together the values of each of the <b>arrays</b> with the
values at the corresponding position. Useful when you have separate
data sources that are coordinated through matching array indexes.
- If you're working with a matrix of nested arrays, <b>zip.apply</b>
+ If you're working with a matrix of nested arrays, <tt>_.zip.apply</tt>
can transpose the matrix in a similar fashion.
</p>
<pre>
_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
=&gt; [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
+
+_.zip.apply(_, arrayOfRowsOfData);
+=&gt; arrayOfColumnsOfData
</pre>
<p id="object">
@@ -830,6 +869,7 @@ _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
<br />
Converts arrays into objects. Pass either a single list of
<tt>[key, value]</tt> pairs, or a list of keys, and a list of values.
+ If duplicate keys exist, the last value wins.
</p>
<pre>
_.object(['moe', 'larry', 'curly'], [30, 40, 50]);
@@ -869,16 +909,21 @@ _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
</pre>
<p id="sortedIndex">
- <b class="header">sortedIndex</b><code>_.sortedIndex(list, value, [iterator])</code>
+ <b class="header">sortedIndex</b><code>_.sortedIndex(list, value, [iterator], [context])</code>
<br />
Uses a binary search to determine the index at which the <b>value</b>
<i>should</i> be inserted into the <b>list</b> in order to maintain the <b>list</b>'s
sorted order. If an <b>iterator</b> is passed, it will be used to compute
the sort ranking of each value, including the <b>value</b> you pass.
+ Iterator may also be the string name of the property to sort by (eg. <tt>length</tt>).
</p>
<pre>
_.sortedIndex([10, 20, 30, 40, 50], 35);
=&gt; 3
+
+var stooges = [{name : 'moe', age : 40}, {name : 'curly', age : 60}];
+_.sortedIndex(stooges, {name : 'larry', age : 50}, 'age');
+=&gt; 1
</pre>
<p id="range">
@@ -910,7 +955,7 @@ _.range(0);
<br />
Bind a <b>function</b> to an <b>object</b>, meaning that whenever
the function is called, the value of <i>this</i> will be the <b>object</b>.
- Optionally, bind <b>arguments</b> to the <b>function</b> to pre-fill them,
+ Optionally, pass <b>arguments</b> to the <b>function</b> to pre-fill them,
also known as <b>partial application</b>.
</p>
<pre>
@@ -921,14 +966,13 @@ func();
</pre>
<p id="bindAll">
- <b class="header">bindAll</b><code>_.bindAll(object, [*methodNames])</code>
+ <b class="header">bindAll</b><code>_.bindAll(object, *methodNames)</code>
<br />
Binds a number of methods on the <b>object</b>, specified by
<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
as event handlers, which would otherwise be invoked with a fairly useless
- <i>this</i>. If no <b>methodNames</b> are provided, all of the object's
- function properties will be bound to it.
+ <i>this</i>. <b>methodNames</b> are required.
</p>
<pre>
var buttonView = {
@@ -936,9 +980,23 @@ var buttonView = {
onClick : function(){ alert('clicked: ' + this.label); },
onHover : function(){ console.log('hovering: ' + this.label); }
};
-_.bindAll(buttonView);
+_.bindAll(buttonView, 'onClick', 'onHover');
+// When the button is clicked, this.label will have the correct value.
jQuery('#underscore_button').bind('click', buttonView.onClick);
-=&gt; When the button is clicked, this.label will have the correct value...
+</pre>
+
+ <p id="partial">
+ <b class="header">partial</b><code>_.partial(function, [*arguments])</code>
+ <br />
+ Partially apply a function by filling in any number of its arguments,
+ <i>without</i> changing its dynamic <tt>this</tt> value. A close cousin
+ of <a href="#bind">bind</a>.
+ </p>
+ <pre>
+var add = function(a, b) { return a + b; };
+add5 = _.partial(add, 5);
+add5(10);
+=&gt; 15
</pre>
<p id="memoize">
@@ -985,7 +1043,7 @@ _.defer(function(){ alert('deferred'); });
</pre>
<p id="throttle">
- <b class="header">throttle</b><code>_.throttle(function, wait)</code>
+ <b class="header">throttle</b><code>_.throttle(function, wait, [options])</code>
<br />
Creates and returns a new, throttled version of the passed function,
that, when invoked repeatedly, will only actually call the original function
@@ -993,6 +1051,14 @@ _.defer(function(){ alert('deferred'); });
milliseconds. Useful for rate-limiting events that occur faster than you
can keep up with.
</p>
+ <p>
+ By default, <b>throttle</b> will execute the function as soon as you call it
+ for the first time, and, if you call it again any number of times
+ during the <b>wait</b> period, as soon as that period is over.
+ If you'd like to disable the leading-edge
+ call, pass <tt>{leading: false}</tt>, and if you'd like to disable the
+ execution on the trailing-edge, pass <br /> <tt>{trailing: false}</tt>.
+ </p>
<pre>
var throttled = _.throttle(updatePosition, 100);
$(window).scroll(throttled);
@@ -1001,7 +1067,7 @@ $(window).scroll(throttled);
<p id="debounce">
<b class="header">debounce</b><code>_.debounce(function, wait, [immediate])</code>
<br />
- Creates and returns a new debounced version of the passed function that
+ Creates and returns a new debounced version of the passed function which
will postpone its execution until after
<b>wait</b> milliseconds have elapsed since the last time it
was invoked. Useful for implementing behavior that should only happen
@@ -1181,7 +1247,7 @@ _.omit({name : 'moe', age : 50, userid : 'moe1'}, 'userid');
<p id="defaults">
<b class="header">defaults</b><code>_.defaults(object, *defaults)</code>
<br />
- Fill in null and undefined properties in <b>object</b> with values from the
+ Fill in <tt>undefined</tt> properties in <b>object</b> with values from the
<b>defaults</b> objects, and return the <b>object</b>. As soon as the
property is filled, further defaults will have no effect.
</p>
@@ -1249,7 +1315,8 @@ _.isEqual(moe, clone);
<p id="isEmpty">
<b class="header">isEmpty</b><code>_.isEmpty(object)</code>
<br />
- Returns <i>true</i> if <b>object</b> contains no values.
+ Returns <i>true</i> if <b>object</b> contains no values
+ (no enumerable own-properties).
</p>
<pre>
_.isEmpty([1, 2, 3]);
@@ -1444,7 +1511,8 @@ moe === _.identity(moe);
<b class="header">times</b><code>_.times(n, iterator, [context])</code>
<br />
Invokes the given iterator function <b>n</b> times. Each invocation of
- <b>iterator</b> is called with an <tt>index</tt> argument.
+ <b>iterator</b> is called with an <tt>index</tt> argument. Produces an
+ array of the returned values.
<br />
<i>Note: this example uses the <a href="#chaining">chaining syntax</a></i>.
</p>
@@ -1484,7 +1552,6 @@ _("fabio").capitalize();
<br />
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.
- Without <b>prefix</b>, returns an integer.
</p>
<pre>
_.uniqueId('contact_');
@@ -1509,13 +1576,14 @@ _.escape('Curly, Larry &amp; Moe');
with their unescaped counterparts.
</p>
<pre>
-_.escape('Curly, Larry &amp;amp; Moe');
+_.unescape('Curly, Larry &amp;amp; Moe');
=&gt; "Curly, Larry &amp; Moe"</pre>
<p id="result">
<b class="header">result</b><code>_.result(object, property)</code>
<br />
- If the value of the named property is a function then invoke it; otherwise, return it.
+ If the value of the named <b>property</b> is a function then invoke it
+ with the <b>object</b> as context; otherwise, return it.
</p>
<pre>
var object = {cheese: 'crumpets', stuff: function(){ return 'nonsense'; }};
@@ -1643,7 +1711,8 @@ _.chain(lyrics)
.reduce(function(counts, word) {
counts[word] = (counts[word] || 0) + 1;
return counts;
-}, {}).value();
+ }, {})
+ .value();
=&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>
@@ -1763,7 +1832,75 @@ _([1, 2, 3]).value();
<h2 id="changelog">Change Log</h2>
<p>
- <b class="header">1.4.3</b> &mdash; <small><i>Dec. 4, 2012</i></small> &mdash; <a href="https://github.com/documentcloud/underscore/compare/1.4.2...1.4.3">Diff</a><br />
+ <b class="header">1.5.1</b> &mdash; <small><i>Jul. 8, 2013</i></small> &mdash; <a href="https://github.com/jashkenas/underscore/compare/1.5.0...1.5.1">Diff</a><br />
+ <ul>
+ <li>
+ Removed <tt>unzip</tt>, as it's simply the application of <tt>zip</tt>
+ to an array of arguments. Use <tt>_.zip.apply(_, list)</tt> to
+ transpose instead.
+ </li>
+ </ul>
+ </p>
+
+ <p>
+ <b class="header">1.5.0</b> &mdash; <small><i>Jul. 6, 2013</i></small> &mdash; <a href="https://github.com/jashkenas/underscore/compare/1.4.4...1.5.0">Diff</a><br />
+ <ul>
+ <li>
+ Added a new <tt>unzip</tt> function, as the inverse of <tt>_.zip</tt>.
+ </li>
+ <li>
+ The <tt>throttle</tt> function now takes an <tt>options</tt> argument,
+ allowing you to disable execution of the throttled function on either
+ the <b>leading</b> or <b>trailing</b> edge.
+ </li>
+ <li>
+ A source map is now supplied for easier debugging of the minified
+ production build of Underscore.
+ </li>
+ <li>
+ The <tt>defaults</tt> function now only overrides <tt>undefined</tt>
+ values, not <tt>null</tt> ones.
+ </li>
+ <li>
+ Removed the ability to call <tt>_.bindAll</tt> with no method name
+ arguments. It's pretty much always wiser to white-list the names of
+ the methods you'd like to bind.
+ </li>
+ <li>
+ Removed the ability to call <tt>_.after</tt> with an invocation count
+ of zero. The minimum number of calls is (naturally) now <tt>1</tt>.
+ </li>
+ </ul>
+ </p>
+
+ <p>
+ <b class="header">1.4.4</b> &mdash; <small><i>Jan. 30, 2013</i></small> &mdash; <a href="https://github.com/jashkenas/underscore/compare/1.4.3...1.4.4">Diff</a><br />
+ <ul>
+ <li>
+ Added <tt>_.findWhere</tt>, for finding the first element in a list
+ that matches a particular set of keys and values.
+ </li>
+ <li>
+ Added <tt>_.partial</tt>, for partially applying a function <i>without</i>
+ changing its dynamic reference to <tt>this</tt>.
+ </li>
+ <li>
+ Simplified <tt>bind</tt> by removing some edge cases involving
+ constructor functions. In short: don't <tt>_.bind</tt> your
+ constructors.
+ </li>
+ <li>
+ A minor optimization to <tt>invoke</tt>.
+ </li>
+ <li>
+ Fix bug in the minified version due to the minifier incorrectly
+ optimizing-away <tt>isFunction</tt>.
+ </li>
+ </ul>
+ </p>
+
+ <p>
+ <b class="header">1.4.3</b> &mdash; <small><i>Dec. 4, 2012</i></small> &mdash; <a href="https://github.com/jashkenas/underscore/compare/1.4.2...1.4.3">Diff</a><br />
<ul>
<li>
Improved Underscore compatibility with Adobe's JS engine that can be
@@ -1788,7 +1925,7 @@ _([1, 2, 3]).value();
</p>
<p>
- <b class="header">1.4.2</b> &mdash; <small><i>Oct. 1, 2012</i></small> &mdash; <a href="https://github.com/documentcloud/underscore/compare/1.4.1...1.4.2">Diff</a><br />
+ <b class="header">1.4.2</b> &mdash; <small><i>Oct. 1, 2012</i></small> &mdash; <a href="https://github.com/jashkenas/underscore/compare/1.4.1...1.4.2">Diff</a><br />
<ul>
<li>
For backwards compatibility, returned to pre-1.4.0 behavior when
@@ -1799,7 +1936,7 @@ _([1, 2, 3]).value();
</p>
<p>
- <b class="header">1.4.1</b> &mdash; <small><i>Oct. 1, 2012</i></small> &mdash; <a href="https://github.com/documentcloud/underscore/compare/1.4.0...1.4.1">Diff</a><br />
+ <b class="header">1.4.1</b> &mdash; <small><i>Oct. 1, 2012</i></small> &mdash; <a href="https://github.com/jashkenas/underscore/compare/1.4.0...1.4.1">Diff</a><br />
<ul>
<li>
Fixed a 1.4.0 regression in the <tt>lastIndexOf</tt> function.
@@ -1808,7 +1945,7 @@ _([1, 2, 3]).value();
</p>
<p>
- <b class="header">1.4.0</b> &mdash; <small><i>Sept. 27, 2012</i></small> &mdash; <a href="https://github.com/documentcloud/underscore/compare/1.3.3...1.4.0">Diff</a><br />
+ <b class="header">1.4.0</b> &mdash; <small><i>Sept. 27, 2012</i></small> &mdash; <a href="https://github.com/jashkenas/underscore/compare/1.3.3...1.4.0">Diff</a><br />
<ul>
<li>
Added a <tt>pairs</tt> function, for turning a JavaScript object
@@ -1865,7 +2002,7 @@ _([1, 2, 3]).value();
</li>
<li>
A number of edge-cases fixes and tweaks, which you can spot in the
- <a href="https://github.com/documentcloud/underscore/compare/1.3.3...1.4.0">diff</a>.
+ <a href="https://github.com/jashkenas/underscore/compare/1.3.3...1.4.0">diff</a>.
Depending on how you're using Underscore, <b>1.4.0</b> may be more
backwards-incompatible than usual &mdash; please test when you upgrade.
</li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment