mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
282 lines
No EOL
123 KiB
HTML
282 lines
No EOL
123 KiB
HTML
<!DOCTYPE html> <html> <head> <title>underscore.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> underscore.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-1">#</a> </div> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-2">#</a> </div> <p><strong>Underscore.coffee
|
|
(c) 2010 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.com">Micro-Templating</a>.
|
|
For all details and documentation:
|
|
http://documentcloud.github.com/underscore/</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-3">#</a> </div> <h2>Baseline setup</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-4">#</a> </div> <p>Establish the root object, <code>window</code> in the browser, or <code>global</code> on the server.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">root: </span><span class="k">this</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-5">#</a> </div> <p>Save the previous value of the <code>_</code> variable.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">previousUnderscore: </span><span class="nx">root</span><span class="p">.</span><span class="nx">_</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" 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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">breaker: </span><span class="k">if</span> <span class="k">typeof</span><span class="p">(</span><span class="nx">StopIteration</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'undefined'</span> <span class="k">then</span> <span class="s1">'__break__'</span> <span class="k">else</span> <span class="nx">StopIteration</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-7">#</a> </div> <p>Helper function to escape <strong>RegExp</strong> contents, because JS doesn't have one.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">escapeRegExp: </span><span class="p">(</span><span class="nx">string</span><span class="p">)</span> <span class="o">-></span> <span class="nx">string</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/([.*+?^${}()|[\]\/\\])/g</span><span class="p">,</span> <span class="s1">'\\$1'</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-8">#</a> </div> <p>Save bytes in the minified (but not gzipped) version:</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">ArrayProto: </span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span>
|
|
<span class="nv">ObjProto: </span> <span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-9">#</a> </div> <p>Create quick reference variables for speed access to core prototypes.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">slice: </span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">slice</span>
|
|
<span class="nv">unshift: </span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">unshift</span>
|
|
<span class="nv">toString: </span> <span class="nx">ObjProto</span><span class="p">.</span><span class="nx">toString</span>
|
|
<span class="nv">hasOwnProperty: </span> <span class="nx">ObjProto</span><span class="p">.</span><span class="nx">hasOwnProperty</span>
|
|
<span class="nv">propertyIsEnumerable: </span><span class="nx">ObjProto</span><span class="p">.</span><span class="nx">propertyIsEnumerable</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-10">#</a> </div> <p>All <strong>ECMA5</strong> native implementations we hope to use are declared here.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">nativeForEach: </span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">forEach</span>
|
|
<span class="nv">nativeMap: </span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">map</span>
|
|
<span class="nv">nativeReduce: </span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">reduce</span>
|
|
<span class="nv">nativeReduceRight: </span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">reduceRight</span>
|
|
<span class="nv">nativeFilter: </span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">filter</span>
|
|
<span class="nv">nativeEvery: </span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">every</span>
|
|
<span class="nv">nativeSome: </span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">some</span>
|
|
<span class="nv">nativeIndexOf: </span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">indexOf</span>
|
|
<span class="nv">nativeLastIndexOf: </span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">lastIndexOf</span>
|
|
<span class="nv">nativeIsArray: </span> <span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span>
|
|
<span class="nv">nativeKeys: </span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-11">#</a> </div> <p>Create a safe reference to the Underscore object for use below.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_: </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">wrapper</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-12">#</a> </div> <p>Export the Underscore object for <strong>CommonJS</strong>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="k">typeof</span><span class="p">(</span><span class="nx">exports</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">'undefined'</span> <span class="k">then</span> <span class="nv">exports._: </span><span class="nx">_</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-13">#</a> </div> <p>Export Underscore to global scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">root._: </span><span class="nx">_</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-14">#</a> </div> <p>Current version.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.VERSION: </span><span class="s1">'1.0.2'</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-15">#</a> </div> <h2>Collection Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-16">#</a> </div> <p>The cornerstone, an <strong>each</strong> implementation.
|
|
Handles objects implementing <strong>forEach</strong>, arrays, and raw objects.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.each: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">try</span>
|
|
<span class="k">if</span> <span class="nx">nativeForEach</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">forEach</span> <span class="o">is</span> <span class="nx">nativeForEach</span>
|
|
<span class="nx">obj</span><span class="p">.</span><span class="nx">forEach</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isNumber</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">obj</span><span class="p">)</span> <span class="k">for</span> <span class="nx">i</span> <span class="k">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">...</span><span class="nx">obj</span><span class="p">.</span><span class="nx">length</span><span class="p">]</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">val</span><span class="p">,</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">obj</span><span class="p">)</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">obj</span>
|
|
<span class="k">catch</span> <span class="nx">e</span>
|
|
<span class="k">throw</span> <span class="nx">e</span> <span class="k">if</span> <span class="nx">e</span> <span class="o">isnt</span> <span class="nx">breaker</span>
|
|
<span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-17">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.map: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="k">if</span> <span class="nx">nativeMap</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">map</span> <span class="o">is</span> <span class="nx">nativeMap</span>
|
|
<span class="nv">results: </span><span class="p">[]</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">results</span><span class="p">.</span><span class="nx">push</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span>
|
|
<span class="nx">results</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-18">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.reduce: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">memo</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduce</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">bind</span><span class="p">(</span><span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">),</span> <span class="nx">memo</span><span class="p">)</span> <span class="k">if</span> <span class="nx">nativeReduce</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduce</span> <span class="o">is</span> <span class="nx">nativeReduce</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">memo: </span><span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">memo</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span>
|
|
<span class="nx">memo</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-19">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.reduceRight: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">memo</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduceRight</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">bind</span><span class="p">(</span><span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">),</span> <span class="nx">memo</span><span class="p">)</span> <span class="k">if</span> <span class="nx">nativeReduceRight</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduceRight</span> <span class="o">is</span> <span class="nx">nativeReduceRight</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">_</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">toArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">)).</span><span class="nx">reverse</span><span class="p">(),</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">memo: </span><span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">memo</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">obj</span>
|
|
<span class="nx">memo</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-20">#</a> </div> <p>Return the first value which passes a truth test.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.detect: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">result: </span><span class="kc">null</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span>
|
|
<span class="nv">result: </span><span class="nx">value</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">breakLoop</span><span class="p">()</span>
|
|
<span class="nx">result</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-21">#</a> </div> <p>Return all the elements that pass a truth test. Use JavaScript 1.6's
|
|
<strong>filter</strong>, if it exists.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.filter: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">filter</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span> <span class="k">if</span> <span class="nx">nativeFilter</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">filter</span> <span class="o">is</span> <span class="nx">nativeFilter</span>
|
|
<span class="nv">results: </span><span class="p">[]</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">results</span><span class="p">.</span><span class="nx">push</span> <span class="nx">value</span> <span class="k">if</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span>
|
|
<span class="nx">results</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-22">#</a> </div> <p>Return all the elements for which a truth test fails.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.reject: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">results: </span><span class="p">[]</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">results</span><span class="p">.</span><span class="nx">push</span> <span class="nx">value</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span>
|
|
<span class="nx">results</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-23">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.every: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">iterator</span> <span class="o">||=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span>
|
|
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">every</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span> <span class="k">if</span> <span class="nx">nativeEvery</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">every</span> <span class="o">is</span> <span class="nx">nativeEvery</span>
|
|
<span class="nv">result: </span><span class="kc">true</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">breakLoop</span><span class="p">()</span> <span class="nx">unless</span> <span class="p">(</span><span class="nv">result: </span><span class="nx">result</span> <span class="o">and</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">))</span>
|
|
<span class="nx">result</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-24">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.some: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">iterator</span> <span class="o">||=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span>
|
|
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">some</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span> <span class="k">if</span> <span class="nx">nativeSome</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">some</span> <span class="o">is</span> <span class="nx">nativeSome</span>
|
|
<span class="nv">result: </span><span class="kc">false</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">breakLoop</span><span class="p">()</span> <span class="k">if</span> <span class="p">(</span><span class="nv">result: </span><span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">))</span>
|
|
<span class="nx">result</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-25">#</a> </div> <p>Determine if a given value is included in the array or object,
|
|
based on <code>===</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.include: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">target</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">target</span><span class="p">)</span> <span class="o">isnt</span> <span class="o">-</span><span class="mi">1</span> <span class="k">if</span> <span class="nx">nativeIndexOf</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">indexOf</span> <span class="o">is</span> <span class="nx">nativeIndexOf</span>
|
|
<span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">obj</span>
|
|
<span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">val</span> <span class="o">is</span> <span class="nx">target</span>
|
|
<span class="kc">false</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-26">#</a> </div> <p>Invoke a method with arguments on every item in a collection.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.invoke: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">method</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">args: </span><span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span><span class="p">,</span> <span class="mi">2</span>
|
|
<span class="p">(</span><span class="k">if</span> <span class="nx">method</span> <span class="k">then</span> <span class="nx">val</span><span class="p">[</span><span class="nx">method</span><span class="p">]</span> <span class="k">else</span> <span class="nx">val</span><span class="p">).</span><span class="nx">apply</span><span class="p">(</span><span class="nx">val</span><span class="p">,</span> <span class="nx">args</span><span class="p">)</span> <span class="k">for</span> <span class="nx">val</span> <span class="k">in</span> <span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-27">#</a> </div> <p>Convenience version of a common use case of <strong>map</strong>: fetching a property.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.pluck: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">key</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="o">-></span> <span class="nx">val</span><span class="p">[</span><span class="nx">key</span><span class="p">])</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-28">#</a> </div> <p>Return the maximum item or (item-based computation).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.max: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">max</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nb">Math</span><span class="p">,</span> <span class="nx">obj</span><span class="p">)</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">iterator</span> <span class="o">and</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span>
|
|
<span class="nv">result: </span><span class="p">{</span><span class="nv">computed: </span><span class="o">-</span><span class="kc">Infinity</span><span class="p">}</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">computed: </span><span class="k">if</span> <span class="nx">iterator</span> <span class="k">then</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="k">else</span> <span class="nx">value</span>
|
|
<span class="nx">computed</span> <span class="o">>=</span> <span class="nx">result</span><span class="p">.</span><span class="nx">computed</span> <span class="o">and</span> <span class="p">(</span><span class="nv">result: </span><span class="p">{</span><span class="nv">value: </span><span class="nx">value</span><span class="p">,</span> <span class="nv">computed: </span><span class="nx">computed</span><span class="p">})</span>
|
|
<span class="nx">result</span><span class="p">.</span><span class="nx">value</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-29">#</a> </div> <p>Return the minimum element (or element-based computation).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.min: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">min</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nb">Math</span><span class="p">,</span> <span class="nx">obj</span><span class="p">)</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">iterator</span> <span class="o">and</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span>
|
|
<span class="nv">result: </span><span class="p">{</span><span class="nv">computed: </span><span class="kc">Infinity</span><span class="p">}</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">computed: </span><span class="k">if</span> <span class="nx">iterator</span> <span class="k">then</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="k">else</span> <span class="nx">value</span>
|
|
<span class="nx">computed</span> <span class="o"><</span> <span class="nx">result</span><span class="p">.</span><span class="nx">computed</span> <span class="o">and</span> <span class="p">(</span><span class="nv">result: </span><span class="p">{</span><span class="nv">value: </span><span class="nx">value</span><span class="p">,</span> <span class="nv">computed: </span><span class="nx">computed</span><span class="p">})</span>
|
|
<span class="nx">result</span><span class="p">.</span><span class="nx">value</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-30">#</a> </div> <p>Sort the object's values by a criterion produced by an iterator.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.sortBy: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">pluck</span><span class="p">(((</span><span class="nx">_</span><span class="p">.</span><span class="nx">map</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">{</span><span class="nv">value: </span><span class="nx">value</span><span class="p">,</span> <span class="nv">criteria: </span><span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)}</span>
|
|
<span class="p">).</span><span class="nx">sort</span><span class="p">((</span><span class="nx">left</span><span class="p">,</span> <span class="nx">right</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">a: </span><span class="nx">left</span><span class="p">.</span><span class="nx">criteria</span><span class="p">;</span> <span class="nv">b: </span><span class="nx">right</span><span class="p">.</span><span class="nx">criteria</span>
|
|
<span class="k">if</span> <span class="nx">a</span> <span class="o"><</span> <span class="nx">b</span> <span class="k">then</span> <span class="o">-</span><span class="mi">1</span> <span class="k">else</span> <span class="k">if</span> <span class="nx">a</span> <span class="o">></span> <span class="nx">b</span> <span class="k">then</span> <span class="mi">1</span> <span class="k">else</span> <span class="mi">0</span>
|
|
<span class="p">)),</span> <span class="s1">'value'</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-31">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.sortedIndex: </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">iterator</span> <span class="o">||=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span>
|
|
<span class="nv">low: </span> <span class="mi">0</span>
|
|
<span class="nv">high: </span><span class="nx">array</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="k">while</span> <span class="nx">low</span> <span class="o"><</span> <span class="nx">high</span>
|
|
<span class="nv">mid: </span><span class="p">(</span><span class="nx">low</span> <span class="o">+</span> <span class="nx">high</span><span class="p">)</span> <span class="o">>></span> <span class="mi">1</span>
|
|
<span class="k">if</span> <span class="nx">iterator</span><span class="p">(</span><span class="nx">array</span><span class="p">[</span><span class="nx">mid</span><span class="p">])</span> <span class="o"><</span> <span class="nx">iterator</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="k">then</span> <span class="nv">low: </span><span class="nx">mid</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">else</span> <span class="nv">high: </span><span class="nx">mid</span>
|
|
<span class="nx">low</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-32">#</a> </div> <p>Convert anything iterable into a real, live array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.toArray: </span><span class="p">(</span><span class="nx">iterable</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="p">[]</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">iterable</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="nx">iterable</span><span class="p">.</span><span class="nx">toArray</span><span class="p">()</span> <span class="k">if</span> <span class="p">(</span><span class="nx">iterable</span><span class="p">.</span><span class="nx">toArray</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="nx">iterable</span> <span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">iterable</span><span class="p">))</span>
|
|
<span class="k">return</span> <span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">iterable</span><span class="p">)</span> <span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">isArguments</span><span class="p">(</span><span class="nx">iterable</span><span class="p">))</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">values</span><span class="p">(</span><span class="nx">iterable</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-33">#</a> </div> <p>Return the number of elements in an object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.size: </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">_</span><span class="p">.</span><span class="nx">toArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">).</span><span class="nx">length</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-34">#</a> </div> <h2>Array Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-35">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.first: </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">n</span><span class="p">,</span> <span class="nx">guard</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">n</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">guard</span> <span class="k">then</span> <span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">n</span><span class="p">)</span> <span class="k">else</span> <span class="nx">array</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-36">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.rest: </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">guard</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isUndefined</span><span class="p">(</span><span class="nx">index</span><span class="p">)</span> <span class="o">or</span> <span class="nx">guard</span> <span class="k">then</span> <span class="mi">1</span> <span class="k">else</span> <span class="nx">index</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-37">#</a> </div> <p>Get the last element of an array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.last: </span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="o">-></span> <span class="nx">array</span><span class="p">[</span><span class="nx">array</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-38">#</a> </div> <p>Trim out all falsy values from an array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.compact: </span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="o">-></span> <span class="nx">item</span> <span class="k">for</span> <span class="nx">item</span> <span class="k">in</span> <span class="nx">array</span> <span class="k">when</span> <span class="nx">item</span></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-39">#</a> </div> <p>Return a completely flattened version of an array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.flatten: </span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">reduce</span> <span class="nx">array</span><span class="p">,</span> <span class="p">[],</span> <span class="p">(</span><span class="nx">memo</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">memo</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">flatten</span><span class="p">(</span><span class="nx">value</span><span class="p">))</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span> <span class="nx">value</span>
|
|
<span class="nx">memo</span><span class="p">.</span><span class="nx">push</span> <span class="nx">value</span>
|
|
<span class="nx">memo</span></pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-40">#</a> </div> <p>Return a version of the array that does not contain the specified value(s).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.without: </span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">values: </span><span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span>
|
|
<span class="nx">val</span> <span class="k">for</span> <span class="nx">val</span> <span class="k">in</span> <span class="nx">_</span><span class="p">.</span><span class="nx">toArray</span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="k">when</span> <span class="o">not</span> <span class="nx">_</span><span class="p">.</span><span class="nx">include</span> <span class="nx">values</span><span class="p">,</span> <span class="nx">val</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-41">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.uniq: </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">isSorted</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">memo: </span><span class="p">[]</span>
|
|
<span class="k">for</span> <span class="nx">el</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">_</span><span class="p">.</span><span class="nx">toArray</span> <span class="nx">array</span>
|
|
<span class="nx">memo</span><span class="p">.</span><span class="nx">push</span> <span class="nx">el</span> <span class="k">if</span> <span class="nx">i</span> <span class="o">is</span> <span class="mi">0</span> <span class="o">||</span> <span class="p">(</span><span class="k">if</span> <span class="nx">isSorted</span> <span class="o">is</span> <span class="kc">true</span> <span class="k">then</span> <span class="nx">_</span><span class="p">.</span><span class="nx">last</span><span class="p">(</span><span class="nx">memo</span><span class="p">)</span> <span class="o">isnt</span> <span class="nx">el</span> <span class="k">else</span> <span class="o">not</span> <span class="nx">_</span><span class="p">.</span><span class="nx">include</span><span class="p">(</span><span class="nx">memo</span><span class="p">,</span> <span class="nx">el</span><span class="p">))</span>
|
|
<span class="nx">memo</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-42">#</a> </div> <p>Produce an array that contains every item shared between all the
|
|
passed-in arrays.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.intersect: </span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">rest: </span><span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">select</span> <span class="nx">_</span><span class="p">.</span><span class="nx">uniq</span><span class="p">(</span><span class="nx">array</span><span class="p">),</span> <span class="p">(</span><span class="nx">item</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">all</span> <span class="nx">rest</span><span class="p">,</span> <span class="p">(</span><span class="nx">other</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">other</span><span class="p">,</span> <span class="nx">item</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">0</span></pre></div> </td> </tr> <tr id="section-43"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-43">#</a> </div> <p>Zip together multiple lists into a single array -- elements that share
|
|
an index go together.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.zip: </span><span class="o">-></span>
|
|
<span class="nv">length: </span> <span class="nx">_</span><span class="p">.</span><span class="nx">max</span> <span class="nx">_</span><span class="p">.</span><span class="nx">pluck</span> <span class="nx">arguments</span><span class="p">,</span> <span class="s1">'length'</span>
|
|
<span class="nv">results: </span><span class="k">new</span> <span class="nb">Array</span> <span class="nx">length</span>
|
|
<span class="k">for</span> <span class="nx">i</span> <span class="k">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">...</span><span class="nx">length</span><span class="p">]</span>
|
|
<span class="nx">results</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="o">:</span> <span class="nx">_</span><span class="p">.</span><span class="nx">pluck</span> <span class="nx">arguments</span><span class="p">,</span> <span class="nb">String</span> <span class="nx">i</span>
|
|
<span class="nx">results</span></pre></div> </td> </tr> <tr id="section-44"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-44">#</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 occurence of an
|
|
item in an array, or -1 if the item is not included in the array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.indexOf: </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">item</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">array</span><span class="p">.</span><span class="nx">indexOf</span> <span class="nx">item</span> <span class="k">if</span> <span class="nx">nativeIndexOf</span> <span class="o">and</span> <span class="nx">array</span><span class="p">.</span><span class="nx">indexOf</span> <span class="o">is</span> <span class="nx">nativeIndexOf</span>
|
|
<span class="nv">i: </span><span class="mi">0</span><span class="p">;</span> <span class="nv">l: </span><span class="nx">array</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="k">while</span> <span class="nx">l</span> <span class="o">-</span> <span class="nx">i</span>
|
|
<span class="k">if</span> <span class="nx">array</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">is</span> <span class="nx">item</span> <span class="k">then</span> <span class="k">return</span> <span class="nx">i</span> <span class="k">else</span> <span class="nx">i</span><span class="o">++</span>
|
|
<span class="o">-</span><span class="mi">1</span></pre></div> </td> </tr> <tr id="section-45"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-45">#</a> </div> <p>Provide JavaScript 1.6's <strong>lastIndexOf</strong>, delegating to the native function,
|
|
if possible.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.lastIndexOf: </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">item</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">array</span><span class="p">.</span><span class="nx">lastIndexOf</span><span class="p">(</span><span class="nx">item</span><span class="p">)</span> <span class="k">if</span> <span class="nx">nativeLastIndexOf</span> <span class="o">and</span> <span class="nx">array</span><span class="p">.</span><span class="nx">lastIndexOf</span> <span class="o">is</span> <span class="nx">nativeLastIndexOf</span>
|
|
<span class="nv">i: </span><span class="nx">array</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="k">while</span> <span class="nx">i</span>
|
|
<span class="k">if</span> <span class="nx">array</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">is</span> <span class="nx">item</span> <span class="k">then</span> <span class="k">return</span> <span class="nx">i</span> <span class="k">else</span> <span class="nx">i</span><span class="o">--</span>
|
|
<span class="o">-</span><span class="mi">1</span></pre></div> </td> </tr> <tr id="section-46"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-46">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.range: </span><span class="p">(</span><span class="nx">start</span><span class="p">,</span> <span class="nx">stop</span><span class="p">,</span> <span class="nx">step</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">a: </span> <span class="nx">arguments</span>
|
|
<span class="nv">solo: </span> <span class="nx">a</span><span class="p">.</span><span class="nx">length</span> <span class="o"><=</span> <span class="mi">1</span>
|
|
<span class="nv">i: start: </span><span class="k">if</span> <span class="nx">solo</span> <span class="k">then</span> <span class="mi">0</span> <span class="k">else</span> <span class="nx">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
|
<span class="nv">stop: </span> <span class="k">if</span> <span class="nx">solo</span> <span class="k">then</span> <span class="nx">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">else</span> <span class="nx">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
|
<span class="nv">step: </span> <span class="nx">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">or</span> <span class="mi">1</span>
|
|
<span class="nv">len: </span> <span class="nb">Math</span><span class="p">.</span><span class="nx">ceil</span><span class="p">((</span><span class="nx">stop</span> <span class="o">-</span> <span class="nx">start</span><span class="p">)</span> <span class="o">/</span> <span class="nx">step</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="p">[]</span> <span class="k">if</span> <span class="nx">len</span> <span class="o"><=</span> <span class="mi">0</span>
|
|
<span class="nv">range: </span> <span class="k">new</span> <span class="nb">Array</span> <span class="nx">len</span>
|
|
<span class="nv">idx: </span> <span class="mi">0</span>
|
|
<span class="k">while</span> <span class="kc">true</span>
|
|
<span class="k">return</span> <span class="nx">range</span> <span class="k">if</span> <span class="p">(</span><span class="k">if</span> <span class="nx">step</span> <span class="o">></span> <span class="mi">0</span> <span class="k">then</span> <span class="nx">i</span> <span class="o">-</span> <span class="nx">stop</span> <span class="k">else</span> <span class="nx">stop</span> <span class="o">-</span> <span class="nx">i</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">0</span>
|
|
<span class="nx">range</span><span class="p">[</span><span class="nx">idx</span><span class="p">]</span><span class="o">:</span> <span class="nx">i</span>
|
|
<span class="nx">idx</span><span class="o">++</span>
|
|
<span class="nx">i</span><span class="o">+=</span> <span class="nx">step</span></pre></div> </td> </tr> <tr id="section-47"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-47">#</a> </div> <h2>Function Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-48">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.bind: </span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">args: </span><span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span><span class="p">,</span> <span class="mi">2</span>
|
|
<span class="o">-></span> <span class="nx">func</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">obj</span> <span class="o">or</span> <span class="nx">root</span><span class="p">,</span> <span class="nx">args</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">arguments</span></pre></div> </td> </tr> <tr id="section-49"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-49">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.bindAll: </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">funcs: </span><span class="k">if</span> <span class="nx">arguments</span><span class="p">.</span><span class="nx">length</span> <span class="o">></span> <span class="mi">1</span> <span class="k">then</span> <span class="nx">_</span><span class="p">.</span><span class="nx">rest</span><span class="p">(</span><span class="nx">arguments</span><span class="p">)</span> <span class="k">else</span> <span class="nx">_</span><span class="p">.</span><span class="nx">functions</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">funcs</span><span class="p">,</span> <span class="p">(</span><span class="nx">f</span><span class="p">)</span> <span class="o">-></span> <span class="nx">obj</span><span class="p">[</span><span class="nx">f</span><span class="p">]</span><span class="o">:</span> <span class="nx">_</span><span class="p">.</span><span class="nx">bind</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">f</span><span class="p">],</span> <span class="nx">obj</span>
|
|
<span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-50">#</a> </div> <p>Delays a function for the given number of milliseconds, and then calls
|
|
it with the arguments supplied.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.delay: </span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">wait</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">args: </span><span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span><span class="p">,</span> <span class="mi">2</span>
|
|
<span class="nx">setTimeout</span><span class="p">((</span><span class="o">-></span> <span class="nx">func</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">args</span><span class="p">)),</span> <span class="nx">wait</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-51">#</a> </div> <p>Defers a function, scheduling it to run after the current call stack has
|
|
cleared.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.defer: </span><span class="p">(</span><span class="nx">func</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">delay</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">_</span><span class="p">,</span> <span class="p">[</span><span class="nx">func</span><span class="p">,</span> <span class="mi">1</span><span class="p">].</span><span class="nx">concat</span> <span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-52">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.wrap: </span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">wrapper</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="o">-></span> <span class="nx">wrapper</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">wrapper</span><span class="p">,</span> <span class="p">[</span><span class="nx">func</span><span class="p">].</span><span class="nx">concat</span> <span class="nx">arguments</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-53">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.compose: </span><span class="o">-></span>
|
|
<span class="nv">funcs: </span><span class="nx">arguments</span>
|
|
<span class="o">-></span>
|
|
<span class="nv">args: </span><span class="nx">arguments</span>
|
|
<span class="k">for</span> <span class="nx">i</span> <span class="k">in</span> <span class="p">[(</span><span class="nx">funcs</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)..</span><span class="mi">0</span><span class="p">]</span>
|
|
<span class="nv">args: </span><span class="p">[</span><span class="nx">funcs</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">args</span><span class="p">)]</span>
|
|
<span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-54">#</a> </div> <h2>Object Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-55">#</a> </div> <p>Retrieve the names of an object's properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.keys: </span><span class="nx">nativeKeys</span> <span class="o">or</span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">range</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span>
|
|
<span class="nx">key</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-56">#</a> </div> <p>Retrieve the values of an object's properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.values: </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">map</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-57">#</a> </div> <p>Return a sorted list of the function names available in Underscore.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.functions: </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">obj</span><span class="p">),</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="o">-></span> <span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">obj</span><span class="p">[</span><span class="nx">key</span><span class="p">])).</span><span class="nx">sort</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-58">#</a> </div> <p>Extend a given object with all of the properties in a source object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.extend: </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">for</span> <span class="nx">source</span> <span class="k">in</span> <span class="nx">_</span><span class="p">.</span><span class="nx">rest</span><span class="p">(</span><span class="nx">arguments</span><span class="p">)</span>
|
|
<span class="p">(</span><span class="nx">obj</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="o">:</span> <span class="nx">val</span><span class="p">)</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">source</span>
|
|
<span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-59">#</a> </div> <p>Create a (shallow-cloned) duplicate of an object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.clone: </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">slice</span> <span class="mi">0</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span> <span class="nx">obj</span>
|
|
<span class="nx">_</span><span class="p">.</span><span class="nx">extend</span> <span class="p">{},</span> <span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-60"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-60">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.tap: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">interceptor</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">interceptor</span> <span class="nx">obj</span>
|
|
<span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-61">#</a> </div> <p>Perform a deep comparison to check if two objects are equal.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isEqual: </span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="o">-></span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-62">#</a> </div> <p>Check object identity.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">a</span> <span class="o">is</span> <span class="nx">b</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-63">#</a> </div> <p>Different types?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">atype: </span><span class="k">typeof</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span> <span class="nv">btype: </span><span class="k">typeof</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">atype</span> <span class="o">isnt</span> <span class="nx">btype</span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-64">#</a> </div> <p>Basic equality test (watch out for coercions).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="o">`</span><span class="nx">a</span> <span class="o">==</span> <span class="nx">b</span><span class="o">`</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-65">#</a> </div> <p>One is falsy and the other truthy.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">a</span> <span class="o">and</span> <span class="nx">b</span><span class="p">)</span> <span class="o">or</span> <span class="p">(</span><span class="nx">a</span> <span class="o">and</span> <span class="o">!</span><span class="nx">b</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-66">#</a> </div> <p>One of them implements an <code>isEqual()</code>?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">a</span><span class="p">.</span><span class="nx">isEqual</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span> <span class="k">if</span> <span class="nx">a</span><span class="p">.</span><span class="nx">isEqual</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-67">#</a> </div> <p>Check dates' integer values.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">a</span><span class="p">.</span><span class="nx">getTime</span><span class="p">()</span> <span class="o">is</span> <span class="nx">b</span><span class="p">.</span><span class="nx">getTime</span><span class="p">()</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isDate</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span> <span class="o">and</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isDate</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-68">#</a> </div> <p>Both are NaN?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span> <span class="o">and</span> <span class="nx">_</span><span class="p">.</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-69">#</a> </div> <p>Compare regular expressions.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isRegExp</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span> <span class="o">and</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isRegExp</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="nx">a</span><span class="p">.</span><span class="nx">source</span> <span class="o">is</span> <span class="nx">b</span><span class="p">.</span><span class="nx">source</span> <span class="o">and</span>
|
|
<span class="nx">a</span><span class="p">.</span><span class="nx">global</span> <span class="o">is</span> <span class="nx">b</span><span class="p">.</span><span class="nx">global</span> <span class="o">and</span>
|
|
<span class="nx">a</span><span class="p">.</span><span class="nx">ignoreCase</span> <span class="o">is</span> <span class="nx">b</span><span class="p">.</span><span class="nx">ignoreCase</span> <span class="o">and</span>
|
|
<span class="nx">a</span><span class="p">.</span><span class="nx">multiline</span> <span class="o">is</span> <span class="nx">b</span><span class="p">.</span><span class="nx">multiline</span></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-70">#</a> </div> <p>If a is not an object by this point, we can't handle it.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">atype</span> <span class="o">isnt</span> <span class="s1">'object'</span></pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-71">#</a> </div> <p>Check for different array lengths before comparing contents.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">a</span><span class="p">.</span><span class="nx">length</span> <span class="o">and</span> <span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">length</span> <span class="o">isnt</span> <span class="nx">b</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-72"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-72">#</a> </div> <p>Nothing else worked, deep compare the contents.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">aKeys: </span><span class="nx">_</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span> <span class="nv">bKeys: </span><span class="nx">_</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-73"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-73">#</a> </div> <p>Different object sizes?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">aKeys</span><span class="p">.</span><span class="nx">length</span> <span class="o">isnt</span> <span class="nx">bKeys</span><span class="p">.</span><span class="nx">length</span></pre></div> </td> </tr> <tr id="section-74"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-74">#</a> </div> <p>Recursive comparison of contents.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="p">(</span><span class="k">return</span> <span class="kc">false</span><span class="p">)</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">a</span> <span class="k">when</span> <span class="o">!</span><span class="p">(</span><span class="nx">key</span> <span class="k">in</span> <span class="nx">b</span><span class="p">)</span> <span class="o">or</span> <span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">isEqual</span><span class="p">(</span><span class="nx">val</span><span class="p">,</span> <span class="nx">b</span><span class="p">[</span><span class="nx">key</span><span class="p">])</span>
|
|
<span class="kc">true</span></pre></div> </td> </tr> <tr id="section-75"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-75">#</a> </div> <p>Is a given array or object empty?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isEmpty: </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">0</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span> <span class="nx">obj</span>
|
|
<span class="p">(</span><span class="k">return</span> <span class="kc">false</span><span class="p">)</span> <span class="k">for</span> <span class="nx">key</span> <span class="k">of</span> <span class="nx">obj</span> <span class="k">when</span> <span class="nx">hasOwnProperty</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">key</span><span class="p">)</span>
|
|
<span class="kc">true</span></pre></div> </td> </tr> <tr id="section-76"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-76">#</a> </div> <p>Is a given value a DOM element?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isElement: </span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">nodeType</span> <span class="o">is</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-77"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-77">#</a> </div> <p>Is a given value an array?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isArray: </span> <span class="nx">nativeIsArray</span> <span class="o">or</span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">concat</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">unshift</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">callee</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-78"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-78">#</a> </div> <p>Is a given variable an arguments object?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isArguments: </span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">callee</span></pre></div> </td> </tr> <tr id="section-79"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-79">#</a> </div> <p>Is the given value a function?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isFunction: </span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">constructor</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">call</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">apply</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-80"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-80">#</a> </div> <p>Is the given value a string?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isString: </span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">is</span> <span class="s1">''</span> <span class="o">or</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">charCodeAt</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">substr</span><span class="p">))</span></pre></div> </td> </tr> <tr id="section-81"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-81">#</a> </div> <p>Is a given value a number?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isNumber: </span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="p">(</span><span class="nx">obj</span> <span class="o">is</span> <span class="o">+</span><span class="nx">obj</span><span class="p">)</span> <span class="o">or</span> <span class="nx">toString</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'[object Number]'</span></pre></div> </td> </tr> <tr id="section-82"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-82">#</a> </div> <p>Is a given value a boolean?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isBoolean: </span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">obj</span> <span class="o">is</span> <span class="kc">true</span> <span class="o">or</span> <span class="nx">obj</span> <span class="o">is</span> <span class="kc">false</span></pre></div> </td> </tr> <tr id="section-83"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-83">#</a> </div> <p>Is a given value a Date?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isDate: </span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">getTimezoneOffset</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">setUTCFullYear</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-84"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-84">#</a> </div> <p>Is the given value a regular expression?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isRegExp: </span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">exec</span> <span class="o">and</span> <span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">ignoreCase</span> <span class="o">or</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">ignoreCase</span> <span class="o">is</span> <span class="kc">false</span><span class="p">))</span></pre></div> </td> </tr> <tr id="section-85"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-85">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isNaN: </span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">_</span><span class="p">.</span><span class="nx">isNumber</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">and</span> <span class="nb">window</span><span class="p">.</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-86"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-86">#</a> </div> <p>Is a given value equal to null?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isNull: </span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">obj</span> <span class="o">is</span> <span class="kc">null</span></pre></div> </td> </tr> <tr id="section-87"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-87">#</a> </div> <p>Is a given variable undefined?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.isUndefined: </span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="k">typeof</span> <span class="nx">obj</span> <span class="o">is</span> <span class="s1">'undefined'</span></pre></div> </td> </tr> <tr id="section-88"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-88">#</a> </div> <h2>Utility Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-89"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-89">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.noConflict: </span><span class="o">-></span>
|
|
<span class="nv">root._: </span><span class="nx">previousUnderscore</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-90"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-90">#</a> </div> <p>Keep the identity function around for default iterators.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.identity: </span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">-></span> <span class="nx">value</span></pre></div> </td> </tr> <tr id="section-91"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-91">#</a> </div> <p>Run a function <code>n</code> times.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.times: </span><span class="p">(</span><span class="nx">n</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="k">for</span> <span class="nx">i</span> <span class="k">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">...</span><span class="nx">n</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-92"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-92">#</a> </div> <p>Break out of the middle of an iteration.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.breakLoop: </span><span class="o">-></span> <span class="k">throw</span> <span class="nx">breaker</span></pre></div> </td> </tr> <tr id="section-93"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-93">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.mixin: </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">for</span> <span class="nx">name</span> <span class="k">in</span> <span class="nx">_</span><span class="p">.</span><span class="nx">functions</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span>
|
|
<span class="nx">addToWrapper</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">_</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span><span class="o">:</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-94"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-94">#</a> </div> <p>Generate a unique integer id (unique within the entire client session).
|
|
Useful for temporary DOM ids.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">idCounter: </span><span class="mi">0</span>
|
|
<span class="nv">_.uniqueId: </span><span class="p">(</span><span class="nx">prefix</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">(</span><span class="nx">prefix</span> <span class="o">or</span> <span class="s1">''</span><span class="p">)</span> <span class="o">+</span> <span class="nx">idCounter</span><span class="o">++</span></pre></div> </td> </tr> <tr id="section-95"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-95">#</a> </div> <p>By default, Underscore uses <strong>ERB</strong>-style template delimiters, change the
|
|
following template settings to use alternative delimiters.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.templateSettings: </span><span class="p">{</span>
|
|
<span class="nv">start: </span> <span class="s1">'<%'</span>
|
|
<span class="nv">end: </span> <span class="s1">'%>'</span>
|
|
<span class="nv">interpolate: </span> <span class="sr">/<%=(.+?)%>/g</span>
|
|
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-96"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-96">#</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.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.template: </span><span class="p">(</span><span class="nx">str</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">c: </span><span class="nx">_</span><span class="p">.</span><span class="nx">templateSettings</span>
|
|
<span class="nv">endMatch: </span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s2">"'(?=[^"</span><span class="o">+</span><span class="nx">c</span><span class="p">.</span><span class="nx">end</span><span class="p">.</span><span class="nx">substr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span><span class="o">+</span><span class="s2">"]*"</span><span class="o">+</span><span class="nx">escapeRegExp</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">end</span><span class="p">)</span><span class="o">+</span><span class="s2">")"</span><span class="p">,</span><span class="s2">"g"</span><span class="p">)</span>
|
|
<span class="nv">fn: </span><span class="k">new</span> <span class="nb">Function</span> <span class="s1">'obj'</span><span class="p">,</span>
|
|
<span class="s1">'var p=[],print=function(){p.push.apply(p,arguments);};'</span> <span class="o">+</span>
|
|
<span class="s1">'with(obj){p.push(\''</span> <span class="o">+</span>
|
|
<span class="nx">str</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/[\r\t\n]/g</span><span class="p">,</span> <span class="s2">" "</span><span class="p">)</span>
|
|
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">endMatch</span><span class="p">,</span><span class="s2">"\t"</span><span class="p">)</span>
|
|
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s2">"'"</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s2">"\\'"</span><span class="p">)</span>
|
|
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s2">"\t"</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s2">"'"</span><span class="p">)</span>
|
|
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">interpolate</span><span class="p">,</span> <span class="s2">"',$1,'"</span><span class="p">)</span>
|
|
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">start</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s2">"');"</span><span class="p">)</span>
|
|
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">end</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s2">"p.push('"</span><span class="p">)</span> <span class="o">+</span>
|
|
<span class="s2">"');}return p.join('');"</span>
|
|
<span class="k">if</span> <span class="nx">data</span> <span class="k">then</span> <span class="nx">fn</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="k">else</span> <span class="nx">fn</span></pre></div> </td> </tr> <tr id="section-97"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-97">#</a> </div> <h2>Aliases</h2> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">_.forEach: </span><span class="nx">_</span><span class="p">.</span><span class="nx">each</span>
|
|
<span class="nv">_.foldl: </span> <span class="nv">_.inject: </span> <span class="nx">_</span><span class="p">.</span><span class="nx">reduce</span>
|
|
<span class="nv">_.foldr: </span> <span class="nx">_</span><span class="p">.</span><span class="nx">reduceRight</span>
|
|
<span class="nv">_.select: </span> <span class="nx">_</span><span class="p">.</span><span class="nx">filter</span>
|
|
<span class="nv">_.all: </span> <span class="nx">_</span><span class="p">.</span><span class="nx">every</span>
|
|
<span class="nv">_.any: </span> <span class="nx">_</span><span class="p">.</span><span class="nx">some</span>
|
|
<span class="nv">_.head: </span> <span class="nx">_</span><span class="p">.</span><span class="nx">first</span>
|
|
<span class="nv">_.tail: </span> <span class="nx">_</span><span class="p">.</span><span class="nx">rest</span>
|
|
<span class="nv">_.methods: </span><span class="nx">_</span><span class="p">.</span><span class="nx">functions</span></pre></div> </td> </tr> <tr id="section-98"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-98">#</a> </div> <h2>Setup the OOP Wrapper</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-99"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-99">#</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> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">wrapper: </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">this</span><span class="p">.</span><span class="nv">_wrapped: </span><span class="nx">obj</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-100"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-100">#</a> </div> <p>Helper function to continue chaining intermediate results.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">result: </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">chain</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">chain</span> <span class="k">then</span> <span class="nx">_</span><span class="p">(</span><span class="nx">obj</span><span class="p">).</span><span class="nx">chain</span><span class="p">()</span> <span class="k">else</span> <span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-101"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-101">#</a> </div> <p>A method to easily add functions to the OOP wrapper.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">addToWrapper: </span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">func</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">wrapper</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nv">args: </span><span class="nx">_</span><span class="p">.</span><span class="nx">toArray</span> <span class="nx">arguments</span>
|
|
<span class="nx">unshift</span><span class="p">.</span><span class="nx">call</span> <span class="nx">args</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span>
|
|
<span class="nx">result</span> <span class="nx">func</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">_</span><span class="p">,</span> <span class="nx">args</span><span class="p">),</span> <span class="k">this</span><span class="p">.</span><span class="nx">_chain</span></pre></div> </td> </tr> <tr id="section-102"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-102">#</a> </div> <p>Add all of the Underscore functions to the wrapper object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">mixin</span> <span class="nx">_</span></pre></div> </td> </tr> <tr id="section-103"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-103">#</a> </div> <p>Add all mutator Array functions to the wrapper.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="p">[</span><span class="s1">'pop'</span><span class="p">,</span> <span class="s1">'push'</span><span class="p">,</span> <span class="s1">'reverse'</span><span class="p">,</span> <span class="s1">'shift'</span><span class="p">,</span> <span class="s1">'sort'</span><span class="p">,</span> <span class="s1">'splice'</span><span class="p">,</span> <span class="s1">'unshift'</span><span class="p">],</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">method: </span><span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span>
|
|
<span class="nx">wrapper</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">method</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">)</span>
|
|
<span class="nx">result</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">_chain</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-104"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-104">#</a> </div> <p>Add all accessor Array functions to the wrapper.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="p">[</span><span class="s1">'concat'</span><span class="p">,</span> <span class="s1">'join'</span><span class="p">,</span> <span class="s1">'slice'</span><span class="p">],</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">method: </span><span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span>
|
|
<span class="nx">wrapper</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">result</span><span class="p">(</span><span class="nx">method</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">),</span> <span class="k">this</span><span class="p">.</span><span class="nx">_chain</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-105"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-105">#</a> </div> <p>Start chaining a wrapped Underscore object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">wrapper::chain: </span><span class="o">-></span>
|
|
<span class="k">this</span><span class="p">.</span><span class="nv">_chain: </span><span class="kc">true</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-106"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-106">#</a> </div> <p>Extracts the result from a wrapped and chained object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">wrapper::value: </span><span class="o">-></span> <span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span>
|
|
|
|
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html> |