mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
339 lines
11 KiB
HTML
339 lines
11 KiB
HTML
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<title>Underscore.js</title>
|
|
<style>
|
|
body {
|
|
font-size: 16px;
|
|
line-height: 24px;
|
|
background: #f0f0e5;
|
|
color: #252519;
|
|
font-family: "Palatino Linotype", "Book Antiqua", Palatino, FreeSerif, serif;
|
|
}
|
|
div.container {
|
|
width: 720px;
|
|
margin: 0 auto;
|
|
margin-top: 50px;
|
|
}
|
|
p {
|
|
width: 550px;
|
|
}
|
|
#documentation p {
|
|
margin-bottom: 8px;
|
|
}
|
|
a, a:visited {
|
|
padding: 0 2px;
|
|
text-decoration: none;
|
|
background: #dadaba;
|
|
color: #252519;
|
|
}
|
|
a:active, a:hover {
|
|
color: #000;
|
|
background: #f0c095;
|
|
}
|
|
h1, h2, h3, h4, h5, h6 {
|
|
margin-top: 35px;
|
|
}
|
|
code, pre {
|
|
font-family: Monaco, Consolas, "Lucida Console", monospace;
|
|
font-size: 12px;
|
|
line-height: 18px;
|
|
color: #555529;
|
|
}
|
|
code {
|
|
margin-left: 20px;
|
|
}
|
|
pre {
|
|
font-size: 12px;
|
|
padding-left: 12px;
|
|
border-left: 6px solid #aaaa99;
|
|
margin: 0px 0 35px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="container">
|
|
|
|
<h1>Underscore.js</h1>
|
|
|
|
<p>
|
|
<a href="http://github.com/documentcloud/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/api">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.
|
|
</p>
|
|
|
|
<p>
|
|
Underscore provides 43-odd functions that support both the usual
|
|
functional suspects: <b>map</b>, <b>select</b>, <b>invoke</b> —
|
|
as well as more specialized helpers: function binding, javascript
|
|
templating, deep equality testing, and so on. It delegates to built-in
|
|
functions, if present, so
|
|
<a href="https://developer.mozilla.org/en/New_in_JavaScript_1.6">Javascript 1.6</a>
|
|
compliant browsers will use the
|
|
native implementations of <b>forEach</b>, <b>map</b>, <b>filter</b>,
|
|
<b>every</b>, <b>some</b> and <b>indexOf</b>.
|
|
</p>
|
|
|
|
<p>
|
|
Underscore includes a complete <a href="test/test.html">Test & Benchmark Suite</a>
|
|
for your perusal.
|
|
</p>
|
|
|
|
<h2>Table of Contents</h2>
|
|
|
|
<p>
|
|
<b>Collections</b>
|
|
<br />
|
|
<span class="methods"><a href="#each">each</a>, <a href="#map">map</a>,
|
|
<a href="#inject">inject</a>, <a href="#detect">detect</a>, <a href="#select">select</a>, <a href="#reject">reject</a>, <a href="#all">all</a>,
|
|
<a href="#any">any</a>, <a href="#include">include</a>, <a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>, <a href="#max">max</a>,
|
|
<a href="#min">min</a>, <a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>, <a href="#toArray">toArray</a>,
|
|
<a href="#size">size</a></span>
|
|
</p>
|
|
|
|
<p>
|
|
<b>Arrays</b>
|
|
<br />
|
|
<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="#intersect">intersect</a>, <a href="#zip">zip</a>, <a href="#indexOf">indexOf</a></span>
|
|
</p>
|
|
|
|
<p>
|
|
<b>Objects</b>
|
|
<br />
|
|
<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="#isEqual">isEqual</a>, <a href="#isElement">isElement</a>,
|
|
<a href="#isArray">isArray</a>, <a href="#isFunction">isFunction</a>, <a href="#isUndefined">isUndefined</a>, <a href="#toString">toString</a>
|
|
</span>
|
|
</p>
|
|
|
|
<p>
|
|
<b>Functions</b>
|
|
<br />
|
|
<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>
|
|
</p>
|
|
|
|
<p>
|
|
<b>Utility</b>
|
|
<br />
|
|
<span class="methods"><a href="#uniqueId">uniqueId</a>, <a href="#template">template</a></span>
|
|
</p>
|
|
|
|
<div id="documentation">
|
|
<h2>Collections</h2>
|
|
|
|
<p id="each">
|
|
<b>each</b><code>_.each(list, iterator, [context])</code>
|
|
<br />
|
|
Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
|
|
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>
|
|
and <b>value</b> properties will be yielded. Delegates to the native
|
|
<b>forEach</b> method if it exists.
|
|
</p>
|
|
<pre>
|
|
_.each([1, 2, 3], function(num){ alert(num); });
|
|
=> alerts each number in turn...</pre>
|
|
|
|
<p id="map">
|
|
<b>map</b><code>_.map(list, iterator, [context])</code>
|
|
<br />
|
|
Produces a new array of values by mapping each value in <b>list</b>
|
|
through a transformation function (<b>iterator</b>). If the native
|
|
<b>map</b> method exists, it will be used instead.
|
|
</p>
|
|
<pre>
|
|
_.map([1, 2, 3], function(num){ return num * 3 });
|
|
=> [3, 6, 9]</pre>
|
|
|
|
<p id="inject">
|
|
<b>inject</b><code>_.inject(list, memo, iterator, [context])</code>
|
|
<br />
|
|
Also known as <b>reduce</b> and <b>foldl</b>, <b>inject</b> reduces a
|
|
<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
|
|
<b>iterator</b>.
|
|
</p>
|
|
<pre>
|
|
var sum = _.inject([1, 2, 3], 0, function(memo, num){ return memo + num });
|
|
=> 6
|
|
</pre>
|
|
|
|
<p id="detect">
|
|
<b>detect</b><code>_.detect(list, iterator, [context])</code>
|
|
<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
|
|
soon as it finds the true element, and doesn't continue to traverse
|
|
the list.
|
|
</p>
|
|
<pre>
|
|
var even = _.detect([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
|
=> 2
|
|
</pre>
|
|
|
|
<p id="select">
|
|
<b>select</b><code>_.select(list, iterator, [context])</code>
|
|
<br />
|
|
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
|
|
native <b>filter</b> method, if it exists.
|
|
</p>
|
|
<pre>
|
|
var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
|
=> [2, 4, 6]
|
|
</pre>
|
|
|
|
<p id="reject">
|
|
<b>reject</b><code>_.reject(list, iterator, [context])</code>
|
|
<br />
|
|
Returns the values in <b>list</b> without the elements that the truth
|
|
test (<b>iterator</b>) passes. The opposite of <b>select</b>.
|
|
</p>
|
|
<pre>
|
|
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
|
=> [1, 3, 5]
|
|
</pre>
|
|
|
|
<p id="all">
|
|
<b>all</b><code>_.all(list, [iterator], [context])</code>
|
|
<br />
|
|
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
|
|
the element will be used instead. Delegates to the native method <b>every</b>, if
|
|
present.
|
|
</p>
|
|
<pre>
|
|
_.all([true, 1, null, 'yes']);
|
|
=> false
|
|
</pre>
|
|
|
|
<p id="any">
|
|
<b>any</b><code>_.any(list, iterator, [context])</code>
|
|
<br />
|
|
Returns <i>true</i> if any of the values in the <b>list</b> pass the
|
|
<b>iterator</b> truth test. Short-circuits and stops traversing the list
|
|
if a true element is found. Delegates to the native method <b>some</b>,
|
|
if present.
|
|
</p>
|
|
<pre>
|
|
_.any([null, 0, 'yes', false]);
|
|
=> true
|
|
</pre>
|
|
|
|
<p id="include">
|
|
<b>include</b><code>_.include(list, value)</code>
|
|
<br />
|
|
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>
|
|
is an Array.
|
|
</p>
|
|
<pre>
|
|
_.include([1, 2, 3], 3);
|
|
=> true
|
|
</pre>
|
|
|
|
<p id="invoke">
|
|
<b>invoke</b><code>_.invoke(list, methodName)</code>
|
|
<br />
|
|
Calls the method named by <b>methodName</b> on each value in the <b>list</b>.
|
|
Any extra arguments passed to <b>invoke</b> will be forwarded on to the
|
|
method invocation.
|
|
</p>
|
|
<pre>
|
|
_.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
|
|
=> [[1, 5, 7], [1, 2, 3]]
|
|
</pre>
|
|
|
|
<p id="pluck">
|
|
<b>pluck</b><code>_.pluck(list, propertyName)</code>
|
|
<br />
|
|
An optimized version of what is perhaps the most common use-case for
|
|
<b>map</b>: returning a list of property values.
|
|
</p>
|
|
<pre>
|
|
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
|
|
_.pluck(stooges, 'name');
|
|
=> ["moe", "larry", "curly"]
|
|
</pre>
|
|
|
|
<p id="max">
|
|
<b>max</b><code>_.max(list, [iterator], [context])</code>
|
|
<br />
|
|
Returns the maximum value in <b>list</b>. If <b>iterator</b> is passed,
|
|
it will be used on each value to generate the criterion by which the
|
|
value is ranked.
|
|
</p>
|
|
<pre>
|
|
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
|
|
_.max(stooges, function(stooge){ return stooge.age; });
|
|
=> {name : 'curly', age : 60};
|
|
</pre>
|
|
|
|
<p id="min">
|
|
<b>min</b><code>_.min(list, [iterator], [context])</code>
|
|
<br />
|
|
Returns the minimum value in <b>list</b>. If <b>iterator</b> is passed,
|
|
it will be used on each value to generate the criterion by which the
|
|
value is ranked.
|
|
</p>
|
|
<pre>
|
|
var numbers = [10, 5, 100, 2, 1000];
|
|
_.min(numbers);
|
|
=> 2
|
|
</pre>
|
|
|
|
<p id="sortBy">
|
|
<b>sortBy</b><code>_.sortBy(list, iterator, [context])</code>
|
|
<br />
|
|
Returns a sorted <b>list</b>, ranked by the results of running each
|
|
value through <b>iterator</b>.
|
|
</p>
|
|
<pre>
|
|
_.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
|
|
=> [5, 4, 6, 3, 1, 2]
|
|
</pre>
|
|
|
|
<p id="sortedIndex">
|
|
<b>sortedIndex</b><code>_.sortedIndex(list, value, [iterator])</code>
|
|
<br />
|
|
Uses a binary search to determine the index at which the <b>value</b>
|
|
should 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.
|
|
</p>
|
|
<pre>
|
|
_.sortedIndex([10, 20, 30, 40, 50], 35);
|
|
=> 3
|
|
</pre>
|
|
|
|
<p id="">
|
|
<b>toArray</b><code>_.(list, iterator, [context])</code>
|
|
<br />
|
|
|
|
</p>
|
|
<pre>
|
|
</pre>
|
|
|
|
<p id="">
|
|
<b>size</b><code>_.(list, iterator, [context])</code>
|
|
<br />
|
|
|
|
</p>
|
|
<pre>
|
|
</pre>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
</html>
|