twigg-app 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/assets/javascripts/_bootstrap.js +7 -0
- data/assets/javascripts/_jquery.js +19 -0
- data/assets/javascripts/_tables.js +3 -0
- data/assets/javascripts/_views.js +10 -0
- data/assets/javascripts/application.js +9 -0
- data/{public/application.js → assets/javascripts/views/_commit_set_bar_chart.js} +32 -34
- data/assets/javascripts/views/_russia.js +61 -0
- data/assets/javascripts/views/_tags.js +56 -0
- data/assets/javascripts/views/_tags_word_cloud.js +51 -0
- data/assets/stylesheets/_bootstrap_overrides.scss +15 -0
- data/assets/stylesheets/_footer.scss +28 -0
- data/assets/stylesheets/_global.scss +14 -0
- data/assets/stylesheets/_tables.scss +20 -0
- data/assets/stylesheets/application.scss +10 -0
- data/assets/stylesheets/d3/_bar_chart.scss +25 -0
- data/assets/stylesheets/d3/_bubble_chart.scss +22 -0
- data/lib/twigg-app/app/routes.rb +4 -0
- data/lib/twigg-app/app/server.rb +78 -21
- data/lib/twigg-app/app/version.rb +1 -1
- data/public/vendor/bootstrap/Gruntfile.js +32 -10
- data/public/vendor/bootstrap/README.md +2 -0
- data/public/vendor/bootstrap/_config.yml +5 -10
- data/public/vendor/bootstrap/_includes/footer.html +2 -1
- data/public/vendor/bootstrap/_includes/nav-components.html +33 -31
- data/public/vendor/bootstrap/_includes/nav-css.html +32 -22
- data/public/vendor/bootstrap/_includes/nav-getting-started.html +17 -1
- data/public/vendor/bootstrap/_includes/nav-javascript.html +1 -1
- data/public/vendor/bootstrap/_includes/nav-main.html +1 -1
- data/public/vendor/bootstrap/_includes/social-buttons.html +1 -1
- data/public/vendor/bootstrap/_layouts/default.html +18 -14
- data/public/vendor/bootstrap/_layouts/home.html +9 -6
- data/public/vendor/bootstrap/assets/css/docs.css +584 -396
- data/public/vendor/bootstrap/assets/ico/apple-touch-icon-114-precomposed.png +0 -0
- data/public/vendor/bootstrap/assets/ico/apple-touch-icon-144-precomposed.png +0 -0
- data/public/vendor/bootstrap/assets/ico/apple-touch-icon-57-precomposed.png +0 -0
- data/public/vendor/bootstrap/assets/ico/apple-touch-icon-72-precomposed.png +0 -0
- data/public/vendor/bootstrap/assets/ico/favicon.png +0 -0
- data/public/vendor/bootstrap/assets/js/application.js +2 -1
- data/public/vendor/bootstrap/assets/js/customizer.js +160 -45
- data/public/vendor/bootstrap/assets/js/filesaver.js +169 -0
- data/public/vendor/bootstrap/assets/js/jquery.js +6 -5
- data/public/vendor/bootstrap/assets/js/raw-files.js +3 -0
- data/public/vendor/bootstrap/bower.json +1 -1
- data/public/vendor/bootstrap/components.html +787 -364
- data/public/vendor/bootstrap/composer.json +1 -1
- data/public/vendor/bootstrap/css.html +413 -248
- data/public/vendor/bootstrap/customize.html +413 -447
- data/public/vendor/bootstrap/dist/css/bootstrap-theme.css +384 -0
- data/public/vendor/bootstrap/dist/css/bootstrap-theme.min.css +1 -0
- data/public/vendor/bootstrap/dist/css/bootstrap.css +3033 -1807
- data/public/vendor/bootstrap/dist/css/bootstrap.min.css +1 -1
- data/public/vendor/bootstrap/dist/fonts/glyphicons-halflings-regular.eot +0 -0
- data/public/vendor/bootstrap/dist/fonts/glyphicons-halflings-regular.svg +228 -0
- data/public/vendor/bootstrap/dist/fonts/glyphicons-halflings-regular.ttf +0 -0
- data/public/vendor/bootstrap/dist/fonts/glyphicons-halflings-regular.woff +0 -0
- data/public/vendor/bootstrap/dist/js/bootstrap.js +18 -12
- data/public/vendor/bootstrap/dist/js/bootstrap.min.js +1 -1
- data/public/vendor/bootstrap/examples/carousel/carousel.css +128 -0
- data/public/vendor/bootstrap/examples/carousel/index.html +201 -0
- data/public/vendor/bootstrap/examples/grid/grid.css +28 -0
- data/public/vendor/bootstrap/examples/grid/index.html +119 -0
- data/public/vendor/bootstrap/examples/jumbotron/index.html +111 -0
- data/public/vendor/bootstrap/examples/jumbotron/jumbotron.css +5 -0
- data/public/vendor/bootstrap/examples/jumbotron-narrow/index.html +78 -0
- data/public/vendor/bootstrap/examples/jumbotron-narrow/jumbotron-narrow.css +79 -0
- data/public/vendor/bootstrap/examples/justified-nav/index.html +79 -0
- data/public/vendor/bootstrap/examples/justified-nav/justified-nav.css +88 -0
- data/public/vendor/bootstrap/examples/navbar/index.html +83 -0
- data/public/vendor/bootstrap/examples/navbar/navbar.css +7 -0
- data/public/vendor/bootstrap/examples/navbar-fixed-top/index.html +86 -0
- data/public/vendor/bootstrap/examples/navbar-fixed-top/navbar-fixed-top.css +4 -0
- data/public/vendor/bootstrap/examples/navbar-static-top/index.html +87 -0
- data/public/vendor/bootstrap/examples/navbar-static-top/navbar-static-top.css +7 -0
- data/public/vendor/bootstrap/examples/non-responsive/index.html +96 -0
- data/public/vendor/bootstrap/examples/non-responsive/non-responsive.css +117 -0
- data/public/vendor/bootstrap/examples/offcanvas/index.html +127 -0
- data/public/vendor/bootstrap/examples/offcanvas/offcanvas.css +48 -0
- data/public/vendor/bootstrap/examples/offcanvas/offcanvas.js +5 -0
- data/public/vendor/bootstrap/examples/screenshots/carousel.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/grid.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/jumbotron-narrow.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/jumbotron.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/justified-nav.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/navbar-fixed.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/navbar-static.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/navbar.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/non-responsive.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/offcanvas.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/sign-in.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/starter-template.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/sticky-footer-navbar.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/sticky-footer.jpg +0 -0
- data/public/vendor/bootstrap/examples/screenshots/theme.jpg +0 -0
- data/public/vendor/bootstrap/examples/signin/index.html +46 -0
- data/public/vendor/bootstrap/examples/signin/signin.css +40 -0
- data/public/vendor/bootstrap/examples/starter-template/index.html +63 -0
- data/public/vendor/bootstrap/examples/starter-template/starter-template.css +7 -0
- data/public/vendor/bootstrap/examples/sticky-footer/index.html +51 -0
- data/public/vendor/bootstrap/examples/sticky-footer/sticky-footer.css +39 -0
- data/public/vendor/bootstrap/examples/sticky-footer-navbar/index.html +86 -0
- data/public/vendor/bootstrap/examples/sticky-footer-navbar/sticky-footer-navbar.css +46 -0
- data/public/vendor/bootstrap/examples/theme/index.html +387 -0
- data/public/vendor/bootstrap/examples/theme/theme.css +14 -0
- data/public/vendor/bootstrap/fonts/glyphicons-halflings-regular.eot +0 -0
- data/public/vendor/bootstrap/fonts/glyphicons-halflings-regular.svg +228 -0
- data/public/vendor/bootstrap/fonts/glyphicons-halflings-regular.ttf +0 -0
- data/public/vendor/bootstrap/fonts/glyphicons-halflings-regular.woff +0 -0
- data/public/vendor/bootstrap/getting-started.html +513 -22
- data/public/vendor/bootstrap/index.html +3 -4
- data/public/vendor/bootstrap/javascript.html +123 -115
- data/public/vendor/bootstrap/js/dropdown.js +3 -3
- data/public/vendor/bootstrap/js/modal.js +5 -3
- data/public/vendor/bootstrap/js/tests/unit/modal.js +19 -0
- data/public/vendor/bootstrap/js/tests/vendor/jquery.js +6 -5
- data/public/vendor/bootstrap/js/tooltip.js +9 -5
- data/public/vendor/bootstrap/js/transition.js +1 -1
- data/public/vendor/bootstrap/less/alerts.less +7 -11
- data/public/vendor/bootstrap/less/bootstrap.less +17 -21
- data/public/vendor/bootstrap/less/button-groups.less +14 -10
- data/public/vendor/bootstrap/less/buttons.less +3 -2
- data/public/vendor/bootstrap/less/carousel.less +6 -1
- data/public/vendor/bootstrap/less/dropdowns.less +22 -5
- data/public/vendor/bootstrap/less/forms.less +36 -15
- data/public/vendor/bootstrap/less/glyphicons.less +232 -0
- data/public/vendor/bootstrap/less/grid.less +9 -3
- data/public/vendor/bootstrap/less/input-groups.less +1 -1
- data/public/vendor/bootstrap/less/jumbotron.less +16 -5
- data/public/vendor/bootstrap/less/labels.less +9 -5
- data/public/vendor/bootstrap/less/mixins.less +73 -43
- data/public/vendor/bootstrap/less/modals.less +8 -0
- data/public/vendor/bootstrap/less/navbar.less +251 -189
- data/public/vendor/bootstrap/less/navs.less +13 -12
- data/public/vendor/bootstrap/less/pager.less +1 -1
- data/public/vendor/bootstrap/less/pagination.less +20 -9
- data/public/vendor/bootstrap/less/panels.less +28 -8
- data/public/vendor/bootstrap/less/progress-bars.less +6 -10
- data/public/vendor/bootstrap/less/responsive-utilities.less +120 -49
- data/public/vendor/bootstrap/less/scaffolding.less +37 -18
- data/public/vendor/bootstrap/less/tables.less +84 -59
- data/public/vendor/bootstrap/less/theme.less +232 -0
- data/public/vendor/bootstrap/less/thumbnails.less +8 -19
- data/public/vendor/bootstrap/less/type.less +2 -2
- data/public/vendor/bootstrap/less/variables.less +63 -50
- data/public/vendor/bootstrap/package.json +2 -1
- data/public/vendor/components-backbone/LICENSE +22 -0
- data/public/vendor/components-backbone/README.md +10 -0
- data/public/vendor/components-backbone/backbone-min.js +4 -0
- data/public/vendor/components-backbone/backbone.js +1571 -0
- data/public/vendor/components-backbone/bower.json +17 -0
- data/public/vendor/components-backbone/component.json +21 -0
- data/public/vendor/components-backbone/composer.json +37 -0
- data/public/vendor/components-backbone/package.json +24 -0
- data/public/vendor/d3.layout.cloud.js +401 -0
- data/public/vendor/replacejs/CHANGELOG.md +4 -0
- data/public/vendor/replacejs/replace.js +21 -2
- data/public/vendor/underscore/CNAME +1 -0
- data/public/vendor/underscore/CONTRIBUTING.md +9 -0
- data/public/vendor/underscore/LICENSE +22 -0
- data/public/vendor/underscore/README.md +19 -0
- data/public/vendor/underscore/Rakefile +10 -0
- data/public/vendor/underscore/docs/docco.css +192 -0
- data/public/vendor/underscore/docs/favicon.ico +0 -0
- data/public/vendor/underscore/docs/images/background.png +0 -0
- data/public/vendor/underscore/docs/images/underscore.png +0 -0
- data/public/vendor/underscore/docs/underscore.html +823 -0
- data/public/vendor/underscore/favicon.ico +0 -0
- data/public/vendor/underscore/index.html +2467 -0
- data/public/vendor/underscore/index.js +1 -0
- data/public/vendor/underscore/package.json +16 -0
- data/public/vendor/underscore/test/arrays.js +200 -0
- data/public/vendor/underscore/test/chaining.js +59 -0
- data/public/vendor/underscore/test/collections.js +453 -0
- data/public/vendor/underscore/test/functions.js +265 -0
- data/public/vendor/underscore/test/index.html +44 -0
- data/public/vendor/underscore/test/objects.js +570 -0
- data/public/vendor/underscore/test/speed.js +75 -0
- data/public/vendor/underscore/test/utility.js +266 -0
- data/public/vendor/underscore/test/vendor/jquery.js +9404 -0
- data/public/vendor/underscore/test/vendor/jslitmus.js +670 -0
- data/public/vendor/underscore/test/vendor/qunit.css +235 -0
- data/public/vendor/underscore/test/vendor/qunit.js +1977 -0
- data/public/vendor/underscore/test/vendor/runner.js +98 -0
- data/public/vendor/underscore/underscore-min.js +1 -0
- data/public/vendor/underscore/underscore.js +1226 -0
- data/views/dashboard.haml +1 -1
- data/views/layout.haml +8 -4
- metadata +132 -25
- data/public/vendor/bootstrap/_layouts/customize.html +0 -52
- data/public/vendor/bootstrap/assets/js/jquery.bbq.min.js +0 -1287
- data/public/vendor/bootstrap-glyphicons/CHANGELOG.md +0 -3
- data/public/vendor/bootstrap-glyphicons/CNAME +0 -1
- data/public/vendor/bootstrap-glyphicons/CONTRIBUTING.md +0 -54
- data/public/vendor/bootstrap-glyphicons/LICENSE +0 -19
- data/public/vendor/bootstrap-glyphicons/README.md +0 -61
- data/public/vendor/bootstrap-glyphicons/_config.yml +0 -12
- data/public/vendor/bootstrap-glyphicons/composer.json +0 -9
- data/public/vendor/bootstrap-glyphicons/css/bootstrap-glyphicons.css +0 -2
- data/public/vendor/bootstrap-glyphicons/css/bootstrap.css +0 -9
- data/public/vendor/bootstrap-glyphicons/css/docs.css +0 -160
- data/public/vendor/bootstrap-glyphicons/fonts/glyphiconshalflings-regular.eot +0 -0
- data/public/vendor/bootstrap-glyphicons/fonts/glyphiconshalflings-regular.otf +0 -0
- data/public/vendor/bootstrap-glyphicons/fonts/glyphiconshalflings-regular.svg +0 -175
- data/public/vendor/bootstrap-glyphicons/fonts/glyphiconshalflings-regular.ttf +0 -0
- data/public/vendor/bootstrap-glyphicons/fonts/glyphiconshalflings-regular.woff +0 -0
- data/public/vendor/bootstrap-glyphicons/index.html +0 -255
- data/public/vendor/bootstrap-glyphicons/less/bootstrap-glyphicons.less +0 -201
- data/public/vendor/bootstrap-glyphicons/package.json +0 -18
@@ -0,0 +1,823 @@
|
|
1
|
+
<!DOCTYPE html> <html> <head> <title>underscore.js</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.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <pre><code>Underscore.js 1.4.4
|
2
|
+
http://underscorejs.org
|
3
|
+
(c) 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
|
4
|
+
Underscore may be freely distributed under the MIT license.
|
5
|
+
</code></pre> </td> <td class="code"> <div class="highlight"><pre><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <h2>Baseline setup</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</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="kd">var</span> <span class="nx">root</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>Save the previous value of the <code>_</code> variable.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">previousUnderscore</span> <span class="o">=</span> <span class="nx">root</span><span class="p">.</span><span class="nx">_</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>Establish the object that gets returned to break out of a loop iteration.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">breaker</span> <span class="o">=</span> <span class="p">{};</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>Save bytes in the minified (but not gzipped) version:</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">ArrayProto</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">,</span> <span class="nx">ObjProto</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">,</span> <span class="nx">FuncProto</span> <span class="o">=</span> <span class="nb">Function</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>Create quick reference variables for speed access to core prototypes.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">push</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">push</span><span class="p">,</span>
|
6
|
+
<span class="nx">slice</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">slice</span><span class="p">,</span>
|
7
|
+
<span class="nx">concat</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">concat</span><span class="p">,</span>
|
8
|
+
<span class="nx">toString</span> <span class="o">=</span> <span class="nx">ObjProto</span><span class="p">.</span><span class="nx">toString</span><span class="p">,</span>
|
9
|
+
<span class="nx">hasOwnProperty</span> <span class="o">=</span> <span class="nx">ObjProto</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>All <strong>ECMAScript 5</strong> native function implementations that we hope to use
|
10
|
+
are declared here.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span>
|
11
|
+
<span class="nx">nativeForEach</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">forEach</span><span class="p">,</span>
|
12
|
+
<span class="nx">nativeMap</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">map</span><span class="p">,</span>
|
13
|
+
<span class="nx">nativeReduce</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">reduce</span><span class="p">,</span>
|
14
|
+
<span class="nx">nativeReduceRight</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">reduceRight</span><span class="p">,</span>
|
15
|
+
<span class="nx">nativeFilter</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">filter</span><span class="p">,</span>
|
16
|
+
<span class="nx">nativeEvery</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">every</span><span class="p">,</span>
|
17
|
+
<span class="nx">nativeSome</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">some</span><span class="p">,</span>
|
18
|
+
<span class="nx">nativeIndexOf</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">,</span>
|
19
|
+
<span class="nx">nativeLastIndexOf</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">lastIndexOf</span><span class="p">,</span>
|
20
|
+
<span class="nx">nativeIsArray</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">,</span>
|
21
|
+
<span class="nx">nativeKeys</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">,</span>
|
22
|
+
<span class="nx">nativeBind</span> <span class="o">=</span> <span class="nx">FuncProto</span><span class="p">.</span><span class="nx">bind</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</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="kd">var</span> <span class="nx">_</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
23
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">_</span><span class="p">)</span> <span class="k">return</span> <span class="nx">obj</span><span class="p">;</span>
|
24
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="k">this</span> <span class="k">instanceof</span> <span class="nx">_</span><span class="p">))</span> <span class="k">return</span> <span class="k">new</span> <span class="nx">_</span><span class="p">(</span><span class="nx">obj</span><span class="p">);</span>
|
25
|
+
<span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span> <span class="o">=</span> <span class="nx">obj</span><span class="p">;</span>
|
26
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>Export the Underscore object for <strong>Node.js</strong>, with
|
27
|
+
backwards-compatibility for the old <code>require()</code> API. If we're in
|
28
|
+
the browser, add <code>_</code> as a global object via a string identifier,
|
29
|
+
for Closure Compiler "advanced" mode.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">exports</span> <span class="o">!==</span> <span class="s1">'undefined'</span><span class="p">)</span> <span class="p">{</span>
|
30
|
+
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">module</span> <span class="o">!==</span> <span class="s1">'undefined'</span> <span class="o">&&</span> <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span><span class="p">)</span> <span class="p">{</span>
|
31
|
+
<span class="nx">exports</span> <span class="o">=</span> <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">_</span><span class="p">;</span>
|
32
|
+
<span class="p">}</span>
|
33
|
+
<span class="nx">exports</span><span class="p">.</span><span class="nx">_</span> <span class="o">=</span> <span class="nx">_</span><span class="p">;</span>
|
34
|
+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
|
35
|
+
<span class="nx">root</span><span class="p">.</span><span class="nx">_</span> <span class="o">=</span> <span class="nx">_</span><span class="p">;</span>
|
36
|
+
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <p>Current version.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">VERSION</span> <span class="o">=</span> <span class="s1">'1.4.4'</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <h2>Collection Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>The cornerstone, an <code>each</code> implementation, aka <code>forEach</code>.
|
37
|
+
Handles objects with the built-in <code>forEach</code>, arrays, and raw objects.
|
38
|
+
Delegates to <strong>ECMAScript 5</strong>'s native <code>forEach</code> if available.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">each</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">forEach</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
39
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
|
40
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">nativeForEach</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">forEach</span> <span class="o">===</span> <span class="nx">nativeForEach</span><span class="p">)</span> <span class="p">{</span>
|
41
|
+
<span class="nx">obj</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">);</span>
|
42
|
+
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="o">+</span><span class="nx">obj</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
|
43
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
|
44
|
+
<span class="k">if</span> <span class="p">(</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="o">===</span> <span class="nx">breaker</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
|
45
|
+
<span class="p">}</span>
|
46
|
+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
|
47
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
48
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">has</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="p">{</span>
|
49
|
+
<span class="k">if</span> <span class="p">(</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">key</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="o">===</span> <span class="nx">breaker</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
|
50
|
+
<span class="p">}</span>
|
51
|
+
<span class="p">}</span>
|
52
|
+
<span class="p">}</span>
|
53
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <p>Return the results of applying the iterator to each element.
|
54
|
+
Delegates to <strong>ECMAScript 5</strong>'s native <code>map</code> if available.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">map</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">collect</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
55
|
+
<span class="kd">var</span> <span class="nx">results</span> <span class="o">=</span> <span class="p">[];</span>
|
56
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="nx">results</span><span class="p">;</span>
|
57
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">nativeMap</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">map</span> <span class="o">===</span> <span class="nx">nativeMap</span><span class="p">)</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>
|
58
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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>
|
59
|
+
<span class="nx">results</span><span class="p">[</span><span class="nx">results</span><span class="p">.</span><span class="nx">length</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">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">);</span>
|
60
|
+
<span class="p">});</span>
|
61
|
+
<span class="k">return</span> <span class="nx">results</span><span class="p">;</span>
|
62
|
+
<span class="p">};</span>
|
63
|
+
|
64
|
+
<span class="kd">var</span> <span class="nx">reduceError</span> <span class="o">=</span> <span class="s1">'Reduce of empty array with no initial value'</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p><strong>Reduce</strong> builds up a single result from a list of values, aka <code>inject</code>,
|
65
|
+
or <code>foldl</code>. Delegates to <strong>ECMAScript 5</strong>'s native <code>reduce</code> if available.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">reduce</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">foldl</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">inject</span> <span class="o">=</span> <span class="kd">function</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">memo</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
|
66
|
+
<span class="kd">var</span> <span class="nx">initial</span> <span class="o">=</span> <span class="nx">arguments</span><span class="p">.</span><span class="nx">length</span> <span class="o">></span> <span class="mi">2</span><span class="p">;</span>
|
67
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">[];</span>
|
68
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">nativeReduce</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduce</span> <span class="o">===</span> <span class="nx">nativeReduce</span><span class="p">)</span> <span class="p">{</span>
|
69
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">context</span><span class="p">)</span> <span class="nx">iterator</span> <span class="o">=</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>
|
70
|
+
<span class="k">return</span> <span class="nx">initial</span> <span class="o">?</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduce</span><span class="p">(</span><span class="nx">iterator</span><span class="p">,</span> <span class="nx">memo</span><span class="p">)</span> <span class="o">:</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduce</span><span class="p">(</span><span class="nx">iterator</span><span class="p">);</span>
|
71
|
+
<span class="p">}</span>
|
72
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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>
|
73
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">initial</span><span class="p">)</span> <span class="p">{</span>
|
74
|
+
<span class="nx">memo</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span>
|
75
|
+
<span class="nx">initial</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
|
76
|
+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
|
77
|
+
<span class="nx">memo</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">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="p">);</span>
|
78
|
+
<span class="p">}</span>
|
79
|
+
<span class="p">});</span>
|
80
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">initial</span><span class="p">)</span> <span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="nx">reduceError</span><span class="p">);</span>
|
81
|
+
<span class="k">return</span> <span class="nx">memo</span><span class="p">;</span>
|
82
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <p>The right-associative version of reduce, also known as <code>foldr</code>.
|
83
|
+
Delegates to <strong>ECMAScript 5</strong>'s native <code>reduceRight</code> if available.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">reduceRight</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">foldr</span> <span class="o">=</span> <span class="kd">function</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">memo</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
|
84
|
+
<span class="kd">var</span> <span class="nx">initial</span> <span class="o">=</span> <span class="nx">arguments</span><span class="p">.</span><span class="nx">length</span> <span class="o">></span> <span class="mi">2</span><span class="p">;</span>
|
85
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">[];</span>
|
86
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">nativeReduceRight</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduceRight</span> <span class="o">===</span> <span class="nx">nativeReduceRight</span><span class="p">)</span> <span class="p">{</span>
|
87
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">context</span><span class="p">)</span> <span class="nx">iterator</span> <span class="o">=</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>
|
88
|
+
<span class="k">return</span> <span class="nx">initial</span> <span class="o">?</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduceRight</span><span class="p">(</span><span class="nx">iterator</span><span class="p">,</span> <span class="nx">memo</span><span class="p">)</span> <span class="o">:</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduceRight</span><span class="p">(</span><span class="nx">iterator</span><span class="p">);</span>
|
89
|
+
<span class="p">}</span>
|
90
|
+
<span class="kd">var</span> <span class="nx">length</span> <span class="o">=</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
|
91
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">length</span> <span class="o">!==</span> <span class="o">+</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
|
92
|
+
<span class="kd">var</span> <span class="nx">keys</span> <span class="o">=</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>
|
93
|
+
<span class="nx">length</span> <span class="o">=</span> <span class="nx">keys</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
|
94
|
+
<span class="p">}</span>
|
95
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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>
|
96
|
+
<span class="nx">index</span> <span class="o">=</span> <span class="nx">keys</span> <span class="o">?</span> <span class="nx">keys</span><span class="p">[</span><span class="o">--</span><span class="nx">length</span><span class="p">]</span> <span class="o">:</span> <span class="o">--</span><span class="nx">length</span><span class="p">;</span>
|
97
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">initial</span><span class="p">)</span> <span class="p">{</span>
|
98
|
+
<span class="nx">memo</span> <span class="o">=</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">index</span><span class="p">];</span>
|
99
|
+
<span class="nx">initial</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
|
100
|
+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
|
101
|
+
<span class="nx">memo</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">memo</span><span class="p">,</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">index</span><span class="p">],</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">);</span>
|
102
|
+
<span class="p">}</span>
|
103
|
+
<span class="p">});</span>
|
104
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">initial</span><span class="p">)</span> <span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="nx">reduceError</span><span class="p">);</span>
|
105
|
+
<span class="k">return</span> <span class="nx">memo</span><span class="p">;</span>
|
106
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div> <p>Return the first value which passes a truth test. Aliased as <code>detect</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">find</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">detect</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
107
|
+
<span class="kd">var</span> <span class="nx">result</span><span class="p">;</span>
|
108
|
+
<span class="nx">any</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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>
|
109
|
+
<span class="k">if</span> <span class="p">(</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>
|
110
|
+
<span class="nx">result</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span>
|
111
|
+
<span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
|
112
|
+
<span class="p">}</span>
|
113
|
+
<span class="p">});</span>
|
114
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
|
115
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <p>Return all the elements that pass a truth test.
|
116
|
+
Delegates to <strong>ECMAScript 5</strong>'s native <code>filter</code> if available.
|
117
|
+
Aliased as <code>select</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">filter</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">select</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
118
|
+
<span class="kd">var</span> <span class="nx">results</span> <span class="o">=</span> <span class="p">[];</span>
|
119
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="nx">results</span><span class="p">;</span>
|
120
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">nativeFilter</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">filter</span> <span class="o">===</span> <span class="nx">nativeFilter</span><span class="p">)</span> <span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">);</span>
|
121
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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>
|
122
|
+
<span class="k">if</span> <span class="p">(</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">results</span><span class="p">[</span><span class="nx">results</span><span class="p">.</span><span class="nx">length</span><span class="p">]</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span>
|
123
|
+
<span class="p">});</span>
|
124
|
+
<span class="k">return</span> <span class="nx">results</span><span class="p">;</span>
|
125
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div> <p>Return all the elements for which a truth test fails.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">reject</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
126
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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>
|
127
|
+
<span class="k">return</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">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">);</span>
|
128
|
+
<span class="p">},</span> <span class="nx">context</span><span class="p">);</span>
|
129
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</a> </div> <p>Determine whether all of the elements match a truth test.
|
130
|
+
Delegates to <strong>ECMAScript 5</strong>'s native <code>every</code> if available.
|
131
|
+
Aliased as <code>all</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">every</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">all</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
132
|
+
<span class="nx">iterator</span> <span class="o">||</span> <span class="p">(</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="p">);</span>
|
133
|
+
<span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
|
134
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
|
135
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">nativeEvery</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">every</span> <span class="o">===</span> <span class="nx">nativeEvery</span><span class="p">)</span> <span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">every</span><span class="p">(</span><span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">);</span>
|
136
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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>
|
137
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="nx">result</span> <span class="o">=</span> <span class="nx">result</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">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">return</span> <span class="nx">breaker</span><span class="p">;</span>
|
138
|
+
<span class="p">});</span>
|
139
|
+
<span class="k">return</span> <span class="o">!!</span><span class="nx">result</span><span class="p">;</span>
|
140
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">¶</a> </div> <p>Determine if at least one element in the object matches a truth test.
|
141
|
+
Delegates to <strong>ECMAScript 5</strong>'s native <code>some</code> if available.
|
142
|
+
Aliased as <code>any</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">any</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">some</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">any</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
143
|
+
<span class="nx">iterator</span> <span class="o">||</span> <span class="p">(</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="p">);</span>
|
144
|
+
<span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
|
145
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
|
146
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">nativeSome</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">some</span> <span class="o">===</span> <span class="nx">nativeSome</span><span class="p">)</span> <span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">some</span><span class="p">(</span><span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">);</span>
|
147
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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>
|
148
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">result</span> <span class="o">||</span> <span class="p">(</span><span class="nx">result</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">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">return</span> <span class="nx">breaker</span><span class="p">;</span>
|
149
|
+
<span class="p">});</span>
|
150
|
+
<span class="k">return</span> <span class="o">!!</span><span class="nx">result</span><span class="p">;</span>
|
151
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <p>Determine if the array or object contains a given value (using <code>===</code>).
|
152
|
+
Aliased as <code>include</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">contains</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">include</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
153
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
|
154
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">nativeIndexOf</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">indexOf</span> <span class="o">===</span> <span class="nx">nativeIndexOf</span><span class="p">)</span> <span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">target</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
|
155
|
+
<span class="k">return</span> <span class="nx">any</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
|
156
|
+
<span class="k">return</span> <span class="nx">value</span> <span class="o">===</span> <span class="nx">target</span><span class="p">;</span>
|
157
|
+
<span class="p">});</span>
|
158
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</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="nx">_</span><span class="p">.</span><span class="nx">invoke</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
159
|
+
<span class="kd">var</span> <span class="nx">args</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">arguments</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
|
160
|
+
<span class="kd">var</span> <span class="nx">isFunc</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">method</span><span class="p">);</span>
|
161
|
+
<span class="k">return</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="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
|
162
|
+
<span class="k">return</span> <span class="p">(</span><span class="nx">isFunc</span> <span class="o">?</span> <span class="nx">method</span> <span class="o">:</span> <span class="nx">value</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="nx">value</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span>
|
163
|
+
<span class="p">});</span>
|
164
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">¶</a> </div> <p>Convenience version of a common use case of <code>map</code>: fetching a property.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">pluck</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
165
|
+
<span class="k">return</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="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">){</span> <span class="k">return</span> <span class="nx">value</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span> <span class="p">});</span>
|
166
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">¶</a> </div> <p>Convenience version of a common use case of <code>filter</code>: selecting only objects
|
167
|
+
containing specific <code>key:value</code> pairs.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">where</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">attrs</span><span class="p">,</span> <span class="nx">first</span><span class="p">)</span> <span class="p">{</span>
|
168
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">isEmpty</span><span class="p">(</span><span class="nx">attrs</span><span class="p">))</span> <span class="k">return</span> <span class="nx">first</span> <span class="o">?</span> <span class="kc">null</span> <span class="o">:</span> <span class="p">[];</span>
|
169
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">[</span><span class="nx">first</span> <span class="o">?</span> <span class="s1">'find'</span> <span class="o">:</span> <span class="s1">'filter'</span><span class="p">](</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
|
170
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">attrs</span><span class="p">)</span> <span class="p">{</span>
|
171
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">attrs</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">!==</span> <span class="nx">value</span><span class="p">[</span><span class="nx">key</span><span class="p">])</span> <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
|
172
|
+
<span class="p">}</span>
|
173
|
+
<span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
|
174
|
+
<span class="p">});</span>
|
175
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">¶</a> </div> <p>Convenience version of a common use case of <code>find</code>: getting the first object
|
176
|
+
containing specific <code>key:value</code> pairs.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">findWhere</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">attrs</span><span class="p">)</span> <span class="p">{</span>
|
177
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">where</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">attrs</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
|
178
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">¶</a> </div> <p>Return the maximum element or (element-based computation).
|
179
|
+
Can't optimize arrays of integers longer than 65,535 elements.
|
180
|
+
See: https://bugs.webkit.org/show_bug.cgi?id=80797</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">max</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
181
|
+
<span class="k">if</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">isArray</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="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">===</span> <span class="o">+</span><span class="nx">obj</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="o"><</span> <span class="mi">65535</span><span class="p">)</span> <span class="p">{</span>
|
182
|
+
<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>
|
183
|
+
<span class="p">}</span>
|
184
|
+
<span class="k">if</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">isEmpty</span><span class="p">(</span><span class="nx">obj</span><span class="p">))</span> <span class="k">return</span> <span class="o">-</span><span class="kc">Infinity</span><span class="p">;</span>
|
185
|
+
<span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="p">{</span><span class="nx">computed</span> <span class="o">:</span> <span class="o">-</span><span class="kc">Infinity</span><span class="p">,</span> <span class="nx">value</span><span class="o">:</span> <span class="o">-</span><span class="kc">Infinity</span><span class="p">};</span>
|
186
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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>
|
187
|
+
<span class="kd">var</span> <span class="nx">computed</span> <span class="o">=</span> <span class="nx">iterator</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">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">value</span><span class="p">;</span>
|
188
|
+
<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">&&</span> <span class="p">(</span><span class="nx">result</span> <span class="o">=</span> <span class="p">{</span><span class="nx">value</span> <span class="o">:</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">computed</span> <span class="o">:</span> <span class="nx">computed</span><span class="p">});</span>
|
189
|
+
<span class="p">});</span>
|
190
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">.</span><span class="nx">value</span><span class="p">;</span>
|
191
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">¶</a> </div> <p>Return the minimum element (or element-based computation).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">min</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
192
|
+
<span class="k">if</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">isArray</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="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">===</span> <span class="o">+</span><span class="nx">obj</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="o"><</span> <span class="mi">65535</span><span class="p">)</span> <span class="p">{</span>
|
193
|
+
<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>
|
194
|
+
<span class="p">}</span>
|
195
|
+
<span class="k">if</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">isEmpty</span><span class="p">(</span><span class="nx">obj</span><span class="p">))</span> <span class="k">return</span> <span class="kc">Infinity</span><span class="p">;</span>
|
196
|
+
<span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="p">{</span><span class="nx">computed</span> <span class="o">:</span> <span class="kc">Infinity</span><span class="p">,</span> <span class="nx">value</span><span class="o">:</span> <span class="kc">Infinity</span><span class="p">};</span>
|
197
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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>
|
198
|
+
<span class="kd">var</span> <span class="nx">computed</span> <span class="o">=</span> <span class="nx">iterator</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">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">value</span><span class="p">;</span>
|
199
|
+
<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">&&</span> <span class="p">(</span><span class="nx">result</span> <span class="o">=</span> <span class="p">{</span><span class="nx">value</span> <span class="o">:</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">computed</span> <span class="o">:</span> <span class="nx">computed</span><span class="p">});</span>
|
200
|
+
<span class="p">});</span>
|
201
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">.</span><span class="nx">value</span><span class="p">;</span>
|
202
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">¶</a> </div> <p>Shuffle an array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">shuffle</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
203
|
+
<span class="kd">var</span> <span class="nx">rand</span><span class="p">;</span>
|
204
|
+
<span class="kd">var</span> <span class="nx">index</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
|
205
|
+
<span class="kd">var</span> <span class="nx">shuffled</span> <span class="o">=</span> <span class="p">[];</span>
|
206
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
|
207
|
+
<span class="nx">rand</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">random</span><span class="p">(</span><span class="nx">index</span><span class="o">++</span><span class="p">);</span>
|
208
|
+
<span class="nx">shuffled</span><span class="p">[</span><span class="nx">index</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nx">shuffled</span><span class="p">[</span><span class="nx">rand</span><span class="p">];</span>
|
209
|
+
<span class="nx">shuffled</span><span class="p">[</span><span class="nx">rand</span><span class="p">]</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span>
|
210
|
+
<span class="p">});</span>
|
211
|
+
<span class="k">return</span> <span class="nx">shuffled</span><span class="p">;</span>
|
212
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">¶</a> </div> <p>An internal function to generate lookup iterators.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">lookupIterator</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
|
213
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">?</span> <span class="nx">value</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">){</span> <span class="k">return</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">value</span><span class="p">];</span> <span class="p">};</span>
|
214
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">¶</a> </div> <p>Sort the object's values by a criterion produced by an iterator.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">sortBy</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
|
215
|
+
<span class="kd">var</span> <span class="nx">iterator</span> <span class="o">=</span> <span class="nx">lookupIterator</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
|
216
|
+
<span class="k">return</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="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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>
|
217
|
+
<span class="k">return</span> <span class="p">{</span>
|
218
|
+
<span class="nx">value</span> <span class="o">:</span> <span class="nx">value</span><span class="p">,</span>
|
219
|
+
<span class="nx">index</span> <span class="o">:</span> <span class="nx">index</span><span class="p">,</span>
|
220
|
+
<span class="nx">criteria</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">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span>
|
221
|
+
<span class="p">};</span>
|
222
|
+
<span class="p">}).</span><span class="nx">sort</span><span class="p">(</span><span class="kd">function</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="p">{</span>
|
223
|
+
<span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="nx">left</span><span class="p">.</span><span class="nx">criteria</span><span class="p">;</span>
|
224
|
+
<span class="kd">var</span> <span class="nx">b</span> <span class="o">=</span> <span class="nx">right</span><span class="p">.</span><span class="nx">criteria</span><span class="p">;</span>
|
225
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">a</span> <span class="o">!==</span> <span class="nx">b</span><span class="p">)</span> <span class="p">{</span>
|
226
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">a</span> <span class="o">></span> <span class="nx">b</span> <span class="o">||</span> <span class="nx">a</span> <span class="o">===</span> <span class="k">void</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
|
227
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">a</span> <span class="o"><</span> <span class="nx">b</span> <span class="o">||</span> <span class="nx">b</span> <span class="o">===</span> <span class="k">void</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
|
228
|
+
<span class="p">}</span>
|
229
|
+
<span class="k">return</span> <span class="nx">left</span><span class="p">.</span><span class="nx">index</span> <span class="o"><</span> <span class="nx">right</span><span class="p">.</span><span class="nx">index</span> <span class="o">?</span> <span class="o">-</span><span class="mi">1</span> <span class="o">:</span> <span class="mi">1</span><span class="p">;</span>
|
230
|
+
<span class="p">}),</span> <span class="s1">'value'</span><span class="p">);</span>
|
231
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">¶</a> </div> <p>An internal function used for aggregate "group by" operations.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">group</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">behavior</span><span class="p">)</span> <span class="p">{</span>
|
232
|
+
<span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="p">{};</span>
|
233
|
+
<span class="kd">var</span> <span class="nx">iterator</span> <span class="o">=</span> <span class="nx">lookupIterator</span><span class="p">(</span><span class="nx">value</span> <span class="o">||</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span><span class="p">);</span>
|
234
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="kd">function</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="p">{</span>
|
235
|
+
<span class="kd">var</span> <span class="nx">key</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">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">obj</span><span class="p">);</span>
|
236
|
+
<span class="nx">behavior</span><span class="p">(</span><span class="nx">result</span><span class="p">,</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">value</span><span class="p">);</span>
|
237
|
+
<span class="p">});</span>
|
238
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
|
239
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</a> </div> <p>Groups the object's values by a criterion. Pass either a string attribute
|
240
|
+
to group by, or a function that returns the criterion.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">groupBy</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
|
241
|
+
<span class="k">return</span> <span class="nx">group</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">context</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">result</span><span class="p">,</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
|
242
|
+
<span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">result</span><span class="p">,</span> <span class="nx">key</span><span class="p">)</span> <span class="o">?</span> <span class="nx">result</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">:</span> <span class="p">(</span><span class="nx">result</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])).</span><span class="nx">push</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
|
243
|
+
<span class="p">});</span>
|
244
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">¶</a> </div> <p>Counts instances of an object that group by a certain criterion. Pass
|
245
|
+
either a string attribute to count by, or a function that returns the
|
246
|
+
criterion.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">countBy</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
|
247
|
+
<span class="k">return</span> <span class="nx">group</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">context</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">result</span><span class="p">,</span> <span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
|
248
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">result</span><span class="p">,</span> <span class="nx">key</span><span class="p">))</span> <span class="nx">result</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
|
249
|
+
<span class="nx">result</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="o">++</span><span class="p">;</span>
|
250
|
+
<span class="p">});</span>
|
251
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">¶</a> </div> <p>Use a comparator function to figure out the smallest index at which
|
252
|
+
an object should be inserted so as to maintain order. Uses binary search.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">sortedIndex</span> <span class="o">=</span> <span class="kd">function</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="nx">context</span><span class="p">)</span> <span class="p">{</span>
|
253
|
+
<span class="nx">iterator</span> <span class="o">=</span> <span class="nx">iterator</span> <span class="o">==</span> <span class="kc">null</span> <span class="o">?</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span> <span class="o">:</span> <span class="nx">lookupIterator</span><span class="p">(</span><span class="nx">iterator</span><span class="p">);</span>
|
254
|
+
<span class="kd">var</span> <span class="nx">value</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">obj</span><span class="p">);</span>
|
255
|
+
<span class="kd">var</span> <span class="nx">low</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">high</span> <span class="o">=</span> <span class="nx">array</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
|
256
|
+
<span class="k">while</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="p">{</span>
|
257
|
+
<span class="kd">var</span> <span class="nx">mid</span> <span class="o">=</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="p">;</span>
|
258
|
+
<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">array</span><span class="p">[</span><span class="nx">mid</span><span class="p">])</span> <span class="o"><</span> <span class="nx">value</span> <span class="o">?</span> <span class="nx">low</span> <span class="o">=</span> <span class="nx">mid</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">:</span> <span class="nx">high</span> <span class="o">=</span> <span class="nx">mid</span><span class="p">;</span>
|
259
|
+
<span class="p">}</span>
|
260
|
+
<span class="k">return</span> <span class="nx">low</span><span class="p">;</span>
|
261
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">¶</a> </div> <p>Safely convert anything iterable into a real, live array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">toArray</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
262
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">obj</span><span class="p">)</span> <span class="k">return</span> <span class="p">[];</span>
|
263
|
+
<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">obj</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">obj</span><span class="p">);</span>
|
264
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="o">+</span><span class="nx">obj</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="k">return</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="nx">_</span><span class="p">.</span><span class="nx">identity</span><span class="p">);</span>
|
265
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">values</span><span class="p">(</span><span class="nx">obj</span><span class="p">);</span>
|
266
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">¶</a> </div> <p>Return the number of elements in an object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">size</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
267
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
|
268
|
+
<span class="k">return</span> <span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="o">+</span><span class="nx">obj</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="o">?</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="o">:</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="nx">length</span><span class="p">;</span>
|
269
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">¶</a> </div> <h2>Array Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-39">¶</a> </div> <p>Get the first element of an array. Passing <strong>n</strong> will return the first N
|
270
|
+
values in the array. Aliased as <code>head</code> and <code>take</code>. The <strong>guard</strong> check
|
271
|
+
allows it to work with <code>_.map</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">first</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">head</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">take</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
272
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">array</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="k">void</span> <span class="mi">0</span><span class="p">;</span>
|
273
|
+
<span class="k">return</span> <span class="p">(</span><span class="nx">n</span> <span class="o">!=</span> <span class="kc">null</span><span class="p">)</span> <span class="o">&&</span> <span class="o">!</span><span class="nx">guard</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="mi">0</span><span class="p">,</span> <span class="nx">n</span><span class="p">)</span> <span class="o">:</span> <span class="nx">array</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
|
274
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-40">¶</a> </div> <p>Returns everything but the last entry of the array. Especially useful on
|
275
|
+
the arguments object. Passing <strong>n</strong> will return all the values in
|
276
|
+
the array, excluding the last N. The <strong>guard</strong> check allows it to work with
|
277
|
+
<code>_.map</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">initial</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
278
|
+
<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">array</span><span class="p">,</span> <span class="mi">0</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="p">((</span><span class="nx">n</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="o">||</span> <span class="nx">guard</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="nx">n</span><span class="p">));</span>
|
279
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-41">¶</a> </div> <p>Get the last element of an array. Passing <strong>n</strong> will return the last N
|
280
|
+
values in the array. The <strong>guard</strong> check allows it to work with <code>_.map</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">last</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
281
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">array</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="k">void</span> <span class="mi">0</span><span class="p">;</span>
|
282
|
+
<span class="k">if</span> <span class="p">((</span><span class="nx">n</span> <span class="o">!=</span> <span class="kc">null</span><span class="p">)</span> <span class="o">&&</span> <span class="o">!</span><span class="nx">guard</span><span class="p">)</span> <span class="p">{</span>
|
283
|
+
<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">array</span><span class="p">,</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">max</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="nx">n</span><span class="p">,</span> <span class="mi">0</span><span class="p">));</span>
|
284
|
+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
|
285
|
+
<span class="k">return</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>
|
286
|
+
<span class="p">}</span>
|
287
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-42">¶</a> </div> <p>Returns everything but the first entry of the array. Aliased as <code>tail</code> and <code>drop</code>.
|
288
|
+
Especially useful on the arguments object. Passing an <strong>n</strong> will return
|
289
|
+
the rest N values in the array. The <strong>guard</strong>
|
290
|
+
check allows it to work with <code>_.map</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">tail</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">drop</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
291
|
+
<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">array</span><span class="p">,</span> <span class="p">(</span><span class="nx">n</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="o">||</span> <span class="nx">guard</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="nx">n</span><span class="p">);</span>
|
292
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-43"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-43">¶</a> </div> <p>Trim out all falsy values from an array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">compact</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="p">{</span>
|
293
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span><span class="p">);</span>
|
294
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-44"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-44">¶</a> </div> <p>Internal implementation of a recursive <code>flatten</code> function.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">flatten</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">input</span><span class="p">,</span> <span class="nx">shallow</span><span class="p">,</span> <span class="nx">output</span><span class="p">)</span> <span class="p">{</span>
|
295
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">input</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
|
296
|
+
<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">value</span><span class="p">))</span> <span class="p">{</span>
|
297
|
+
<span class="nx">shallow</span> <span class="o">?</span> <span class="nx">push</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">output</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="o">:</span> <span class="nx">flatten</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">shallow</span><span class="p">,</span> <span class="nx">output</span><span class="p">);</span>
|
298
|
+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
|
299
|
+
<span class="nx">output</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
|
300
|
+
<span class="p">}</span>
|
301
|
+
<span class="p">});</span>
|
302
|
+
<span class="k">return</span> <span class="nx">output</span><span class="p">;</span>
|
303
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-45"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-45">¶</a> </div> <p>Return a completely flattened version of an array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">flatten</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">shallow</span><span class="p">)</span> <span class="p">{</span>
|
304
|
+
<span class="k">return</span> <span class="nx">flatten</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">shallow</span><span class="p">,</span> <span class="p">[]);</span>
|
305
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-46"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-46">¶</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="nx">_</span><span class="p">.</span><span class="nx">without</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="p">{</span>
|
306
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">difference</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">));</span>
|
307
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-47"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-47">¶</a> </div> <p>Produce a duplicate-free version of the array. If the array has already
|
308
|
+
been sorted, you have the option of using a faster algorithm.
|
309
|
+
Aliased as <code>unique</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">uniq</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">unique</span> <span class="o">=</span> <span class="kd">function</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="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
|
310
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">isSorted</span><span class="p">))</span> <span class="p">{</span>
|
311
|
+
<span class="nx">context</span> <span class="o">=</span> <span class="nx">iterator</span><span class="p">;</span>
|
312
|
+
<span class="nx">iterator</span> <span class="o">=</span> <span class="nx">isSorted</span><span class="p">;</span>
|
313
|
+
<span class="nx">isSorted</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
|
314
|
+
<span class="p">}</span>
|
315
|
+
<span class="kd">var</span> <span class="nx">initial</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">map</span><span class="p">(</span><span class="nx">array</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">array</span><span class="p">;</span>
|
316
|
+
<span class="kd">var</span> <span class="nx">results</span> <span class="o">=</span> <span class="p">[];</span>
|
317
|
+
<span class="kd">var</span> <span class="nx">seen</span> <span class="o">=</span> <span class="p">[];</span>
|
318
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">initial</span><span class="p">,</span> <span class="kd">function</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="p">{</span>
|
319
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">isSorted</span> <span class="o">?</span> <span class="p">(</span><span class="o">!</span><span class="nx">index</span> <span class="o">||</span> <span class="nx">seen</span><span class="p">[</span><span class="nx">seen</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="o">!==</span> <span class="nx">value</span><span class="p">)</span> <span class="o">:</span> <span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">contains</span><span class="p">(</span><span class="nx">seen</span><span class="p">,</span> <span class="nx">value</span><span class="p">))</span> <span class="p">{</span>
|
320
|
+
<span class="nx">seen</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
|
321
|
+
<span class="nx">results</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">array</span><span class="p">[</span><span class="nx">index</span><span class="p">]);</span>
|
322
|
+
<span class="p">}</span>
|
323
|
+
<span class="p">});</span>
|
324
|
+
<span class="k">return</span> <span class="nx">results</span><span class="p">;</span>
|
325
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-48">¶</a> </div> <p>Produce an array that contains the union: each distinct element from all of
|
326
|
+
the passed-in arrays.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">union</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
327
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">uniq</span><span class="p">(</span><span class="nx">concat</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">ArrayProto</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">));</span>
|
328
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-49"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-49">¶</a> </div> <p>Produce an array that contains every item shared between all the
|
329
|
+
passed-in arrays.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">intersection</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="p">{</span>
|
330
|
+
<span class="kd">var</span> <span class="nx">rest</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">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
|
331
|
+
<span class="k">return</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">uniq</span><span class="p">(</span><span class="nx">array</span><span class="p">),</span> <span class="kd">function</span><span class="p">(</span><span class="nx">item</span><span class="p">)</span> <span class="p">{</span>
|
332
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">every</span><span class="p">(</span><span class="nx">rest</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">other</span><span class="p">)</span> <span class="p">{</span>
|
333
|
+
<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">other</span><span class="p">,</span> <span class="nx">item</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">0</span><span class="p">;</span>
|
334
|
+
<span class="p">});</span>
|
335
|
+
<span class="p">});</span>
|
336
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">¶</a> </div> <p>Take the difference between one array and a number of other arrays.
|
337
|
+
Only the elements present in just the first array will remain.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">difference</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="p">{</span>
|
338
|
+
<span class="kd">var</span> <span class="nx">rest</span> <span class="o">=</span> <span class="nx">concat</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">ArrayProto</span><span class="p">,</span> <span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">));</span>
|
339
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">){</span> <span class="k">return</span> <span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">contains</span><span class="p">(</span><span class="nx">rest</span><span class="p">,</span> <span class="nx">value</span><span class="p">);</span> <span class="p">});</span>
|
340
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">¶</a> </div> <p>Zip together multiple lists into a single array -- elements that share
|
341
|
+
an index go together.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">zip</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
342
|
+
<span class="kd">var</span> <span class="nx">args</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">arguments</span><span class="p">);</span>
|
343
|
+
<span class="kd">var</span> <span class="nx">length</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">max</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">pluck</span><span class="p">(</span><span class="nx">args</span><span class="p">,</span> <span class="s1">'length'</span><span class="p">));</span>
|
344
|
+
<span class="kd">var</span> <span class="nx">results</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Array</span><span class="p">(</span><span class="nx">length</span><span class="p">);</span>
|
345
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
|
346
|
+
<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="p">(</span><span class="nx">args</span><span class="p">,</span> <span class="s2">""</span> <span class="o">+</span> <span class="nx">i</span><span class="p">);</span>
|
347
|
+
<span class="p">}</span>
|
348
|
+
<span class="k">return</span> <span class="nx">results</span><span class="p">;</span>
|
349
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-52">¶</a> </div> <p>Converts lists into objects. Pass either a single array of <code>[key, value]</code>
|
350
|
+
pairs, or two parallel arrays of the same length -- one of keys, and one of
|
351
|
+
the corresponding values.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">object</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">values</span><span class="p">)</span> <span class="p">{</span>
|
352
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">list</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="p">{};</span>
|
353
|
+
<span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="p">{};</span>
|
354
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">list</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
|
355
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">values</span><span class="p">)</span> <span class="p">{</span>
|
356
|
+
<span class="nx">result</span><span class="p">[</span><span class="nx">list</span><span class="p">[</span><span class="nx">i</span><span class="p">]]</span> <span class="o">=</span> <span class="nx">values</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
|
357
|
+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
|
358
|
+
<span class="nx">result</span><span class="p">[</span><span class="nx">list</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="nx">list</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="mi">1</span><span class="p">];</span>
|
359
|
+
<span class="p">}</span>
|
360
|
+
<span class="p">}</span>
|
361
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
|
362
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-53">¶</a> </div> <p>If the browser doesn't supply us with indexOf (I'm looking at you, <strong>MSIE</strong>),
|
363
|
+
we need this function. Return the position of the first occurrence of an
|
364
|
+
item in an array, or -1 if the item is not included in the array.
|
365
|
+
Delegates to <strong>ECMAScript 5</strong>'s native <code>indexOf</code> if available.
|
366
|
+
If the array is large and already in sort order, pass <code>true</code>
|
367
|
+
for <strong>isSorted</strong> to use binary search.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">indexOf</span> <span class="o">=</span> <span class="kd">function</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="nx">isSorted</span><span class="p">)</span> <span class="p">{</span>
|
368
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">array</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
|
369
|
+
<span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">array</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
|
370
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">isSorted</span><span class="p">)</span> <span class="p">{</span>
|
371
|
+
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">isSorted</span> <span class="o">==</span> <span class="s1">'number'</span><span class="p">)</span> <span class="p">{</span>
|
372
|
+
<span class="nx">i</span> <span class="o">=</span> <span class="p">(</span><span class="nx">isSorted</span> <span class="o"><</span> <span class="mi">0</span> <span class="o">?</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">+</span> <span class="nx">isSorted</span><span class="p">)</span> <span class="o">:</span> <span class="nx">isSorted</span><span class="p">);</span>
|
373
|
+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
|
374
|
+
<span class="nx">i</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">sortedIndex</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">item</span><span class="p">);</span>
|
375
|
+
<span class="k">return</span> <span class="nx">array</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">===</span> <span class="nx">item</span> <span class="o">?</span> <span class="nx">i</span> <span class="o">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
|
376
|
+
<span class="p">}</span>
|
377
|
+
<span class="p">}</span>
|
378
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">nativeIndexOf</span> <span class="o">&&</span> <span class="nx">array</span><span class="p">.</span><span class="nx">indexOf</span> <span class="o">===</span> <span class="nx">nativeIndexOf</span><span class="p">)</span> <span class="k">return</span> <span class="nx">array</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">item</span><span class="p">,</span> <span class="nx">isSorted</span><span class="p">);</span>
|
379
|
+
<span class="k">for</span> <span class="p">(;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="k">if</span> <span class="p">(</span><span class="nx">array</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">===</span> <span class="nx">item</span><span class="p">)</span> <span class="k">return</span> <span class="nx">i</span><span class="p">;</span>
|
380
|
+
<span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
|
381
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-54">¶</a> </div> <p>Delegates to <strong>ECMAScript 5</strong>'s native <code>lastIndexOf</code> if available.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">lastIndexOf</span> <span class="o">=</span> <span class="kd">function</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="nx">from</span><span class="p">)</span> <span class="p">{</span>
|
382
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">array</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
|
383
|
+
<span class="kd">var</span> <span class="nx">hasIndex</span> <span class="o">=</span> <span class="nx">from</span> <span class="o">!=</span> <span class="kc">null</span><span class="p">;</span>
|
384
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">nativeLastIndexOf</span> <span class="o">&&</span> <span class="nx">array</span><span class="p">.</span><span class="nx">lastIndexOf</span> <span class="o">===</span> <span class="nx">nativeLastIndexOf</span><span class="p">)</span> <span class="p">{</span>
|
385
|
+
<span class="k">return</span> <span class="nx">hasIndex</span> <span class="o">?</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="nx">from</span><span class="p">)</span> <span class="o">:</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>
|
386
|
+
<span class="p">}</span>
|
387
|
+
<span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="p">(</span><span class="nx">hasIndex</span> <span class="o">?</span> <span class="nx">from</span> <span class="o">:</span> <span class="nx">array</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span>
|
388
|
+
<span class="k">while</span> <span class="p">(</span><span class="nx">i</span><span class="o">--</span><span class="p">)</span> <span class="k">if</span> <span class="p">(</span><span class="nx">array</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">===</span> <span class="nx">item</span><span class="p">)</span> <span class="k">return</span> <span class="nx">i</span><span class="p">;</span>
|
389
|
+
<span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
|
390
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-55">¶</a> </div> <p>Generate an integer Array containing an arithmetic progression. A port of
|
391
|
+
the native Python <code>range()</code> function. See
|
392
|
+
<a href="http://docs.python.org/library/functions.html#range">the Python documentation</a>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">range</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
393
|
+
<span class="k">if</span> <span class="p">(</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="p">)</span> <span class="p">{</span>
|
394
|
+
<span class="nx">stop</span> <span class="o">=</span> <span class="nx">start</span> <span class="o">||</span> <span class="mi">0</span><span class="p">;</span>
|
395
|
+
<span class="nx">start</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
|
396
|
+
<span class="p">}</span>
|
397
|
+
<span class="nx">step</span> <span class="o">=</span> <span class="nx">arguments</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">||</span> <span class="mi">1</span><span class="p">;</span>
|
398
|
+
|
399
|
+
<span class="kd">var</span> <span class="nx">len</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">max</span><span class="p">(</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="mi">0</span><span class="p">);</span>
|
400
|
+
<span class="kd">var</span> <span class="nx">idx</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
|
401
|
+
<span class="kd">var</span> <span class="nx">range</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Array</span><span class="p">(</span><span class="nx">len</span><span class="p">);</span>
|
402
|
+
|
403
|
+
<span class="k">while</span><span class="p">(</span><span class="nx">idx</span> <span class="o"><</span> <span class="nx">len</span><span class="p">)</span> <span class="p">{</span>
|
404
|
+
<span class="nx">range</span><span class="p">[</span><span class="nx">idx</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="nx">start</span><span class="p">;</span>
|
405
|
+
<span class="nx">start</span> <span class="o">+=</span> <span class="nx">step</span><span class="p">;</span>
|
406
|
+
<span class="p">}</span>
|
407
|
+
|
408
|
+
<span class="k">return</span> <span class="nx">range</span><span class="p">;</span>
|
409
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-56">¶</a> </div> <h2>Function (ahem) Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-57">¶</a> </div> <p>Create a function bound to a given object (assigning <code>this</code>, and arguments,
|
410
|
+
optionally). Delegates to <strong>ECMAScript 5</strong>'s native <code>Function.bind</code> if
|
411
|
+
available.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">bind</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
|
412
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">func</span><span class="p">.</span><span class="nx">bind</span> <span class="o">===</span> <span class="nx">nativeBind</span> <span class="o">&&</span> <span class="nx">nativeBind</span><span class="p">)</span> <span class="k">return</span> <span class="nx">nativeBind</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">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">));</span>
|
413
|
+
<span class="kd">var</span> <span class="nx">args</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">arguments</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
|
414
|
+
<span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
415
|
+
<span class="k">return</span> <span class="nx">func</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">args</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">)));</span>
|
416
|
+
<span class="p">};</span>
|
417
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-58">¶</a> </div> <p>Partially apply a function by creating a version that has had some of its
|
418
|
+
arguments pre-filled, without changing its dynamic <code>this</code> context.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">partial</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">func</span><span class="p">)</span> <span class="p">{</span>
|
419
|
+
<span class="kd">var</span> <span class="nx">args</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">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
|
420
|
+
<span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
421
|
+
<span class="k">return</span> <span class="nx">func</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">concat</span><span class="p">(</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">)));</span>
|
422
|
+
<span class="p">};</span>
|
423
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">¶</a> </div> <p>Bind all of an object's methods to that object. Useful for ensuring that
|
424
|
+
all callbacks defined on an object belong to it.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">bindAll</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
425
|
+
<span class="kd">var</span> <span class="nx">funcs</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">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
|
426
|
+
<span class="k">if</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">0</span><span class="p">)</span> <span class="nx">funcs</span> <span class="o">=</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>
|
427
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">funcs</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span> <span class="p">{</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="p">(</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="p">);</span> <span class="p">});</span>
|
428
|
+
<span class="k">return</span> <span class="nx">obj</span><span class="p">;</span>
|
429
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-60"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-60">¶</a> </div> <p>Memoize an expensive function by storing its results.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">memoize</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">hasher</span><span class="p">)</span> <span class="p">{</span>
|
430
|
+
<span class="kd">var</span> <span class="nx">memo</span> <span class="o">=</span> <span class="p">{};</span>
|
431
|
+
<span class="nx">hasher</span> <span class="o">||</span> <span class="p">(</span><span class="nx">hasher</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span><span class="p">);</span>
|
432
|
+
<span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
433
|
+
<span class="kd">var</span> <span class="nx">key</span> <span class="o">=</span> <span class="nx">hasher</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">arguments</span><span class="p">);</span>
|
434
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">memo</span><span class="p">,</span> <span class="nx">key</span><span class="p">)</span> <span class="o">?</span> <span class="nx">memo</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">:</span> <span class="p">(</span><span class="nx">memo</span><span class="p">[</span><span class="nx">key</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="k">this</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">));</span>
|
435
|
+
<span class="p">};</span>
|
436
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">¶</a> </div> <p>Delays a function for the given number of milliseconds, and then calls
|
437
|
+
it with the arguments supplied.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">delay</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
438
|
+
<span class="kd">var</span> <span class="nx">args</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">arguments</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
|
439
|
+
<span class="k">return</span> <span class="nx">setTimeout</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span> <span class="k">return</span> <span class="nx">func</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span> <span class="p">},</span> <span class="nx">wait</span><span class="p">);</span>
|
440
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">¶</a> </div> <p>Defers a function, scheduling it to run after the current call stack has
|
441
|
+
cleared.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">defer</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">func</span><span class="p">)</span> <span class="p">{</span>
|
442
|
+
<span class="k">return</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="p">(</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="p">(</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">)));</span>
|
443
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-63">¶</a> </div> <p>Returns a function, that, when invoked, will only be triggered at most once
|
444
|
+
during a given window of time.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">throttle</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
445
|
+
<span class="kd">var</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">args</span><span class="p">,</span> <span class="nx">timeout</span><span class="p">,</span> <span class="nx">result</span><span class="p">;</span>
|
446
|
+
<span class="kd">var</span> <span class="nx">previous</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
|
447
|
+
<span class="kd">var</span> <span class="nx">later</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
448
|
+
<span class="nx">previous</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">;</span>
|
449
|
+
<span class="nx">timeout</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
|
450
|
+
<span class="nx">result</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">context</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span>
|
451
|
+
<span class="p">};</span>
|
452
|
+
<span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
453
|
+
<span class="kd">var</span> <span class="nx">now</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">;</span>
|
454
|
+
<span class="kd">var</span> <span class="nx">remaining</span> <span class="o">=</span> <span class="nx">wait</span> <span class="o">-</span> <span class="p">(</span><span class="nx">now</span> <span class="o">-</span> <span class="nx">previous</span><span class="p">);</span>
|
455
|
+
<span class="nx">context</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
|
456
|
+
<span class="nx">args</span> <span class="o">=</span> <span class="nx">arguments</span><span class="p">;</span>
|
457
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">remaining</span> <span class="o"><=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
|
458
|
+
<span class="nx">clearTimeout</span><span class="p">(</span><span class="nx">timeout</span><span class="p">);</span>
|
459
|
+
<span class="nx">timeout</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
|
460
|
+
<span class="nx">previous</span> <span class="o">=</span> <span class="nx">now</span><span class="p">;</span>
|
461
|
+
<span class="nx">result</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">context</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span>
|
462
|
+
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">timeout</span><span class="p">)</span> <span class="p">{</span>
|
463
|
+
<span class="nx">timeout</span> <span class="o">=</span> <span class="nx">setTimeout</span><span class="p">(</span><span class="nx">later</span><span class="p">,</span> <span class="nx">remaining</span><span class="p">);</span>
|
464
|
+
<span class="p">}</span>
|
465
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
|
466
|
+
<span class="p">};</span>
|
467
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-64">¶</a> </div> <p>Returns a function, that, as long as it continues to be invoked, will not
|
468
|
+
be triggered. The function will be called after it stops being called for
|
469
|
+
N milliseconds. If <code>immediate</code> is passed, trigger the function on the
|
470
|
+
leading edge, instead of the trailing.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">debounce</span> <span class="o">=</span> <span class="kd">function</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="nx">immediate</span><span class="p">)</span> <span class="p">{</span>
|
471
|
+
<span class="kd">var</span> <span class="nx">timeout</span><span class="p">,</span> <span class="nx">result</span><span class="p">;</span>
|
472
|
+
<span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
473
|
+
<span class="kd">var</span> <span class="nx">context</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span> <span class="nx">args</span> <span class="o">=</span> <span class="nx">arguments</span><span class="p">;</span>
|
474
|
+
<span class="kd">var</span> <span class="nx">later</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
475
|
+
<span class="nx">timeout</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
|
476
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">immediate</span><span class="p">)</span> <span class="nx">result</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">context</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span>
|
477
|
+
<span class="p">};</span>
|
478
|
+
<span class="kd">var</span> <span class="nx">callNow</span> <span class="o">=</span> <span class="nx">immediate</span> <span class="o">&&</span> <span class="o">!</span><span class="nx">timeout</span><span class="p">;</span>
|
479
|
+
<span class="nx">clearTimeout</span><span class="p">(</span><span class="nx">timeout</span><span class="p">);</span>
|
480
|
+
<span class="nx">timeout</span> <span class="o">=</span> <span class="nx">setTimeout</span><span class="p">(</span><span class="nx">later</span><span class="p">,</span> <span class="nx">wait</span><span class="p">);</span>
|
481
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">callNow</span><span class="p">)</span> <span class="nx">result</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">context</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span>
|
482
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
|
483
|
+
<span class="p">};</span>
|
484
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">¶</a> </div> <p>Returns a function that will be executed at most one time, no matter how
|
485
|
+
often you call it. Useful for lazy initialization.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">once</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">func</span><span class="p">)</span> <span class="p">{</span>
|
486
|
+
<span class="kd">var</span> <span class="nx">ran</span> <span class="o">=</span> <span class="kc">false</span><span class="p">,</span> <span class="nx">memo</span><span class="p">;</span>
|
487
|
+
<span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
488
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">ran</span><span class="p">)</span> <span class="k">return</span> <span class="nx">memo</span><span class="p">;</span>
|
489
|
+
<span class="nx">ran</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
|
490
|
+
<span class="nx">memo</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="k">this</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span>
|
491
|
+
<span class="nx">func</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
|
492
|
+
<span class="k">return</span> <span class="nx">memo</span><span class="p">;</span>
|
493
|
+
<span class="p">};</span>
|
494
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-66">¶</a> </div> <p>Returns the first function passed as an argument to the second,
|
495
|
+
allowing you to adjust arguments, run code before and after, and
|
496
|
+
conditionally execute the original function.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">wrap</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
497
|
+
<span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
498
|
+
<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="p">[</span><span class="nx">func</span><span class="p">];</span>
|
499
|
+
<span class="nx">push</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">args</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span>
|
500
|
+
<span class="k">return</span> <span class="nx">wrapper</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>
|
501
|
+
<span class="p">};</span>
|
502
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-67">¶</a> </div> <p>Returns a function that is the composition of a list of functions, each
|
503
|
+
consuming the return value of the function that follows.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">compose</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
504
|
+
<span class="kd">var</span> <span class="nx">funcs</span> <span class="o">=</span> <span class="nx">arguments</span><span class="p">;</span>
|
505
|
+
<span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
506
|
+
<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nx">arguments</span><span class="p">;</span>
|
507
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</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="nx">i</span> <span class="o">>=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span>
|
508
|
+
<span class="nx">args</span> <span class="o">=</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>
|
509
|
+
<span class="p">}</span>
|
510
|
+
<span class="k">return</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
|
511
|
+
<span class="p">};</span>
|
512
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-68">¶</a> </div> <p>Returns a function that will only be executed after being called N times.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">after</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">times</span><span class="p">,</span> <span class="nx">func</span><span class="p">)</span> <span class="p">{</span>
|
513
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">times</span> <span class="o"><=</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="nx">func</span><span class="p">();</span>
|
514
|
+
<span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
515
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">--</span><span class="nx">times</span> <span class="o"><</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
|
516
|
+
<span class="k">return</span> <span class="nx">func</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">arguments</span><span class="p">);</span>
|
517
|
+
<span class="p">}</span>
|
518
|
+
<span class="p">};</span>
|
519
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-69">¶</a> </div> <h2>Object Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-70">¶</a> </div> <p>Retrieve the names of an object's properties.
|
520
|
+
Delegates to <strong>ECMAScript 5</strong>'s native <code>Object.keys</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">keys</span> <span class="o">=</span> <span class="nx">nativeKeys</span> <span class="o">||</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
521
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">!==</span> <span class="nb">Object</span><span class="p">(</span><span class="nx">obj</span><span class="p">))</span> <span class="k">throw</span> <span class="k">new</span> <span class="nx">TypeError</span><span class="p">(</span><span class="s1">'Invalid object'</span><span class="p">);</span>
|
522
|
+
<span class="kd">var</span> <span class="nx">keys</span> <span class="o">=</span> <span class="p">[];</span>
|
523
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">obj</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">has</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">keys</span><span class="p">[</span><span class="nx">keys</span><span class="p">.</span><span class="nx">length</span><span class="p">]</span> <span class="o">=</span> <span class="nx">key</span><span class="p">;</span>
|
524
|
+
<span class="k">return</span> <span class="nx">keys</span><span class="p">;</span>
|
525
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-71">¶</a> </div> <p>Retrieve the values of an object's properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">values</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
526
|
+
<span class="kd">var</span> <span class="nx">values</span> <span class="o">=</span> <span class="p">[];</span>
|
527
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">obj</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">has</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">values</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">obj</span><span class="p">[</span><span class="nx">key</span><span class="p">]);</span>
|
528
|
+
<span class="k">return</span> <span class="nx">values</span><span class="p">;</span>
|
529
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-72"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-72">¶</a> </div> <p>Convert an object into a list of <code>[key, value]</code> pairs.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">pairs</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
530
|
+
<span class="kd">var</span> <span class="nx">pairs</span> <span class="o">=</span> <span class="p">[];</span>
|
531
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">obj</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">has</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">pairs</span><span class="p">.</span><span class="nx">push</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="nx">key</span><span class="p">]]);</span>
|
532
|
+
<span class="k">return</span> <span class="nx">pairs</span><span class="p">;</span>
|
533
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-73"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-73">¶</a> </div> <p>Invert the keys and values of an object. The values must be serializable.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">invert</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
534
|
+
<span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="p">{};</span>
|
535
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">obj</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">has</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">result</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">key</span><span class="p">;</span>
|
536
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
|
537
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-74"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-74">¶</a> </div> <p>Return a sorted list of the function names available on the object.
|
538
|
+
Aliased as <code>methods</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">functions</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">methods</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
539
|
+
<span class="kd">var</span> <span class="nx">names</span> <span class="o">=</span> <span class="p">[];</span>
|
540
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
541
|
+
<span class="k">if</span> <span class="p">(</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">names</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">key</span><span class="p">);</span>
|
542
|
+
<span class="p">}</span>
|
543
|
+
<span class="k">return</span> <span class="nx">names</span><span class="p">.</span><span class="nx">sort</span><span class="p">();</span>
|
544
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-75"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-75">¶</a> </div> <p>Extend a given object with all the properties in passed-in object(s).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">extend</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
545
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="kd">function</span><span class="p">(</span><span class="nx">source</span><span class="p">)</span> <span class="p">{</span>
|
546
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">source</span><span class="p">)</span> <span class="p">{</span>
|
547
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">source</span><span class="p">)</span> <span class="p">{</span>
|
548
|
+
<span class="nx">obj</span><span class="p">[</span><span class="nx">prop</span><span class="p">]</span> <span class="o">=</span> <span class="nx">source</span><span class="p">[</span><span class="nx">prop</span><span class="p">];</span>
|
549
|
+
<span class="p">}</span>
|
550
|
+
<span class="p">}</span>
|
551
|
+
<span class="p">});</span>
|
552
|
+
<span class="k">return</span> <span class="nx">obj</span><span class="p">;</span>
|
553
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-76"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-76">¶</a> </div> <p>Return a copy of the object only containing the whitelisted properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">pick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
554
|
+
<span class="kd">var</span> <span class="nx">copy</span> <span class="o">=</span> <span class="p">{};</span>
|
555
|
+
<span class="kd">var</span> <span class="nx">keys</span> <span class="o">=</span> <span class="nx">concat</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">ArrayProto</span><span class="p">,</span> <span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">));</span>
|
556
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">keys</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
|
557
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">key</span> <span class="k">in</span> <span class="nx">obj</span><span class="p">)</span> <span class="nx">copy</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
|
558
|
+
<span class="p">});</span>
|
559
|
+
<span class="k">return</span> <span class="nx">copy</span><span class="p">;</span>
|
560
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-77"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-77">¶</a> </div> <p>Return a copy of the object without the blacklisted properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">omit</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
561
|
+
<span class="kd">var</span> <span class="nx">copy</span> <span class="o">=</span> <span class="p">{};</span>
|
562
|
+
<span class="kd">var</span> <span class="nx">keys</span> <span class="o">=</span> <span class="nx">concat</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">ArrayProto</span><span class="p">,</span> <span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">));</span>
|
563
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
564
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">contains</span><span class="p">(</span><span class="nx">keys</span><span class="p">,</span> <span class="nx">key</span><span class="p">))</span> <span class="nx">copy</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
|
565
|
+
<span class="p">}</span>
|
566
|
+
<span class="k">return</span> <span class="nx">copy</span><span class="p">;</span>
|
567
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-78"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-78">¶</a> </div> <p>Fill in a given object with default properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">defaults</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
568
|
+
<span class="nx">each</span><span class="p">(</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="kd">function</span><span class="p">(</span><span class="nx">source</span><span class="p">)</span> <span class="p">{</span>
|
569
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">source</span><span class="p">)</span> <span class="p">{</span>
|
570
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">source</span><span class="p">)</span> <span class="p">{</span>
|
571
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span><span class="p">[</span><span class="nx">prop</span><span class="p">]</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">prop</span><span class="p">]</span> <span class="o">=</span> <span class="nx">source</span><span class="p">[</span><span class="nx">prop</span><span class="p">];</span>
|
572
|
+
<span class="p">}</span>
|
573
|
+
<span class="p">}</span>
|
574
|
+
<span class="p">});</span>
|
575
|
+
<span class="k">return</span> <span class="nx">obj</span><span class="p">;</span>
|
576
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-79"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-79">¶</a> </div> <p>Create a (shallow-cloned) duplicate of an object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">clone</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
577
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">isObject</span><span class="p">(</span><span class="nx">obj</span><span class="p">))</span> <span class="k">return</span> <span class="nx">obj</span><span class="p">;</span>
|
578
|
+
<span class="k">return</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="o">?</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">slice</span><span class="p">()</span> <span class="o">:</span> <span class="nx">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">({},</span> <span class="nx">obj</span><span class="p">);</span>
|
579
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-80"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-80">¶</a> </div> <p>Invokes interceptor with the obj, and then returns obj.
|
580
|
+
The primary purpose of this method is to "tap into" a method chain, in
|
581
|
+
order to perform operations on intermediate results within the chain.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">tap</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
582
|
+
<span class="nx">interceptor</span><span class="p">(</span><span class="nx">obj</span><span class="p">);</span>
|
583
|
+
<span class="k">return</span> <span class="nx">obj</span><span class="p">;</span>
|
584
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-81"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-81">¶</a> </div> <p>Internal recursive comparison function for <code>isEqual</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">eq</span> <span class="o">=</span> <span class="kd">function</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="nx">aStack</span><span class="p">,</span> <span class="nx">bStack</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-82"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-82">¶</a> </div> <p>Identical objects are equal. <code>0 === -0</code>, but they aren't identical.
|
585
|
+
See the Harmony <code>egal</code> proposal: http://wiki.ecmascript.org/doku.php?id=harmony:egal.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">a</span> <span class="o">===</span> <span class="nx">b</span><span class="p">)</span> <span class="k">return</span> <span class="nx">a</span> <span class="o">!==</span> <span class="mi">0</span> <span class="o">||</span> <span class="mi">1</span> <span class="o">/</span> <span class="nx">a</span> <span class="o">==</span> <span class="mi">1</span> <span class="o">/</span> <span class="nx">b</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-83"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-83">¶</a> </div> <p>A strict comparison is necessary because <code>null == undefined</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">a</span> <span class="o">==</span> <span class="kc">null</span> <span class="o">||</span> <span class="nx">b</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="nx">a</span> <span class="o">===</span> <span class="nx">b</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-84"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-84">¶</a> </div> <p>Unwrap any wrapped objects.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">a</span> <span class="k">instanceof</span> <span class="nx">_</span><span class="p">)</span> <span class="nx">a</span> <span class="o">=</span> <span class="nx">a</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">;</span>
|
586
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">b</span> <span class="k">instanceof</span> <span class="nx">_</span><span class="p">)</span> <span class="nx">b</span> <span class="o">=</span> <span class="nx">b</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-85"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-85">¶</a> </div> <p>Compare <code>[[Class]]</code> names.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">className</span> <span class="o">=</span> <span class="nx">toString</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span>
|
587
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">className</span> <span class="o">!=</span> <span class="nx">toString</span><span class="p">.</span><span class="nx">call</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="p">;</span>
|
588
|
+
<span class="k">switch</span> <span class="p">(</span><span class="nx">className</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-86"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-86">¶</a> </div> <p>Strings, numbers, dates, and booleans are compared by value.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">case</span> <span class="s1">'[object String]'</span><span class="o">:</span></pre></div> </td> </tr> <tr id="section-87"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-87">¶</a> </div> <p>Primitives and their corresponding object wrappers are equivalent; thus, <code>"5"</code> is
|
589
|
+
equivalent to <code>new String("5")</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">a</span> <span class="o">==</span> <span class="nb">String</span><span class="p">(</span><span class="nx">b</span><span class="p">);</span>
|
590
|
+
<span class="k">case</span> <span class="s1">'[object Number]'</span><span class="o">:</span></pre></div> </td> </tr> <tr id="section-88"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-88">¶</a> </div> <p><code>NaN</code>s are equivalent, but non-reflexive. An <code>egal</code> comparison is performed for
|
591
|
+
other numeric values.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">a</span> <span class="o">!=</span> <span class="o">+</span><span class="nx">a</span> <span class="o">?</span> <span class="nx">b</span> <span class="o">!=</span> <span class="o">+</span><span class="nx">b</span> <span class="o">:</span> <span class="p">(</span><span class="nx">a</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">/</span> <span class="nx">a</span> <span class="o">==</span> <span class="mi">1</span> <span class="o">/</span> <span class="nx">b</span> <span class="o">:</span> <span class="nx">a</span> <span class="o">==</span> <span class="o">+</span><span class="nx">b</span><span class="p">);</span>
|
592
|
+
<span class="k">case</span> <span class="s1">'[object Date]'</span><span class="o">:</span>
|
593
|
+
<span class="k">case</span> <span class="s1">'[object Boolean]'</span><span class="o">:</span></pre></div> </td> </tr> <tr id="section-89"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-89">¶</a> </div> <p>Coerce dates and booleans to numeric primitive values. Dates are compared by their
|
594
|
+
millisecond representations. Note that invalid dates with millisecond representations
|
595
|
+
of <code>NaN</code> are not equivalent.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="o">+</span><span class="nx">a</span> <span class="o">==</span> <span class="o">+</span><span class="nx">b</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-90"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-90">¶</a> </div> <p>RegExps are compared by their source patterns and flags.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">case</span> <span class="s1">'[object RegExp]'</span><span class="o">:</span>
|
596
|
+
<span class="k">return</span> <span class="nx">a</span><span class="p">.</span><span class="nx">source</span> <span class="o">==</span> <span class="nx">b</span><span class="p">.</span><span class="nx">source</span> <span class="o">&&</span>
|
597
|
+
<span class="nx">a</span><span class="p">.</span><span class="nx">global</span> <span class="o">==</span> <span class="nx">b</span><span class="p">.</span><span class="nx">global</span> <span class="o">&&</span>
|
598
|
+
<span class="nx">a</span><span class="p">.</span><span class="nx">multiline</span> <span class="o">==</span> <span class="nx">b</span><span class="p">.</span><span class="nx">multiline</span> <span class="o">&&</span>
|
599
|
+
<span class="nx">a</span><span class="p">.</span><span class="nx">ignoreCase</span> <span class="o">==</span> <span class="nx">b</span><span class="p">.</span><span class="nx">ignoreCase</span><span class="p">;</span>
|
600
|
+
<span class="p">}</span>
|
601
|
+
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">a</span> <span class="o">!=</span> <span class="s1">'object'</span> <span class="o">||</span> <span class="k">typeof</span> <span class="nx">b</span> <span class="o">!=</span> <span class="s1">'object'</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-91"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-91">¶</a> </div> <p>Assume equality for cyclic structures. The algorithm for detecting cyclic
|
602
|
+
structures is adapted from ES 5.1 section 15.12.3, abstract operation <code>JO</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">length</span> <span class="o">=</span> <span class="nx">aStack</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
|
603
|
+
<span class="k">while</span> <span class="p">(</span><span class="nx">length</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-92"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-92">¶</a> </div> <p>Linear search. Performance is inversely proportional to the number of
|
604
|
+
unique nested structures.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">aStack</span><span class="p">[</span><span class="nx">length</span><span class="p">]</span> <span class="o">==</span> <span class="nx">a</span><span class="p">)</span> <span class="k">return</span> <span class="nx">bStack</span><span class="p">[</span><span class="nx">length</span><span class="p">]</span> <span class="o">==</span> <span class="nx">b</span><span class="p">;</span>
|
605
|
+
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-93"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-93">¶</a> </div> <p>Add the first object to the stack of traversed objects.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">aStack</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span>
|
606
|
+
<span class="nx">bStack</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">b</span><span class="p">);</span>
|
607
|
+
<span class="kd">var</span> <span class="nx">size</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">result</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-94"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-94">¶</a> </div> <p>Recursively compare objects and arrays.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">className</span> <span class="o">==</span> <span class="s1">'[object Array]'</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-95"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-95">¶</a> </div> <p>Compare array lengths to determine if a deep comparison is necessary.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">size</span> <span class="o">=</span> <span class="nx">a</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
|
608
|
+
<span class="nx">result</span> <span class="o">=</span> <span class="nx">size</span> <span class="o">==</span> <span class="nx">b</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
|
609
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">result</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-96"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-96">¶</a> </div> <p>Deep compare the contents, ignoring non-numeric properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">while</span> <span class="p">(</span><span class="nx">size</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span>
|
610
|
+
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="nx">result</span> <span class="o">=</span> <span class="nx">eq</span><span class="p">(</span><span class="nx">a</span><span class="p">[</span><span class="nx">size</span><span class="p">],</span> <span class="nx">b</span><span class="p">[</span><span class="nx">size</span><span class="p">],</span> <span class="nx">aStack</span><span class="p">,</span> <span class="nx">bStack</span><span class="p">)))</span> <span class="k">break</span><span class="p">;</span>
|
611
|
+
<span class="p">}</span>
|
612
|
+
<span class="p">}</span>
|
613
|
+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-97"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-97">¶</a> </div> <p>Objects with different constructors are not equivalent, but <code>Object</code>s
|
614
|
+
from different frames are.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">aCtor</span> <span class="o">=</span> <span class="nx">a</span><span class="p">.</span><span class="nx">constructor</span><span class="p">,</span> <span class="nx">bCtor</span> <span class="o">=</span> <span class="nx">b</span><span class="p">.</span><span class="nx">constructor</span><span class="p">;</span>
|
615
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">aCtor</span> <span class="o">!==</span> <span class="nx">bCtor</span> <span class="o">&&</span> <span class="o">!</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">aCtor</span><span class="p">)</span> <span class="o">&&</span> <span class="p">(</span><span class="nx">aCtor</span> <span class="k">instanceof</span> <span class="nx">aCtor</span><span class="p">)</span> <span class="o">&&</span>
|
616
|
+
<span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">bCtor</span><span class="p">)</span> <span class="o">&&</span> <span class="p">(</span><span class="nx">bCtor</span> <span class="k">instanceof</span> <span class="nx">bCtor</span><span class="p">)))</span> <span class="p">{</span>
|
617
|
+
<span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
|
618
|
+
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-98"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-98">¶</a> </div> <p>Deep compare objects.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">a</span><span class="p">)</span> <span class="p">{</span>
|
619
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">key</span><span class="p">))</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-99"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-99">¶</a> </div> <p>Count the expected number of properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">size</span><span class="o">++</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-100"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-100">¶</a> </div> <p>Deep compare each member.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="nx">result</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">has</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="o">&&</span> <span class="nx">eq</span><span class="p">(</span><span class="nx">a</span><span class="p">[</span><span class="nx">key</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="nx">aStack</span><span class="p">,</span> <span class="nx">bStack</span><span class="p">)))</span> <span class="k">break</span><span class="p">;</span>
|
620
|
+
<span class="p">}</span>
|
621
|
+
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-101"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-101">¶</a> </div> <p>Ensure that both objects contain the same number of properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">result</span><span class="p">)</span> <span class="p">{</span>
|
622
|
+
<span class="k">for</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="p">{</span>
|
623
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">has</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="o">&&</span> <span class="o">!</span><span class="p">(</span><span class="nx">size</span><span class="o">--</span><span class="p">))</span> <span class="k">break</span><span class="p">;</span>
|
624
|
+
<span class="p">}</span>
|
625
|
+
<span class="nx">result</span> <span class="o">=</span> <span class="o">!</span><span class="nx">size</span><span class="p">;</span>
|
626
|
+
<span class="p">}</span>
|
627
|
+
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-102"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-102">¶</a> </div> <p>Remove the first object from the stack of traversed objects.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">aStack</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>
|
628
|
+
<span class="nx">bStack</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>
|
629
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
|
630
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-103"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-103">¶</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="nx">_</span><span class="p">.</span><span class="nx">isEqual</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
631
|
+
<span class="k">return</span> <span class="nx">eq</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="p">[],</span> <span class="p">[]);</span>
|
632
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-104"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-104">¶</a> </div> <p>Is a given array, string, or object empty?
|
633
|
+
An "empty" object has no enumerable own-properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">isEmpty</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
634
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
|
635
|
+
<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">obj</span><span class="p">)</span> <span class="o">||</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isString</span><span class="p">(</span><span class="nx">obj</span><span class="p">))</span> <span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">0</span><span class="p">;</span>
|
636
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">obj</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">has</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="k">return</span> <span class="kc">false</span><span class="p">;</span>
|
637
|
+
<span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
|
638
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-105"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-105">¶</a> </div> <p>Is a given value a DOM element?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">isElement</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
639
|
+
<span class="k">return</span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">nodeType</span> <span class="o">===</span> <span class="mi">1</span><span class="p">);</span>
|
640
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-106"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-106">¶</a> </div> <p>Is a given value an array?
|
641
|
+
Delegates to ECMA5's native Array.isArray</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span> <span class="o">=</span> <span class="nx">nativeIsArray</span> <span class="o">||</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
642
|
+
<span class="k">return</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">==</span> <span class="s1">'[object Array]'</span><span class="p">;</span>
|
643
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-107"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-107">¶</a> </div> <p>Is a given variable an object?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">isObject</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
644
|
+
<span class="k">return</span> <span class="nx">obj</span> <span class="o">===</span> <span class="nb">Object</span><span class="p">(</span><span class="nx">obj</span><span class="p">);</span>
|
645
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-108"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-108">¶</a> </div> <p>Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">each</span><span class="p">([</span><span class="s1">'Arguments'</span><span class="p">,</span> <span class="s1">'Function'</span><span class="p">,</span> <span class="s1">'String'</span><span class="p">,</span> <span class="s1">'Number'</span><span class="p">,</span> <span class="s1">'Date'</span><span class="p">,</span> <span class="s1">'RegExp'</span><span class="p">],</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
|
646
|
+
<span class="nx">_</span><span class="p">[</span><span class="s1">'is'</span> <span class="o">+</span> <span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
647
|
+
<span class="k">return</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">==</span> <span class="s1">'[object '</span> <span class="o">+</span> <span class="nx">name</span> <span class="o">+</span> <span class="s1">']'</span><span class="p">;</span>
|
648
|
+
<span class="p">};</span>
|
649
|
+
<span class="p">});</span></pre></div> </td> </tr> <tr id="section-109"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-109">¶</a> </div> <p>Define a fallback version of the method in browsers (ahem, IE), where
|
650
|
+
there isn't any inspectable "Arguments" type.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">isArguments</span><span class="p">(</span><span class="nx">arguments</span><span class="p">))</span> <span class="p">{</span>
|
651
|
+
<span class="nx">_</span><span class="p">.</span><span class="nx">isArguments</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
652
|
+
<span class="k">return</span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">&&</span> <span class="nx">_</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="s1">'callee'</span><span class="p">));</span>
|
653
|
+
<span class="p">};</span>
|
654
|
+
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-110"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-110">¶</a> </div> <p>Optimize <code>isFunction</code> if appropriate.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="p">(</span><span class="sr">/./</span><span class="p">)</span> <span class="o">!==</span> <span class="s1">'function'</span><span class="p">)</span> <span class="p">{</span>
|
655
|
+
<span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
656
|
+
<span class="k">return</span> <span class="k">typeof</span> <span class="nx">obj</span> <span class="o">===</span> <span class="s1">'function'</span><span class="p">;</span>
|
657
|
+
<span class="p">};</span>
|
658
|
+
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-111"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-111">¶</a> </div> <p>Is a given object a finite number?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nb">isFinite</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
659
|
+
<span class="k">return</span> <span class="nb">isFinite</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">&&</span> <span class="o">!</span><span class="nb">isNaN</span><span class="p">(</span><span class="nb">parseFloat</span><span class="p">(</span><span class="nx">obj</span><span class="p">));</span>
|
660
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-112"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-112">¶</a> </div> <p>Is the given value <code>NaN</code>? (NaN is the only number which does not equal itself).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nb">isNaN</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
661
|
+
<span class="k">return</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">&&</span> <span class="nx">obj</span> <span class="o">!=</span> <span class="o">+</span><span class="nx">obj</span><span class="p">;</span>
|
662
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-113"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-113">¶</a> </div> <p>Is a given value a boolean?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">isBoolean</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
663
|
+
<span class="k">return</span> <span class="nx">obj</span> <span class="o">===</span> <span class="kc">true</span> <span class="o">||</span> <span class="nx">obj</span> <span class="o">===</span> <span class="kc">false</span> <span class="o">||</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">==</span> <span class="s1">'[object Boolean]'</span><span class="p">;</span>
|
664
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-114"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-114">¶</a> </div> <p>Is a given value equal to null?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">isNull</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
665
|
+
<span class="k">return</span> <span class="nx">obj</span> <span class="o">===</span> <span class="kc">null</span><span class="p">;</span>
|
666
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-115"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-115">¶</a> </div> <p>Is a given variable undefined?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">isUndefined</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
667
|
+
<span class="k">return</span> <span class="nx">obj</span> <span class="o">===</span> <span class="k">void</span> <span class="mi">0</span><span class="p">;</span>
|
668
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-116"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-116">¶</a> </div> <p>Shortcut function for checking if an object has a given property directly
|
669
|
+
on itself (in other words, not on a prototype).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">has</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
670
|
+
<span class="k">return</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>
|
671
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-117"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-117">¶</a> </div> <h2>Utility Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-118"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-118">¶</a> </div> <p>Run Underscore.js in <em>noConflict</em> mode, returning the <code>_</code> variable to its
|
672
|
+
previous owner. Returns a reference to the Underscore object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">noConflict</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
673
|
+
<span class="nx">root</span><span class="p">.</span><span class="nx">_</span> <span class="o">=</span> <span class="nx">previousUnderscore</span><span class="p">;</span>
|
674
|
+
<span class="k">return</span> <span class="k">this</span><span class="p">;</span>
|
675
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-119"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-119">¶</a> </div> <p>Keep the identity function around for default iterators.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
|
676
|
+
<span class="k">return</span> <span class="nx">value</span><span class="p">;</span>
|
677
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-120"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-120">¶</a> </div> <p>Run a function <strong>n</strong> times.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">times</span> <span class="o">=</span> <span class="kd">function</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="p">{</span>
|
678
|
+
<span class="kd">var</span> <span class="nx">accum</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">(</span><span class="nx">n</span><span class="p">);</span>
|
679
|
+
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="nx">n</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="nx">accum</span><span class="p">[</span><span class="nx">i</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>
|
680
|
+
<span class="k">return</span> <span class="nx">accum</span><span class="p">;</span>
|
681
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-121"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-121">¶</a> </div> <p>Return a random integer between min and max (inclusive).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">random</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">min</span><span class="p">,</span> <span class="nx">max</span><span class="p">)</span> <span class="p">{</span>
|
682
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">max</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span>
|
683
|
+
<span class="nx">max</span> <span class="o">=</span> <span class="nx">min</span><span class="p">;</span>
|
684
|
+
<span class="nx">min</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
|
685
|
+
<span class="p">}</span>
|
686
|
+
<span class="k">return</span> <span class="nx">min</span> <span class="o">+</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span> <span class="o">*</span> <span class="p">(</span><span class="nx">max</span> <span class="o">-</span> <span class="nx">min</span> <span class="o">+</span> <span class="mi">1</span><span class="p">));</span>
|
687
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-122"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-122">¶</a> </div> <p>List of HTML entities for escaping.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">entityMap</span> <span class="o">=</span> <span class="p">{</span>
|
688
|
+
<span class="nx">escape</span><span class="o">:</span> <span class="p">{</span>
|
689
|
+
<span class="s1">'&'</span><span class="o">:</span> <span class="s1">'&amp;'</span><span class="p">,</span>
|
690
|
+
<span class="s1">'<'</span><span class="o">:</span> <span class="s1">'&lt;'</span><span class="p">,</span>
|
691
|
+
<span class="s1">'>'</span><span class="o">:</span> <span class="s1">'&gt;'</span><span class="p">,</span>
|
692
|
+
<span class="s1">'"'</span><span class="o">:</span> <span class="s1">'&quot;'</span><span class="p">,</span>
|
693
|
+
<span class="s2">"'"</span><span class="o">:</span> <span class="s1">'&#x27;'</span><span class="p">,</span>
|
694
|
+
<span class="s1">'/'</span><span class="o">:</span> <span class="s1">'&#x2F;'</span>
|
695
|
+
<span class="p">}</span>
|
696
|
+
<span class="p">};</span>
|
697
|
+
<span class="nx">entityMap</span><span class="p">.</span><span class="nx">unescape</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">invert</span><span class="p">(</span><span class="nx">entityMap</span><span class="p">.</span><span class="nx">escape</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-123"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-123">¶</a> </div> <p>Regexes containing the keys and values listed immediately above.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">entityRegexes</span> <span class="o">=</span> <span class="p">{</span>
|
698
|
+
<span class="nx">escape</span><span class="o">:</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s1">'['</span> <span class="o">+</span> <span class="nx">_</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">entityMap</span><span class="p">.</span><span class="nx">escape</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span> <span class="o">+</span> <span class="s1">']'</span><span class="p">,</span> <span class="s1">'g'</span><span class="p">),</span>
|
699
|
+
<span class="nx">unescape</span><span class="o">:</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s1">'('</span> <span class="o">+</span> <span class="nx">_</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">entityMap</span><span class="p">.</span><span class="nx">unescape</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s1">'|'</span><span class="p">)</span> <span class="o">+</span> <span class="s1">')'</span><span class="p">,</span> <span class="s1">'g'</span><span class="p">)</span>
|
700
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-124"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-124">¶</a> </div> <p>Functions for escaping and unescaping strings to/from HTML interpolation.</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">'escape'</span><span class="p">,</span> <span class="s1">'unescape'</span><span class="p">],</span> <span class="kd">function</span><span class="p">(</span><span class="nx">method</span><span class="p">)</span> <span class="p">{</span>
|
701
|
+
<span class="nx">_</span><span class="p">[</span><span class="nx">method</span><span class="p">]</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">string</span><span class="p">)</span> <span class="p">{</span>
|
702
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">string</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="s1">''</span><span class="p">;</span>
|
703
|
+
<span class="k">return</span> <span class="p">(</span><span class="s1">''</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="nx">entityRegexes</span><span class="p">[</span><span class="nx">method</span><span class="p">],</span> <span class="kd">function</span><span class="p">(</span><span class="nx">match</span><span class="p">)</span> <span class="p">{</span>
|
704
|
+
<span class="k">return</span> <span class="nx">entityMap</span><span class="p">[</span><span class="nx">method</span><span class="p">][</span><span class="nx">match</span><span class="p">];</span>
|
705
|
+
<span class="p">});</span>
|
706
|
+
<span class="p">};</span>
|
707
|
+
<span class="p">});</span></pre></div> </td> </tr> <tr id="section-125"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-125">¶</a> </div> <p>If the value of the named property is a function then invoke it;
|
708
|
+
otherwise, return it.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">result</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">object</span><span class="p">,</span> <span class="nx">property</span><span class="p">)</span> <span class="p">{</span>
|
709
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">object</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="k">return</span> <span class="kc">null</span><span class="p">;</span>
|
710
|
+
<span class="kd">var</span> <span class="nx">value</span> <span class="o">=</span> <span class="nx">object</span><span class="p">[</span><span class="nx">property</span><span class="p">];</span>
|
711
|
+
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">?</span> <span class="nx">value</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">object</span><span class="p">)</span> <span class="o">:</span> <span class="nx">value</span><span class="p">;</span>
|
712
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-126"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-126">¶</a> </div> <p>Add your own custom functions to the Underscore 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="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
713
|
+
<span class="nx">each</span><span class="p">(</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="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">){</span>
|
714
|
+
<span class="kd">var</span> <span class="nx">func</span> <span class="o">=</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>
|
715
|
+
<span class="nx">_</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="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
716
|
+
<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">];</span>
|
717
|
+
<span class="nx">push</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">args</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span>
|
718
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</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>
|
719
|
+
<span class="p">};</span>
|
720
|
+
<span class="p">});</span>
|
721
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-127"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-127">¶</a> </div> <p>Generate a unique integer id (unique within the entire client session).
|
722
|
+
Useful for temporary DOM ids.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">idCounter</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
|
723
|
+
<span class="nx">_</span><span class="p">.</span><span class="nx">uniqueId</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">prefix</span><span class="p">)</span> <span class="p">{</span>
|
724
|
+
<span class="kd">var</span> <span class="nx">id</span> <span class="o">=</span> <span class="o">++</span><span class="nx">idCounter</span> <span class="o">+</span> <span class="s1">''</span><span class="p">;</span>
|
725
|
+
<span class="k">return</span> <span class="nx">prefix</span> <span class="o">?</span> <span class="nx">prefix</span> <span class="o">+</span> <span class="nx">id</span> <span class="o">:</span> <span class="nx">id</span><span class="p">;</span>
|
726
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-128"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-128">¶</a> </div> <p>By default, Underscore uses ERB-style template delimiters, change the
|
727
|
+
following template settings to use alternative delimiters.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">templateSettings</span> <span class="o">=</span> <span class="p">{</span>
|
728
|
+
<span class="nx">evaluate</span> <span class="o">:</span> <span class="sr">/<%([\s\S]+?)%>/g</span><span class="p">,</span>
|
729
|
+
<span class="nx">interpolate</span> <span class="o">:</span> <span class="sr">/<%=([\s\S]+?)%>/g</span><span class="p">,</span>
|
730
|
+
<span class="nx">escape</span> <span class="o">:</span> <span class="sr">/<%-([\s\S]+?)%>/g</span>
|
731
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-129"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-129">¶</a> </div> <p>When customizing <code>templateSettings</code>, if you don't want to define an
|
732
|
+
interpolation, evaluation or escaping regex, we need one that is
|
733
|
+
guaranteed not to match.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">noMatch</span> <span class="o">=</span> <span class="sr">/(.)^/</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-130"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-130">¶</a> </div> <p>Certain characters need to be escaped so that they can be put into a
|
734
|
+
string literal.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">escapes</span> <span class="o">=</span> <span class="p">{</span>
|
735
|
+
<span class="s2">"'"</span><span class="o">:</span> <span class="s2">"'"</span><span class="p">,</span>
|
736
|
+
<span class="s1">'\\'</span><span class="o">:</span> <span class="s1">'\\'</span><span class="p">,</span>
|
737
|
+
<span class="s1">'\r'</span><span class="o">:</span> <span class="s1">'r'</span><span class="p">,</span>
|
738
|
+
<span class="s1">'\n'</span><span class="o">:</span> <span class="s1">'n'</span><span class="p">,</span>
|
739
|
+
<span class="s1">'\t'</span><span class="o">:</span> <span class="s1">'t'</span><span class="p">,</span>
|
740
|
+
<span class="s1">'\u2028'</span><span class="o">:</span> <span class="s1">'u2028'</span><span class="p">,</span>
|
741
|
+
<span class="s1">'\u2029'</span><span class="o">:</span> <span class="s1">'u2029'</span>
|
742
|
+
<span class="p">};</span>
|
743
|
+
|
744
|
+
<span class="kd">var</span> <span class="nx">escaper</span> <span class="o">=</span> <span class="sr">/\\|'|\r|\n|\t|\u2028|\u2029/g</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-131"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-131">¶</a> </div> <p>JavaScript micro-templating, similar to John Resig's implementation.
|
745
|
+
Underscore templating handles arbitrary delimiters, preserves whitespace,
|
746
|
+
and correctly escapes quotes within interpolated code.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">template</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">text</span><span class="p">,</span> <span class="nx">data</span><span class="p">,</span> <span class="nx">settings</span><span class="p">)</span> <span class="p">{</span>
|
747
|
+
<span class="kd">var</span> <span class="nx">render</span><span class="p">;</span>
|
748
|
+
<span class="nx">settings</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">defaults</span><span class="p">({},</span> <span class="nx">settings</span><span class="p">,</span> <span class="nx">_</span><span class="p">.</span><span class="nx">templateSettings</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-132"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-132">¶</a> </div> <p>Combine delimiters into one regular expression via alternation.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">matcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">([</span>
|
749
|
+
<span class="p">(</span><span class="nx">settings</span><span class="p">.</span><span class="nx">escape</span> <span class="o">||</span> <span class="nx">noMatch</span><span class="p">).</span><span class="nx">source</span><span class="p">,</span>
|
750
|
+
<span class="p">(</span><span class="nx">settings</span><span class="p">.</span><span class="nx">interpolate</span> <span class="o">||</span> <span class="nx">noMatch</span><span class="p">).</span><span class="nx">source</span><span class="p">,</span>
|
751
|
+
<span class="p">(</span><span class="nx">settings</span><span class="p">.</span><span class="nx">evaluate</span> <span class="o">||</span> <span class="nx">noMatch</span><span class="p">).</span><span class="nx">source</span>
|
752
|
+
<span class="p">].</span><span class="nx">join</span><span class="p">(</span><span class="s1">'|'</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'|$'</span><span class="p">,</span> <span class="s1">'g'</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-133"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-133">¶</a> </div> <p>Compile the template source, escaping string literals appropriately.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">index</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
|
753
|
+
<span class="kd">var</span> <span class="nx">source</span> <span class="o">=</span> <span class="s2">"__p+='"</span><span class="p">;</span>
|
754
|
+
<span class="nx">text</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">matcher</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">match</span><span class="p">,</span> <span class="nx">escape</span><span class="p">,</span> <span class="nx">interpolate</span><span class="p">,</span> <span class="nx">evaluate</span><span class="p">,</span> <span class="nx">offset</span><span class="p">)</span> <span class="p">{</span>
|
755
|
+
<span class="nx">source</span> <span class="o">+=</span> <span class="nx">text</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="nx">offset</span><span class="p">)</span>
|
756
|
+
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">escaper</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">match</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="s1">'\\'</span> <span class="o">+</span> <span class="nx">escapes</span><span class="p">[</span><span class="nx">match</span><span class="p">];</span> <span class="p">});</span>
|
757
|
+
|
758
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">escape</span><span class="p">)</span> <span class="p">{</span>
|
759
|
+
<span class="nx">source</span> <span class="o">+=</span> <span class="s2">"'+\n((__t=("</span> <span class="o">+</span> <span class="nx">escape</span> <span class="o">+</span> <span class="s2">"))==null?'':_.escape(__t))+\n'"</span><span class="p">;</span>
|
760
|
+
<span class="p">}</span>
|
761
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">interpolate</span><span class="p">)</span> <span class="p">{</span>
|
762
|
+
<span class="nx">source</span> <span class="o">+=</span> <span class="s2">"'+\n((__t=("</span> <span class="o">+</span> <span class="nx">interpolate</span> <span class="o">+</span> <span class="s2">"))==null?'':__t)+\n'"</span><span class="p">;</span>
|
763
|
+
<span class="p">}</span>
|
764
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">evaluate</span><span class="p">)</span> <span class="p">{</span>
|
765
|
+
<span class="nx">source</span> <span class="o">+=</span> <span class="s2">"';\n"</span> <span class="o">+</span> <span class="nx">evaluate</span> <span class="o">+</span> <span class="s2">"\n__p+='"</span><span class="p">;</span>
|
766
|
+
<span class="p">}</span>
|
767
|
+
<span class="nx">index</span> <span class="o">=</span> <span class="nx">offset</span> <span class="o">+</span> <span class="nx">match</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
|
768
|
+
<span class="k">return</span> <span class="nx">match</span><span class="p">;</span>
|
769
|
+
<span class="p">});</span>
|
770
|
+
<span class="nx">source</span> <span class="o">+=</span> <span class="s2">"';\n"</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-134"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-134">¶</a> </div> <p>If a variable is not specified, place data values in local scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">settings</span><span class="p">.</span><span class="nx">variable</span><span class="p">)</span> <span class="nx">source</span> <span class="o">=</span> <span class="s1">'with(obj||{}){\n'</span> <span class="o">+</span> <span class="nx">source</span> <span class="o">+</span> <span class="s1">'}\n'</span><span class="p">;</span>
|
771
|
+
|
772
|
+
<span class="nx">source</span> <span class="o">=</span> <span class="s2">"var __t,__p='',__j=Array.prototype.join,"</span> <span class="o">+</span>
|
773
|
+
<span class="s2">"print=function(){__p+=__j.call(arguments,'');};\n"</span> <span class="o">+</span>
|
774
|
+
<span class="nx">source</span> <span class="o">+</span> <span class="s2">"return __p;\n"</span><span class="p">;</span>
|
775
|
+
|
776
|
+
<span class="k">try</span> <span class="p">{</span>
|
777
|
+
<span class="nx">render</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Function</span><span class="p">(</span><span class="nx">settings</span><span class="p">.</span><span class="nx">variable</span> <span class="o">||</span> <span class="s1">'obj'</span><span class="p">,</span> <span class="s1">'_'</span><span class="p">,</span> <span class="nx">source</span><span class="p">);</span>
|
778
|
+
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
|
779
|
+
<span class="nx">e</span><span class="p">.</span><span class="nx">source</span> <span class="o">=</span> <span class="nx">source</span><span class="p">;</span>
|
780
|
+
<span class="k">throw</span> <span class="nx">e</span><span class="p">;</span>
|
781
|
+
<span class="p">}</span>
|
782
|
+
|
783
|
+
<span class="k">if</span> <span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="k">return</span> <span class="nx">render</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="nx">_</span><span class="p">);</span>
|
784
|
+
<span class="kd">var</span> <span class="nx">template</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>
|
785
|
+
<span class="k">return</span> <span class="nx">render</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">data</span><span class="p">,</span> <span class="nx">_</span><span class="p">);</span>
|
786
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-135"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-135">¶</a> </div> <p>Provide the compiled function source as a convenience for precompilation.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">template</span><span class="p">.</span><span class="nx">source</span> <span class="o">=</span> <span class="s1">'function('</span> <span class="o">+</span> <span class="p">(</span><span class="nx">settings</span><span class="p">.</span><span class="nx">variable</span> <span class="o">||</span> <span class="s1">'obj'</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'){\n'</span> <span class="o">+</span> <span class="nx">source</span> <span class="o">+</span> <span class="s1">'}'</span><span class="p">;</span>
|
787
|
+
|
788
|
+
<span class="k">return</span> <span class="nx">template</span><span class="p">;</span>
|
789
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-136"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-136">¶</a> </div> <p>Add a "chain" function, which will delegate to the wrapper.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">chain</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
790
|
+
<span class="k">return</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>
|
791
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-137"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-137">¶</a> </div> <h2>OOP</h2>
|
792
|
+
|
793
|
+
<p>If Underscore is called as a function, it returns a wrapped object that
|
794
|
+
can be used OO-style. This wrapper holds altered versions of all the
|
795
|
+
underscore functions. Wrapped objects may be chained.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-138"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-138">¶</a> </div> <p>Helper function to continue chaining intermediate results.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
|
796
|
+
<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_chain</span> <span class="o">?</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="o">:</span> <span class="nx">obj</span><span class="p">;</span>
|
797
|
+
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-139"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-139">¶</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="p">(</span><span class="nx">_</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-140"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-140">¶</a> </div> <p>Add all mutator Array functions to the wrapper.</p> </td> <td class="code"> <div class="highlight"><pre> <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="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
|
798
|
+
<span class="kd">var</span> <span class="nx">method</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span>
|
799
|
+
<span class="nx">_</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="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
800
|
+
<span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">;</span>
|
801
|
+
<span class="nx">method</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span>
|
802
|
+
<span class="k">if</span> <span class="p">((</span><span class="nx">name</span> <span class="o">==</span> <span class="s1">'shift'</span> <span class="o">||</span> <span class="nx">name</span> <span class="o">==</span> <span class="s1">'splice'</span><span class="p">)</span> <span class="o">&&</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="k">delete</span> <span class="nx">obj</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
|
803
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">obj</span><span class="p">);</span>
|
804
|
+
<span class="p">};</span>
|
805
|
+
<span class="p">});</span></pre></div> </td> </tr> <tr id="section-141"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-141">¶</a> </div> <p>Add all accessor Array functions to the wrapper.</p> </td> <td class="code"> <div class="highlight"><pre> <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="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
|
806
|
+
<span class="kd">var</span> <span class="nx">method</span> <span class="o">=</span> <span class="nx">ArrayProto</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span>
|
807
|
+
<span class="nx">_</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="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
808
|
+
<span class="k">return</span> <span class="nx">result</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</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>
|
809
|
+
<span class="p">};</span>
|
810
|
+
<span class="p">});</span>
|
811
|
+
|
812
|
+
<span class="nx">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">prototype</span><span class="p">,</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-142"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-142">¶</a> </div> <p>Start chaining a wrapped Underscore object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">chain</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
813
|
+
<span class="k">this</span><span class="p">.</span><span class="nx">_chain</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
|
814
|
+
<span class="k">return</span> <span class="k">this</span><span class="p">;</span>
|
815
|
+
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-143"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-143">¶</a> </div> <p>Extracts the result from a wrapped and chained object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">value</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
|
816
|
+
<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">;</span>
|
817
|
+
<span class="p">}</span>
|
818
|
+
|
819
|
+
<span class="p">});</span>
|
820
|
+
|
821
|
+
<span class="p">}).</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
|
822
|
+
|
823
|
+
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|