mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
first batch of documentation
This commit is contained in:
308
index.html
Normal file
308
index.html
Normal file
@@ -0,0 +1,308 @@
|
|||||||
|
<!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 the 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> and <b>some</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>_.(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>_.(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="">
|
||||||
|
<b>include</b><code>_.(list, iterator, [context])</code>
|
||||||
|
<br />
|
||||||
|
|
||||||
|
</p>
|
||||||
|
<pre>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p id="">
|
||||||
|
<b>invoke</b><code>_.(list, iterator, [context])</code>
|
||||||
|
<br />
|
||||||
|
|
||||||
|
</p>
|
||||||
|
<pre>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p id="">
|
||||||
|
<b>pluck</b><code>_.(list, iterator, [context])</code>
|
||||||
|
<br />
|
||||||
|
|
||||||
|
</p>
|
||||||
|
<pre>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p id="">
|
||||||
|
<b>max</b><code>_.(list, iterator, [context])</code>
|
||||||
|
<br />
|
||||||
|
|
||||||
|
</p>
|
||||||
|
<pre>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p id="">
|
||||||
|
<b>min</b><code>_.(list, iterator, [context])</code>
|
||||||
|
<br />
|
||||||
|
|
||||||
|
</p>
|
||||||
|
<pre>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p id="">
|
||||||
|
<b>sortBy</b><code>_.(list, iterator, [context])</code>
|
||||||
|
<br />
|
||||||
|
|
||||||
|
</p>
|
||||||
|
<pre>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p id="">
|
||||||
|
<b>sortedIndex</b><code>_.(list, iterator, [context])</code>
|
||||||
|
<br />
|
||||||
|
|
||||||
|
</p>
|
||||||
|
<pre>
|
||||||
|
</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>
|
||||||
@@ -29,4 +29,23 @@ $(document).ready(function() {
|
|||||||
equals(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object');
|
equals(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
asyncTest("functions: delay", function() {
|
||||||
|
var delayed = false;
|
||||||
|
_.delay(function(){ delayed = true; }, 100);
|
||||||
|
_.delay(function(){ ok(!delayed, "didn't delay the function quite yet"); }, 50);
|
||||||
|
_.delay(function(){ ok(delayed, 'delayed the function'); start(); }, 150);
|
||||||
|
});
|
||||||
|
|
||||||
|
asyncTest("functions: defer", function() {
|
||||||
|
var deferred = false;
|
||||||
|
_.defer(function(bool){ deferred = bool; }, true);
|
||||||
|
_.delay(function(){ ok(deferred, "deferred the function"); start(); }, 50);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("functions: wrap", function() {
|
||||||
|
var greet = function(name){ return "hi: " + name; };
|
||||||
|
var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); });
|
||||||
|
equals(backwards('moe'), 'hi: moe eom', 'wrapped the saluation function');
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,8 +9,8 @@
|
|||||||
<script type="text/javascript" src="../underscore.js"></script>
|
<script type="text/javascript" src="../underscore.js"></script>
|
||||||
<script type="text/javascript" src="collections.js"></script>
|
<script type="text/javascript" src="collections.js"></script>
|
||||||
<script type="text/javascript" src="arrays.js"></script>
|
<script type="text/javascript" src="arrays.js"></script>
|
||||||
<script type="text/javascript" src="functions.js"></script>
|
|
||||||
<script type="text/javascript" src="objects.js"></script>
|
<script type="text/javascript" src="objects.js"></script>
|
||||||
|
<script type="text/javascript" src="functions.js"></script>
|
||||||
<script type="text/javascript" src="utility.js"></script>
|
<script type="text/javascript" src="utility.js"></script>
|
||||||
<script type="text/javascript" src="speed.js"></script>
|
<script type="text/javascript" src="speed.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
@@ -283,6 +283,29 @@ window._ = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Delays a function for the given number of milliseconds, and then calls
|
||||||
|
// it with the arguments supplied.
|
||||||
|
delay : function(func, wait) {
|
||||||
|
var args = _.toArray(arguments).slice(2);
|
||||||
|
return window.setTimeout(function(){ return func.apply(func, args); }, wait);
|
||||||
|
},
|
||||||
|
|
||||||
|
// Defers a function, scheduling it to run after the current call stack has
|
||||||
|
// cleared.
|
||||||
|
defer : function(func) {
|
||||||
|
return _.delay.apply(_, [func, 1].concat(_.toArray(arguments).slice(1)));
|
||||||
|
},
|
||||||
|
|
||||||
|
// Returns the first function passed as an argument to the second,
|
||||||
|
// allowing you to adjust arguments, run code before and after, and
|
||||||
|
// conditionally execute the original function.
|
||||||
|
wrap : function(func, wrapper) {
|
||||||
|
return function() {
|
||||||
|
var args = [func].concat(_.toArray(arguments));
|
||||||
|
return wrapper.apply(wrapper, args);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
/* ---------------- The following methods apply to objects ---------------- */
|
/* ---------------- The following methods apply to objects ---------------- */
|
||||||
|
|
||||||
// Retrieve the names of an object's properties.
|
// Retrieve the names of an object's properties.
|
||||||
|
|||||||
Reference in New Issue
Block a user