mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
1998 lines
86 KiB
HTML
1998 lines
86 KiB
HTML
<!DOCTYPE html>
|
||
|
||
<html>
|
||
<head>
|
||
<title>underscore.coffee</title>
|
||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
|
||
<link rel="stylesheet" media="all" href="docco.css" />
|
||
</head>
|
||
<body>
|
||
<div id="container">
|
||
<div id="background"></div>
|
||
|
||
<ul class="sections">
|
||
|
||
<li id="title">
|
||
<div class="annotation">
|
||
<h1>underscore.coffee</h1>
|
||
</div>
|
||
</li>
|
||
|
||
|
||
|
||
<li id="section-1">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-1">¶</a>
|
||
</div>
|
||
<p><strong>Underscore.coffee
|
||
(c) 2011 Jeremy Ashkenas, DocumentCloud Inc.</strong>
|
||
Underscore is freely distributable under the terms of the
|
||
<a href="http://en.wikipedia.org/wiki/MIT_License">MIT license</a>.
|
||
Portions of Underscore are inspired by or borrowed from
|
||
<a href="http://prototypejs.org/api">Prototype.js</a>, Oliver Steele’s
|
||
<a href="http://osteele.com">Functional</a>, and John Resig’s
|
||
<a href="http://ejohn.org">Micro-Templating</a>.
|
||
For all details and documentation:
|
||
<a href="http://documentcloud.github.com/underscore/">http://documentcloud.github.com/underscore/</a></p>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-2">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-2">¶</a>
|
||
</div>
|
||
<h2 id="baseline-setup">Baseline setup</h2>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-3">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-3">¶</a>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-4">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-4">¶</a>
|
||
</div>
|
||
<p>Establish the root object, <code>window</code> in the browser, or <code>global</code> on the server.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>root = <span class="hljs-keyword">this</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-5">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-5">¶</a>
|
||
</div>
|
||
<p>Save the previous value of the <code>_</code> variable.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>previousUnderscore = root._</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-6">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-6">¶</a>
|
||
</div>
|
||
<p>Establish the object that gets thrown to break out of a loop iteration.
|
||
<code>StopIteration</code> is SOP on Mozilla.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>breaker = <span class="hljs-keyword">if</span> <span class="hljs-keyword">typeof</span>(StopIteration) <span class="hljs-keyword">is</span> <span class="hljs-string">'undefined'</span> <span class="hljs-keyword">then</span> <span class="hljs-string">'__break__'</span> <span class="hljs-keyword">else</span> StopIteration</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-7">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-7">¶</a>
|
||
</div>
|
||
<p>Helper function to escape <strong>RegExp</strong> contents, because JS doesn’t have one.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">escapeRegExp</span> = <span class="hljs-params">(string)</span> -></span> string.replace(<span class="hljs-regexp">/([.*+?^${}()|[\]\/\\])/g</span>, <span class="hljs-string">'\\$1'</span>)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-8">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-8">¶</a>
|
||
</div>
|
||
<p>Save bytes in the minified (but not gzipped) version:</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>ArrayProto = Array.prototype
|
||
ObjProto = Object.prototype</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-9">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-9">¶</a>
|
||
</div>
|
||
<p>Create quick reference variables for speed access to core prototypes.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>slice = ArrayProto.slice
|
||
unshift = ArrayProto.unshift
|
||
toString = ObjProto.toString
|
||
hasOwnProperty = ObjProto.hasOwnProperty
|
||
propertyIsEnumerable = ObjProto.propertyIsEnumerable</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-10">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-10">¶</a>
|
||
</div>
|
||
<p>All <strong>ECMA5</strong> native implementations we hope to use are declared here.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>nativeForEach = ArrayProto.forEach
|
||
nativeMap = ArrayProto.map
|
||
nativeReduce = ArrayProto.reduce
|
||
nativeReduceRight = ArrayProto.reduceRight
|
||
nativeFilter = ArrayProto.filter
|
||
nativeEvery = ArrayProto.every
|
||
nativeSome = ArrayProto.some
|
||
nativeIndexOf = ArrayProto.indexOf
|
||
nativeLastIndexOf = ArrayProto.lastIndexOf
|
||
nativeIsArray = Array.isArray
|
||
nativeKeys = Object.keys</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-11">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-11">¶</a>
|
||
</div>
|
||
<p>Create a safe reference to the Underscore object for use below.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">_</span> = <span class="hljs-params">(obj)</span> -></span> <span class="hljs-keyword">new</span> wrapper(obj)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-12">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-12">¶</a>
|
||
</div>
|
||
<p>Export the Underscore object for <strong>CommonJS</strong>.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">if</span> <span class="hljs-keyword">typeof</span>(<span class="hljs-built_in">exports</span>) != <span class="hljs-string">'undefined'</span> <span class="hljs-keyword">then</span> <span class="hljs-built_in">exports</span>._ = _</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-13">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-13">¶</a>
|
||
</div>
|
||
<p>Export Underscore to global scope.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>root._ = _</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-14">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-14">¶</a>
|
||
</div>
|
||
<p>Current version.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.VERSION = <span class="hljs-string">'1.1.0'</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-15">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-15">¶</a>
|
||
</div>
|
||
<h2 id="collection-functions">Collection Functions</h2>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-16">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-16">¶</a>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-17">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-17">¶</a>
|
||
</div>
|
||
<p>The cornerstone, an <strong>each</strong> implementation.
|
||
Handles objects implementing <strong>forEach</strong>, arrays, and raw objects.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">each</span> = <span class="hljs-params">(obj, iterator, context)</span> -></span>
|
||
<span class="hljs-keyword">try</span>
|
||
<span class="hljs-keyword">if</span> nativeForEach <span class="hljs-keyword">and</span> obj.forEach <span class="hljs-keyword">is</span> nativeForEach
|
||
obj.forEach iterator, context
|
||
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> _.isNumber obj.length
|
||
iterator.call context, obj[i], i, obj <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span>..obj.length]
|
||
<span class="hljs-keyword">else</span>
|
||
iterator.call context, val, key, obj <span class="hljs-keyword">for</span> own key, val <span class="hljs-keyword">of</span> obj
|
||
<span class="hljs-keyword">catch</span> e
|
||
<span class="hljs-keyword">throw</span> e <span class="hljs-keyword">if</span> e <span class="hljs-keyword">isnt</span> breaker
|
||
obj</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-18">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-18">¶</a>
|
||
</div>
|
||
<p>Return the results of applying the iterator to each element. Use JavaScript
|
||
1.6’s version of <strong>map</strong>, if possible.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">map</span> = <span class="hljs-params">(obj, iterator, context)</span> -></span>
|
||
<span class="hljs-keyword">return</span> obj.map(iterator, context) <span class="hljs-keyword">if</span> nativeMap <span class="hljs-keyword">and</span> obj.map <span class="hljs-keyword">is</span> nativeMap
|
||
results = []
|
||
_.each obj, <span class="hljs-function"><span class="hljs-params">(value, index, list)</span> -></span>
|
||
results.push iterator.call context, value, index, list
|
||
results</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-19">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-19">¶</a>
|
||
</div>
|
||
<p><strong>Reduce</strong> builds up a single result from a list of values. Also known as
|
||
<strong>inject</strong>, or <strong>foldl</strong>. Uses JavaScript 1.8’s version of <strong>reduce</strong>, if possible.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">reduce</span> = <span class="hljs-params">(obj, iterator, memo, context)</span> -></span>
|
||
<span class="hljs-keyword">if</span> nativeReduce <span class="hljs-keyword">and</span> obj.reduce <span class="hljs-keyword">is</span> nativeReduce
|
||
iterator = _.bind iterator, context <span class="hljs-keyword">if</span> context
|
||
<span class="hljs-keyword">return</span> obj.reduce iterator, memo
|
||
_.each obj, <span class="hljs-function"><span class="hljs-params">(value, index, list)</span> -></span>
|
||
memo = iterator.call context, memo, value, index, list
|
||
memo</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-20">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-20">¶</a>
|
||
</div>
|
||
<p>The right-associative version of <strong>reduce</strong>, also known as <strong>foldr</strong>. Uses
|
||
JavaScript 1.8’s version of <strong>reduceRight</strong>, if available.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">reduceRight</span> = <span class="hljs-params">(obj, iterator, memo, context)</span> -></span>
|
||
<span class="hljs-keyword">if</span> nativeReduceRight <span class="hljs-keyword">and</span> obj.reduceRight <span class="hljs-keyword">is</span> nativeReduceRight
|
||
iterator = _.bind iterator, context <span class="hljs-keyword">if</span> context
|
||
<span class="hljs-keyword">return</span> obj.reduceRight iterator, memo
|
||
reversed = _.clone(_.toArray(obj)).reverse()
|
||
_.reduce reversed, iterator, memo, context</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-21">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-21">¶</a>
|
||
</div>
|
||
<p>Return the first value which passes a truth test.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">detect</span> = <span class="hljs-params">(obj, iterator, context)</span> -></span>
|
||
result = <span class="hljs-literal">null</span>
|
||
_.each obj, <span class="hljs-function"><span class="hljs-params">(value, index, list)</span> -></span>
|
||
<span class="hljs-keyword">if</span> iterator.call context, value, index, list
|
||
result = value
|
||
_.breakLoop()
|
||
result</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-22">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-22">¶</a>
|
||
</div>
|
||
<p>Return all the elements that pass a truth test. Use JavaScript 1.6’s
|
||
<strong>filter</strong>, if it exists.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">filter</span> = <span class="hljs-params">(obj, iterator, context)</span> -></span>
|
||
<span class="hljs-keyword">return</span> obj.filter iterator, context <span class="hljs-keyword">if</span> nativeFilter <span class="hljs-keyword">and</span> obj.filter <span class="hljs-keyword">is</span> nativeFilter
|
||
results = []
|
||
_.each obj, <span class="hljs-function"><span class="hljs-params">(value, index, list)</span> -></span>
|
||
results.push value <span class="hljs-keyword">if</span> iterator.call context, value, index, list
|
||
results</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-23">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-23">¶</a>
|
||
</div>
|
||
<p>Return all the elements for which a truth test fails.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">reject</span> = <span class="hljs-params">(obj, iterator, context)</span> -></span>
|
||
results = []
|
||
_.each obj, <span class="hljs-function"><span class="hljs-params">(value, index, list)</span> -></span>
|
||
results.push value <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> iterator.call context, value, index, list
|
||
results</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-24">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-24">¶</a>
|
||
</div>
|
||
<p>Determine whether all of the elements match a truth test. Delegate to
|
||
JavaScript 1.6’s <strong>every</strong>, if it is present.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">every</span> = <span class="hljs-params">(obj, iterator, context)</span> -></span>
|
||
iterator ||= _.identity
|
||
<span class="hljs-keyword">return</span> obj.every iterator, context <span class="hljs-keyword">if</span> nativeEvery <span class="hljs-keyword">and</span> obj.every <span class="hljs-keyword">is</span> nativeEvery
|
||
result = <span class="hljs-literal">true</span>
|
||
_.each obj, <span class="hljs-function"><span class="hljs-params">(value, index, list)</span> -></span>
|
||
_.breakLoop() <span class="hljs-keyword">unless</span> (result = result <span class="hljs-keyword">and</span> iterator.call(context, value, index, list))
|
||
result</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-25">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-25">¶</a>
|
||
</div>
|
||
<p>Determine if at least one element in the object matches a truth test. Use
|
||
JavaScript 1.6’s <strong>some</strong>, if it exists.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">some</span> = <span class="hljs-params">(obj, iterator, context)</span> -></span>
|
||
iterator ||= _.identity
|
||
<span class="hljs-keyword">return</span> obj.some iterator, context <span class="hljs-keyword">if</span> nativeSome <span class="hljs-keyword">and</span> obj.some <span class="hljs-keyword">is</span> nativeSome
|
||
result = <span class="hljs-literal">false</span>
|
||
_.each obj, <span class="hljs-function"><span class="hljs-params">(value, index, list)</span> -></span>
|
||
_.breakLoop() <span class="hljs-keyword">if</span> (result = iterator.call(context, value, index, list))
|
||
result</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-26">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-26">¶</a>
|
||
</div>
|
||
<p>Determine if a given value is included in the array or object,
|
||
based on <code>===</code>.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">include</span> = <span class="hljs-params">(obj, target)</span> -></span>
|
||
<span class="hljs-keyword">return</span> _.indexOf(obj, target) <span class="hljs-keyword">isnt</span> -<span class="hljs-number">1</span> <span class="hljs-keyword">if</span> nativeIndexOf <span class="hljs-keyword">and</span> obj.indexOf <span class="hljs-keyword">is</span> nativeIndexOf
|
||
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span> <span class="hljs-keyword">for</span> own key, val <span class="hljs-keyword">of</span> obj <span class="hljs-keyword">when</span> val <span class="hljs-keyword">is</span> target
|
||
<span class="hljs-literal">false</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-27">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-27">¶</a>
|
||
</div>
|
||
<p>Invoke a method with arguments on every item in a collection.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">invoke</span> = <span class="hljs-params">(obj, method)</span> -></span>
|
||
args = _.rest arguments, <span class="hljs-number">2</span>
|
||
(<span class="hljs-keyword">if</span> method <span class="hljs-keyword">then</span> val[method] <span class="hljs-keyword">else</span> val).apply(val, args) <span class="hljs-keyword">for</span> val <span class="hljs-keyword">in</span> obj</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-28">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-28">¶</a>
|
||
</div>
|
||
<p>Convenience version of a common use case of <strong>map</strong>: fetching a property.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">pluck</span> = <span class="hljs-params">(obj, key)</span> -></span>
|
||
_.map<span class="hljs-function"><span class="hljs-params">(obj, (val) -> val[key])</span>
|
||
|
||
|
||
</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-29">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-29">¶</a>
|
||
</div>
|
||
<p>Return the maximum item or (item-based computation).</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">max</span> = <span class="hljs-params">(obj, iterator, context)</span> -></span>
|
||
<span class="hljs-keyword">return</span> Math.max.apply(Math, obj) <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> iterator <span class="hljs-keyword">and</span> _.isArray(obj)
|
||
result = <span class="hljs-attribute">computed</span>: -Infinity
|
||
_.each obj, <span class="hljs-function"><span class="hljs-params">(value, index, list)</span> -></span>
|
||
computed = <span class="hljs-keyword">if</span> iterator <span class="hljs-keyword">then</span> iterator.call(context, value, index, list) <span class="hljs-keyword">else</span> value
|
||
computed >= result.computed <span class="hljs-keyword">and</span> (result = {<span class="hljs-attribute">value</span>: value, <span class="hljs-attribute">computed</span>: computed})
|
||
result.value</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-30">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-30">¶</a>
|
||
</div>
|
||
<p>Return the minimum element (or element-based computation).</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">min</span> = <span class="hljs-params">(obj, iterator, context)</span> -></span>
|
||
<span class="hljs-keyword">return</span> Math.min.apply(Math, obj) <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> iterator <span class="hljs-keyword">and</span> _.isArray(obj)
|
||
result = <span class="hljs-attribute">computed</span>: Infinity
|
||
_.each obj, <span class="hljs-function"><span class="hljs-params">(value, index, list)</span> -></span>
|
||
computed = <span class="hljs-keyword">if</span> iterator <span class="hljs-keyword">then</span> iterator.call(context, value, index, list) <span class="hljs-keyword">else</span> value
|
||
computed < result.computed <span class="hljs-keyword">and</span> (result = {<span class="hljs-attribute">value</span>: value, <span class="hljs-attribute">computed</span>: computed})
|
||
result.value</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-31">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-31">¶</a>
|
||
</div>
|
||
<p>Sort the object’s values by a criterion produced by an iterator.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">sortBy</span> = <span class="hljs-params">(obj, iterator, context)</span> -></span>
|
||
_.pluck<span class="hljs-function"><span class="hljs-params">(((_.map obj, (value, index, list) ->
|
||
{value: value, criteria: iterator.call(context, value, index, list)}
|
||
).sort((left, right) ->
|
||
a = left.criteria; b = right.criteria
|
||
<span class="hljs-keyword">if</span> a < b <span class="hljs-keyword">then</span> -<span class="hljs-number">1</span> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> a > b <span class="hljs-keyword">then</span> <span class="hljs-number">1</span> <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>
|
||
)), <span class="hljs-string">'value'</span>)</span>
|
||
|
||
|
||
</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-32">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-32">¶</a>
|
||
</div>
|
||
<p>Use a comparator function to figure out at what index an object should
|
||
be inserted so as to maintain order. Uses binary search.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">sortedIndex</span> = <span class="hljs-params">(array, obj, iterator)</span> -></span>
|
||
iterator ||= _.identity
|
||
low = <span class="hljs-number">0</span>
|
||
high = array.length
|
||
<span class="hljs-keyword">while</span> low < high
|
||
mid = (low + high) >> <span class="hljs-number">1</span>
|
||
<span class="hljs-keyword">if</span> iterator(array[mid]) < iterator(obj) <span class="hljs-keyword">then</span> low = mid + <span class="hljs-number">1</span> <span class="hljs-keyword">else</span> high = mid
|
||
low</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-33">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-33">¶</a>
|
||
</div>
|
||
<p>Convert anything iterable into a real, live array.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">toArray</span> = <span class="hljs-params">(iterable)</span> -></span>
|
||
<span class="hljs-keyword">return</span> [] <span class="hljs-keyword">if</span> (!iterable)
|
||
<span class="hljs-keyword">return</span> iterable.toArray() <span class="hljs-keyword">if</span> (iterable.toArray)
|
||
<span class="hljs-keyword">return</span> iterable <span class="hljs-keyword">if</span> (_.isArray(iterable))
|
||
<span class="hljs-keyword">return</span> slice.call(iterable) <span class="hljs-keyword">if</span> (_.isArguments(iterable))
|
||
_.values(iterable)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-34">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-34">¶</a>
|
||
</div>
|
||
<p>Return the number of elements in an object.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">size</span> = <span class="hljs-params">(obj)</span> -></span> _.toArray(obj).length</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-35">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-35">¶</a>
|
||
</div>
|
||
<h2 id="array-functions">Array Functions</h2>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-36">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-36">¶</a>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-37">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-37">¶</a>
|
||
</div>
|
||
<p>Get the first element of an array. Passing <code>n</code> will return the first N
|
||
values in the array. Aliased as <strong>head</strong>. The <code>guard</code> check allows it to work
|
||
with <strong>map</strong>.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">first</span> = <span class="hljs-params">(array, n, guard)</span> -></span>
|
||
<span class="hljs-keyword">if</span> n <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> guard <span class="hljs-keyword">then</span> slice.call(array, <span class="hljs-number">0</span>, n) <span class="hljs-keyword">else</span> array[<span class="hljs-number">0</span>]</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-38">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-38">¶</a>
|
||
</div>
|
||
<p>Returns everything but the first entry of the array. Aliased as <strong>tail</strong>.
|
||
Especially useful on the arguments object. Passing an <code>index</code> will return
|
||
the rest of the values in the array from that index onward. The <code>guard</code>
|
||
check allows it to work with <strong>map</strong>.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">rest</span> = <span class="hljs-params">(array, index, guard)</span> -></span>
|
||
slice.call(array, <span class="hljs-keyword">if</span> _.isUndefined(index) <span class="hljs-keyword">or</span> guard <span class="hljs-keyword">then</span> <span class="hljs-number">1</span> <span class="hljs-keyword">else</span> index)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-39">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-39">¶</a>
|
||
</div>
|
||
<p>Get the last element of an array.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">last</span> = <span class="hljs-params">(array)</span> -></span> array[array.length - <span class="hljs-number">1</span>]</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-40">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-40">¶</a>
|
||
</div>
|
||
<p>Trim out all falsy values from an array.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">compact</span> = <span class="hljs-params">(array)</span> -></span> item <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> array <span class="hljs-keyword">when</span> item</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-41">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-41">¶</a>
|
||
</div>
|
||
<p>Return a completely flattened version of an array.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">flatten</span> = <span class="hljs-params">(array)</span> -></span>
|
||
_.reduce array, <span class="hljs-function"><span class="hljs-params">(memo, value)</span> -></span>
|
||
<span class="hljs-keyword">return</span> memo.concat(_.flatten(value)) <span class="hljs-keyword">if</span> _.isArray value
|
||
memo.push value
|
||
memo
|
||
, []</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-42">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-42">¶</a>
|
||
</div>
|
||
<p>Return a version of the array that does not contain the specified value(s).</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">without</span> = <span class="hljs-params">(array)</span> -></span>
|
||
values = _.rest arguments
|
||
val <span class="hljs-keyword">for</span> val <span class="hljs-keyword">in</span> _.toArray(array) <span class="hljs-keyword">when</span> <span class="hljs-keyword">not</span> _.include values, val</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-43">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-43">¶</a>
|
||
</div>
|
||
<p>Produce a duplicate-free version of the array. If the array has already
|
||
been sorted, you have the option of using a faster algorithm.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">uniq</span> = <span class="hljs-params">(array, isSorted)</span> -></span>
|
||
memo = []
|
||
<span class="hljs-keyword">for</span> el, i <span class="hljs-keyword">in</span> _.toArray array
|
||
memo.push el <span class="hljs-keyword">if</span> i <span class="hljs-keyword">is</span> <span class="hljs-number">0</span> || (<span class="hljs-keyword">if</span> isSorted <span class="hljs-keyword">is</span> <span class="hljs-literal">true</span> <span class="hljs-keyword">then</span> _.last(memo) <span class="hljs-keyword">isnt</span> el <span class="hljs-keyword">else</span> <span class="hljs-keyword">not</span> _.include(memo, el))
|
||
memo</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-44">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-44">¶</a>
|
||
</div>
|
||
<p>Produce an array that contains every item shared between all the
|
||
passed-in arrays.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">intersect</span> = <span class="hljs-params">(array)</span> -></span>
|
||
rest = _.rest arguments
|
||
_.select _.uniq<span class="hljs-function"><span class="hljs-params">(array)</span>, <span class="hljs-params">(item)</span> -></span>
|
||
_.all rest, <span class="hljs-function"><span class="hljs-params">(other)</span> -></span>
|
||
_.indexOf(other, item) >= <span class="hljs-number">0</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-45">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-45">¶</a>
|
||
</div>
|
||
<p>Zip together multiple lists into a single array — elements that share
|
||
an index go together.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">zip</span> = -></span>
|
||
length = _.max _.pluck arguments, <span class="hljs-string">'length'</span>
|
||
results = <span class="hljs-keyword">new</span> Array length
|
||
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span>..length]
|
||
results[i] = _.pluck arguments, String i
|
||
results</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-46">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-46">¶</a>
|
||
</div>
|
||
<p>If the browser doesn’t supply us with <strong>indexOf</strong> (I’m looking at you, MSIE),
|
||
we need this function. Return the position of the first occurrence of an
|
||
item in an array, or -1 if the item is not included in the array.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">indexOf</span> = <span class="hljs-params">(array, item)</span> -></span>
|
||
<span class="hljs-keyword">return</span> array.indexOf item <span class="hljs-keyword">if</span> nativeIndexOf <span class="hljs-keyword">and</span> array.indexOf <span class="hljs-keyword">is</span> nativeIndexOf
|
||
i = <span class="hljs-number">0</span>; l = array.length
|
||
<span class="hljs-keyword">while</span> l - i
|
||
<span class="hljs-keyword">if</span> array[i] <span class="hljs-keyword">is</span> item <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> i <span class="hljs-keyword">else</span> i++
|
||
-<span class="hljs-number">1</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-47">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-47">¶</a>
|
||
</div>
|
||
<p>Provide JavaScript 1.6’s <strong>lastIndexOf</strong>, delegating to the native function,
|
||
if possible.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">lastIndexOf</span> = <span class="hljs-params">(array, item)</span> -></span>
|
||
<span class="hljs-keyword">return</span> array.lastIndexOf(item) <span class="hljs-keyword">if</span> nativeLastIndexOf <span class="hljs-keyword">and</span> array.lastIndexOf <span class="hljs-keyword">is</span> nativeLastIndexOf
|
||
i = array.length
|
||
<span class="hljs-keyword">while</span> i
|
||
<span class="hljs-keyword">if</span> array[i] <span class="hljs-keyword">is</span> item <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> i <span class="hljs-keyword">else</span> i--
|
||
-<span class="hljs-number">1</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-48">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-48">¶</a>
|
||
</div>
|
||
<p>Generate an integer Array containing an arithmetic progression. A port of
|
||
<a href="http://docs.python.org/library/functions.html#range">the native Python <strong>range</strong> function</a>.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">range</span> = <span class="hljs-params">(start, stop, step)</span> -></span>
|
||
a = arguments
|
||
solo = a.length <= <span class="hljs-number">1</span>
|
||
i = start = <span class="hljs-keyword">if</span> solo <span class="hljs-keyword">then</span> <span class="hljs-number">0</span> <span class="hljs-keyword">else</span> a[<span class="hljs-number">0</span>]
|
||
stop = <span class="hljs-keyword">if</span> solo <span class="hljs-keyword">then</span> a[<span class="hljs-number">0</span>] <span class="hljs-keyword">else</span> a[<span class="hljs-number">1</span>]
|
||
step = a[<span class="hljs-number">2</span>] <span class="hljs-keyword">or</span> <span class="hljs-number">1</span>
|
||
len = Math.ceil((stop - start) / step)
|
||
<span class="hljs-keyword">return</span> [] <span class="hljs-keyword">if</span> len <= <span class="hljs-number">0</span>
|
||
range = <span class="hljs-keyword">new</span> Array len
|
||
idx = <span class="hljs-number">0</span>
|
||
<span class="hljs-keyword">loop</span>
|
||
<span class="hljs-keyword">return</span> range <span class="hljs-keyword">if</span> (<span class="hljs-keyword">if</span> step > <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> i - stop <span class="hljs-keyword">else</span> stop - i) >= <span class="hljs-number">0</span>
|
||
range[idx] = i
|
||
idx++
|
||
i+= step</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-49">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-49">¶</a>
|
||
</div>
|
||
<h2 id="function-functions">Function Functions</h2>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-50">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-50">¶</a>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-51">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-51">¶</a>
|
||
</div>
|
||
<p>Create a function bound to a given object (assigning <code>this</code>, and arguments,
|
||
optionally). Binding with arguments is also known as <strong>curry</strong>.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">bind</span> = <span class="hljs-params">(func, obj)</span> -></span>
|
||
args = _.rest arguments, <span class="hljs-number">2</span><span class="hljs-function">
|
||
-></span> func.apply obj <span class="hljs-keyword">or</span> root, args.concat arguments</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-52">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-52">¶</a>
|
||
</div>
|
||
<p>Bind all of an object’s methods to that object. Useful for ensuring that
|
||
all callbacks defined on an object belong to it.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">bindAll</span> = <span class="hljs-params">(obj)</span> -></span>
|
||
funcs = <span class="hljs-keyword">if</span> arguments.length > <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> _.rest(arguments) <span class="hljs-keyword">else</span> _.functions(obj)
|
||
_.each funcs, <span class="hljs-function"><span class="hljs-params">(f)</span> -></span> obj[f] = _.bind obj[f], obj
|
||
obj</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-53">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-53">¶</a>
|
||
</div>
|
||
<p>Delays a function for the given number of milliseconds, and then calls
|
||
it with the arguments supplied.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">delay</span> = <span class="hljs-params">(func, wait)</span> -></span>
|
||
args = _.rest arguments, <span class="hljs-number">2</span>
|
||
setTimeout((<span class="hljs-function">-></span> func.apply(func, args)), wait)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-54">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-54">¶</a>
|
||
</div>
|
||
<p>Memoize an expensive function by storing its results.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">memoize</span> = <span class="hljs-params">(func, hasher)</span> -></span>
|
||
memo = {}
|
||
hasher <span class="hljs-keyword">or</span>= _.identity<span class="hljs-function">
|
||
-></span>
|
||
key = hasher.apply <span class="hljs-keyword">this</span>, arguments
|
||
<span class="hljs-keyword">return</span> memo[key] <span class="hljs-keyword">if</span> key <span class="hljs-keyword">of</span> memo
|
||
memo[key] = func.apply <span class="hljs-keyword">this</span>, arguments</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-55">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-55">¶</a>
|
||
</div>
|
||
<p>Defers a function, scheduling it to run after the current call stack has
|
||
cleared.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">defer</span> = <span class="hljs-params">(func)</span> -></span>
|
||
_.delay.apply _, [func, <span class="hljs-number">1</span>].concat _.rest arguments</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-56">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-56">¶</a>
|
||
</div>
|
||
<p>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.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">wrap</span> = <span class="hljs-params">(func, wrapper)</span> -></span><span class="hljs-function">
|
||
-></span> wrapper.apply wrapper, [func].concat arguments</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-57">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-57">¶</a>
|
||
</div>
|
||
<p>Returns a function that is the composition of a list of functions, each
|
||
consuming the return value of the function that follows.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">compose</span> = -></span>
|
||
funcs = arguments<span class="hljs-function">
|
||
-></span>
|
||
args = arguments
|
||
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [funcs.length - <span class="hljs-number">1.</span><span class="hljs-number">.0</span>] <span class="hljs-keyword">by</span> -<span class="hljs-number">1</span>
|
||
args = [funcs[i].apply(<span class="hljs-keyword">this</span>, args)]
|
||
args[<span class="hljs-number">0</span>]</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-58">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-58">¶</a>
|
||
</div>
|
||
<h2 id="object-functions">Object Functions</h2>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-59">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-59">¶</a>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-60">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-60">¶</a>
|
||
</div>
|
||
<p>Retrieve the names of an object’s properties.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.keys = nativeKeys <span class="hljs-keyword">or</span> <span class="hljs-function"><span class="hljs-params">(obj)</span> -></span>
|
||
<span class="hljs-keyword">return</span> _.range <span class="hljs-number">0</span>, obj.length <span class="hljs-keyword">if</span> _.isArray(obj)
|
||
key <span class="hljs-keyword">for</span> key, val <span class="hljs-keyword">of</span> obj</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-61">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-61">¶</a>
|
||
</div>
|
||
<p>Retrieve the values of an object’s properties.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">values</span> = <span class="hljs-params">(obj)</span> -></span>
|
||
_.map obj, _.identity</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-62">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-62">¶</a>
|
||
</div>
|
||
<p>Return a sorted list of the function names available in Underscore.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">functions</span> = <span class="hljs-params">(obj)</span> -></span>
|
||
_.filter<span class="hljs-function"><span class="hljs-params">(_.keys(obj), (key) -> _.isFunction(obj[key]))</span>.<span class="hljs-title">sort</span><span class="hljs-params">()</span>
|
||
|
||
|
||
</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-63">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-63">¶</a>
|
||
</div>
|
||
<p>Extend a given object with all of the properties in a source object.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">extend</span> = <span class="hljs-params">(obj)</span> -></span>
|
||
<span class="hljs-keyword">for</span> source <span class="hljs-keyword">in</span> _.rest(arguments)
|
||
obj[key] = val <span class="hljs-keyword">for</span> key, val <span class="hljs-keyword">of</span> source
|
||
obj</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-64">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-64">¶</a>
|
||
</div>
|
||
<p>Create a (shallow-cloned) duplicate of an object.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">clone</span> = <span class="hljs-params">(obj)</span> -></span>
|
||
<span class="hljs-keyword">return</span> obj.slice <span class="hljs-number">0</span> <span class="hljs-keyword">if</span> _.isArray obj
|
||
_.extend {}, obj</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-65">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-65">¶</a>
|
||
</div>
|
||
<p>Invokes interceptor with the obj, and then returns obj.
|
||
The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">tap</span> = <span class="hljs-params">(obj, interceptor)</span> -></span>
|
||
interceptor obj
|
||
obj</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-66">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-66">¶</a>
|
||
</div>
|
||
<p>Perform a deep comparison to check if two objects are equal.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isEqual</span> = <span class="hljs-params">(a, b)</span> -></span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-67">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-67">¶</a>
|
||
</div>
|
||
<p>Check object identity.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span> <span class="hljs-keyword">if</span> a <span class="hljs-keyword">is</span> b</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-68">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-68">¶</a>
|
||
</div>
|
||
<p>Different types?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> atype = <span class="hljs-keyword">typeof</span>(a); btype = <span class="hljs-keyword">typeof</span>(b)
|
||
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">if</span> atype <span class="hljs-keyword">isnt</span> btype</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-69">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-69">¶</a>
|
||
</div>
|
||
<p>Basic equality test (watch out for coercions).</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span> <span class="hljs-keyword">if</span> `<span class="javascript">a == b</span>`</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-70">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-70">¶</a>
|
||
</div>
|
||
<p>One is falsy and the other truthy.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">if</span> (!a <span class="hljs-keyword">and</span> b) <span class="hljs-keyword">or</span> (a <span class="hljs-keyword">and</span> !b)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-71">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-71">¶</a>
|
||
</div>
|
||
<p>One of them implements an <code>isEqual()</code>?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> a.isEqual(b) <span class="hljs-keyword">if</span> a.isEqual</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-72">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-72">¶</a>
|
||
</div>
|
||
<p>Check dates’ integer values.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> a.getTime() <span class="hljs-keyword">is</span> b.getTime() <span class="hljs-keyword">if</span> _.isDate(a) <span class="hljs-keyword">and</span> _.isDate(b)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-73">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-73">¶</a>
|
||
</div>
|
||
<p>Both are NaN?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">if</span> _.isNaN(a) <span class="hljs-keyword">and</span> _.isNaN(b)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-74">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-74">¶</a>
|
||
</div>
|
||
<p>Compare regular expressions.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> _.isRegExp(a) <span class="hljs-keyword">and</span> _.isRegExp(b)
|
||
<span class="hljs-keyword">return</span> a.source <span class="hljs-keyword">is</span> b.source <span class="hljs-keyword">and</span>
|
||
a.<span class="hljs-built_in">global</span> <span class="hljs-keyword">is</span> b.<span class="hljs-built_in">global</span> <span class="hljs-keyword">and</span>
|
||
a.ignoreCase <span class="hljs-keyword">is</span> b.ignoreCase <span class="hljs-keyword">and</span>
|
||
a.multiline <span class="hljs-keyword">is</span> b.multiline</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-75">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-75">¶</a>
|
||
</div>
|
||
<p>If a is not an object by this point, we can’t handle it.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">if</span> atype <span class="hljs-keyword">isnt</span> <span class="hljs-string">'object'</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-76">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-76">¶</a>
|
||
</div>
|
||
<p>Check for different array lengths before comparing contents.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">if</span> a.length <span class="hljs-keyword">and</span> (a.length <span class="hljs-keyword">isnt</span> b.length)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-77">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-77">¶</a>
|
||
</div>
|
||
<p>Nothing else worked, deep compare the contents.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> aKeys = _.keys(a); bKeys = _.keys(b)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-78">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-78">¶</a>
|
||
</div>
|
||
<p>Different object sizes?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">if</span> aKeys.length <span class="hljs-keyword">isnt</span> bKeys.length</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-79">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-79">¶</a>
|
||
</div>
|
||
<p>Recursive comparison of contents.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">for</span> key, val <span class="hljs-keyword">of</span> a <span class="hljs-keyword">when</span> !(key <span class="hljs-keyword">of</span> b) <span class="hljs-keyword">or</span> !_.isEqual(val, b[key])
|
||
<span class="hljs-literal">true</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-80">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-80">¶</a>
|
||
</div>
|
||
<p>Is a given array or object empty?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isEmpty</span> = <span class="hljs-params">(obj)</span> -></span>
|
||
<span class="hljs-keyword">return</span> obj.length <span class="hljs-keyword">is</span> <span class="hljs-number">0</span> <span class="hljs-keyword">if</span> _.isArray(obj) <span class="hljs-keyword">or</span> _.isString(obj)
|
||
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">for</span> own key <span class="hljs-keyword">of</span> obj
|
||
<span class="hljs-literal">true</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-81">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-81">¶</a>
|
||
</div>
|
||
<p>Is a given value a DOM element?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isElement</span> = <span class="hljs-params">(obj)</span> -></span> obj <span class="hljs-keyword">and</span> obj.nodeType <span class="hljs-keyword">is</span> <span class="hljs-number">1</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-82">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-82">¶</a>
|
||
</div>
|
||
<p>Is a given value an array?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.isArray = nativeIsArray <span class="hljs-keyword">or</span> <span class="hljs-function"><span class="hljs-params">(obj)</span> -></span> !!(obj <span class="hljs-keyword">and</span> obj.concat <span class="hljs-keyword">and</span> obj.unshift <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> obj.callee)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-83">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-83">¶</a>
|
||
</div>
|
||
<p>Is a given variable an arguments object?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isArguments</span> = <span class="hljs-params">(obj)</span> -></span> obj <span class="hljs-keyword">and</span> obj.callee</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-84">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-84">¶</a>
|
||
</div>
|
||
<p>Is the given value a function?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isFunction</span> = <span class="hljs-params">(obj)</span> -></span> !!(obj <span class="hljs-keyword">and</span> obj.constructor <span class="hljs-keyword">and</span> obj.call <span class="hljs-keyword">and</span> obj.apply)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-85">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-85">¶</a>
|
||
</div>
|
||
<p>Is the given value a string?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isString</span> = <span class="hljs-params">(obj)</span> -></span> !!(obj <span class="hljs-keyword">is</span> <span class="hljs-string">''</span> <span class="hljs-keyword">or</span> (obj <span class="hljs-keyword">and</span> obj.charCodeAt <span class="hljs-keyword">and</span> obj.substr))</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-86">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-86">¶</a>
|
||
</div>
|
||
<p>Is a given value a number?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isNumber</span> = <span class="hljs-params">(obj)</span> -></span> (obj <span class="hljs-keyword">is</span> +obj) <span class="hljs-keyword">or</span> toString.call(obj) <span class="hljs-keyword">is</span> <span class="hljs-string">'[object Number]'</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-87">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-87">¶</a>
|
||
</div>
|
||
<p>Is a given value a boolean?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isBoolean</span> = <span class="hljs-params">(obj)</span> -></span> obj <span class="hljs-keyword">is</span> <span class="hljs-literal">true</span> <span class="hljs-keyword">or</span> obj <span class="hljs-keyword">is</span> <span class="hljs-literal">false</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-88">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-88">¶</a>
|
||
</div>
|
||
<p>Is a given value a Date?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isDate</span> = <span class="hljs-params">(obj)</span> -></span> !!(obj <span class="hljs-keyword">and</span> obj.getTimezoneOffset <span class="hljs-keyword">and</span> obj.setUTCFullYear)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-89">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-89">¶</a>
|
||
</div>
|
||
<p>Is the given value a regular expression?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isRegExp</span> = <span class="hljs-params">(obj)</span> -></span> !!(obj <span class="hljs-keyword">and</span> obj.exec <span class="hljs-keyword">and</span> (obj.ignoreCase <span class="hljs-keyword">or</span> obj.ignoreCase <span class="hljs-keyword">is</span> <span class="hljs-literal">false</span>))</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-90">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-90">¶</a>
|
||
</div>
|
||
<p>Is the given value NaN — this one is interesting. <code>NaN != NaN</code>, and
|
||
<code>isNaN(undefined) == true</code>, so we make sure it’s a number first.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isNaN</span> = <span class="hljs-params">(obj)</span> -></span> _.isNumber(obj) <span class="hljs-keyword">and</span> <span class="hljs-built_in">window</span>.isNaN(obj)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-91">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-91">¶</a>
|
||
</div>
|
||
<p>Is a given value equal to null?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isNull</span> = <span class="hljs-params">(obj)</span> -></span> obj <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-92">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-92">¶</a>
|
||
</div>
|
||
<p>Is a given variable undefined?</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">isUndefined</span> = <span class="hljs-params">(obj)</span> -></span> <span class="hljs-keyword">typeof</span> obj <span class="hljs-keyword">is</span> <span class="hljs-string">'undefined'</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-93">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-93">¶</a>
|
||
</div>
|
||
<h2 id="utility-functions">Utility Functions</h2>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-94">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-94">¶</a>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-95">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-95">¶</a>
|
||
</div>
|
||
<p>Run Underscore.js in noConflict mode, returning the <code>_</code> variable to its
|
||
previous owner. Returns a reference to the Underscore object.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">noConflict</span> = -></span>
|
||
root._ = previousUnderscore
|
||
<span class="hljs-keyword">this</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-96">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-96">¶</a>
|
||
</div>
|
||
<p>Keep the identity function around for default iterators.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">identity</span> = <span class="hljs-params">(value)</span> -></span> value</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-97">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-97">¶</a>
|
||
</div>
|
||
<p>Run a function <code>n</code> times.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">times</span> = <span class="hljs-params">(n, iterator, context)</span> -></span>
|
||
iterator.call context, i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span>..n]</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-98">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-98">¶</a>
|
||
</div>
|
||
<p>Break out of the middle of an iteration.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">breakLoop</span> = -></span> <span class="hljs-keyword">throw</span> breaker</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-99">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-99">¶</a>
|
||
</div>
|
||
<p>Add your own custom functions to the Underscore object, ensuring that
|
||
they’re correctly added to the OOP wrapper as well.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">mixin</span> = <span class="hljs-params">(obj)</span> -></span>
|
||
<span class="hljs-keyword">for</span> name <span class="hljs-keyword">in</span> _.functions(obj)
|
||
addToWrapper name, _[name] = obj[name]</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-100">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-100">¶</a>
|
||
</div>
|
||
<p>Generate a unique integer id (unique within the entire client session).
|
||
Useful for temporary DOM ids.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>idCounter = <span class="hljs-number">0</span>
|
||
_.<span class="hljs-function"><span class="hljs-title">uniqueId</span> = <span class="hljs-params">(prefix)</span> -></span>
|
||
(prefix <span class="hljs-keyword">or</span> <span class="hljs-string">''</span>) + idCounter++</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-101">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-101">¶</a>
|
||
</div>
|
||
<p>By default, Underscore uses <strong>ERB</strong>-style template delimiters, change the
|
||
following template settings to use alternative delimiters.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.templateSettings = {
|
||
<span class="hljs-attribute">start</span>: <span class="hljs-string">'<%'</span>
|
||
<span class="hljs-attribute">end</span>: <span class="hljs-string">'%>'</span>
|
||
<span class="hljs-attribute">interpolate</span>: <span class="hljs-regexp">/<%=(.+?)%>/g</span>
|
||
}</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-102">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-102">¶</a>
|
||
</div>
|
||
<p>JavaScript templating a-la <strong>ERB</strong>, pilfered from John Resig’s
|
||
<em>Secrets of the JavaScript Ninja</em>, page 83.
|
||
Single-quote fix from Rick Strahl.
|
||
With alterations for arbitrary delimiters, and to preserve whitespace.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.<span class="hljs-function"><span class="hljs-title">template</span> = <span class="hljs-params">(str, data)</span> -></span>
|
||
c = _.templateSettings
|
||
endMatch = <span class="hljs-keyword">new</span> RegExp(<span class="hljs-string">"'(?=[^"</span>+c.end.substr(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>)+<span class="hljs-string">"]*"</span>+escapeRegExp(c.end)+<span class="hljs-string">")"</span>,<span class="hljs-string">"g"</span>)
|
||
fn = <span class="hljs-keyword">new</span> Function <span class="hljs-string">'obj'</span>,
|
||
<span class="hljs-string">'var p=[],print=function(){p.push.apply(p,arguments);};'</span> +
|
||
<span class="hljs-string">'with(obj||{}){p.push(\''</span> +
|
||
str.replace(<span class="hljs-regexp">/\r/g</span>, <span class="hljs-string">'\\r'</span>)
|
||
.replace(<span class="hljs-regexp">/\n/g</span>, <span class="hljs-string">'\\n'</span>)
|
||
.replace(<span class="hljs-regexp">/\t/g</span>, <span class="hljs-string">'\\t'</span>)
|
||
.replace(endMatch,<span class="hljs-string">"✄"</span>)
|
||
.split(<span class="hljs-string">"'"</span>).join(<span class="hljs-string">"\\'"</span>)
|
||
.split(<span class="hljs-string">"✄"</span>).join(<span class="hljs-string">"'"</span>)
|
||
.replace(c.interpolate, <span class="hljs-string">"',$1,'"</span>)
|
||
.split(c.start).join(<span class="hljs-string">"');"</span>)
|
||
.split(c.end).join(<span class="hljs-string">"p.push('"</span>) +
|
||
<span class="hljs-string">"');}return p.join('');"</span>
|
||
<span class="hljs-keyword">if</span> data <span class="hljs-keyword">then</span> fn(data) <span class="hljs-keyword">else</span> fn</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-103">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-103">¶</a>
|
||
</div>
|
||
<h2 id="aliases">Aliases</h2>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-104">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-104">¶</a>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>
|
||
_.forEach = _.each
|
||
_.foldl = _.inject = _.reduce
|
||
_.foldr = _.reduceRight
|
||
_.select = _.filter
|
||
_.all = _.every
|
||
_.any = _.some
|
||
_.contains = _.include
|
||
_.head = _.first
|
||
_.tail = _.rest
|
||
_.methods = _.functions</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-105">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-105">¶</a>
|
||
</div>
|
||
<h2 id="setup-the-oop-wrapper">Setup the OOP Wrapper</h2>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-106">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-106">¶</a>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-107">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-107">¶</a>
|
||
</div>
|
||
<p>If Underscore is called as a function, it returns a wrapped object that
|
||
can be used OO-style. This wrapper holds altered versions of all the
|
||
underscore functions. Wrapped objects may be chained.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">wrapper</span> = <span class="hljs-params">(obj)</span> -></span>
|
||
<span class="hljs-keyword">this</span>._wrapped = obj
|
||
<span class="hljs-keyword">this</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-108">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-108">¶</a>
|
||
</div>
|
||
<p>Helper function to continue chaining intermediate results.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">result</span> = <span class="hljs-params">(obj, chain)</span> -></span>
|
||
<span class="hljs-keyword">if</span> chain <span class="hljs-keyword">then</span> _(obj).chain() <span class="hljs-keyword">else</span> obj</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-109">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-109">¶</a>
|
||
</div>
|
||
<p>A method to easily add functions to the OOP wrapper.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">addToWrapper</span> = <span class="hljs-params">(name, func)</span> -></span>
|
||
wrapper.prototype[name] =<span class="hljs-function"> -></span>
|
||
args = _.toArray arguments
|
||
unshift.call args, <span class="hljs-keyword">this</span>._wrapped
|
||
result func.apply(_, args), <span class="hljs-keyword">this</span>._chain</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-110">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-110">¶</a>
|
||
</div>
|
||
<p>Add all ofthe Underscore functions to the wrapper object.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.mixin _</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-111">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-111">¶</a>
|
||
</div>
|
||
<p>Add all mutator Array functions to the wrapper.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.each [<span class="hljs-string">'pop'</span>, <span class="hljs-string">'push'</span>, <span class="hljs-string">'reverse'</span>, <span class="hljs-string">'shift'</span>, <span class="hljs-string">'sort'</span>, <span class="hljs-string">'splice'</span>, <span class="hljs-string">'unshift'</span>], <span class="hljs-function"><span class="hljs-params">(name)</span> -></span>
|
||
method = Array.prototype[name]
|
||
wrapper.prototype[name] =<span class="hljs-function"> -></span>
|
||
method.apply(<span class="hljs-keyword">this</span>._wrapped, arguments)
|
||
result(<span class="hljs-keyword">this</span>._wrapped, <span class="hljs-keyword">this</span>._chain)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-112">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-112">¶</a>
|
||
</div>
|
||
<p>Add all accessor Array functions to the wrapper.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre>_.each [<span class="hljs-string">'concat'</span>, <span class="hljs-string">'join'</span>, <span class="hljs-string">'slice'</span>], <span class="hljs-function"><span class="hljs-params">(name)</span> -></span>
|
||
method = Array.prototype[name]
|
||
wrapper.prototype[name] =<span class="hljs-function"> -></span>
|
||
result(method.apply(<span class="hljs-keyword">this</span>._wrapped, arguments), <span class="hljs-keyword">this</span>._chain)</pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-113">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-113">¶</a>
|
||
</div>
|
||
<p>Start chaining a wrapped Underscore object.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">wrapper</span>::<span class="hljs-function"><span class="hljs-title">chain</span> = -></span>
|
||
<span class="hljs-keyword">this</span>._chain = <span class="hljs-literal">true</span>
|
||
<span class="hljs-keyword">this</span></pre></div></div>
|
||
|
||
</li>
|
||
|
||
|
||
<li id="section-114">
|
||
<div class="annotation">
|
||
|
||
<div class="pilwrap ">
|
||
<a class="pilcrow" href="#section-114">¶</a>
|
||
</div>
|
||
<p>Extracts the result from a wrapped and chained object.</p>
|
||
|
||
</div>
|
||
|
||
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">wrapper</span>::<span class="hljs-function"><span class="hljs-title">value</span> = -></span> <span class="hljs-keyword">this</span>._wrapped</pre></div></div>
|
||
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
</body>
|
||
</html>
|