spade 0.0.8.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +0 -1
- data/.gitmodules +4 -4
- data/README.md +4 -0
- data/bin/spade +1 -1
- data/lib/spade.rb +0 -51
- data/lib/spade/cli.rb +10 -4
- data/lib/spade/version.rb +1 -1
- data/spade.gemspec +4 -21
- metadata +13 -482
- data/Gemfile +0 -3
- data/Gemfile.lock +0 -40
- data/lib/index.js +0 -14
- data/lib/node/loader.js +0 -146
- data/lib/node/sandbox.js +0 -44
- data/lib/spade.js +0 -1130
- data/lib/spade/bundle.rb +0 -171
- data/lib/spade/cli/base.rb +0 -354
- data/lib/spade/cli/owner.rb +0 -45
- data/lib/spade/cli/project_generator.rb +0 -58
- data/lib/spade/compiler.rb +0 -34
- data/lib/spade/console.rb +0 -39
- data/lib/spade/context.rb +0 -116
- data/lib/spade/credentials.rb +0 -36
- data/lib/spade/dependency_installer.rb +0 -103
- data/lib/spade/environment.rb +0 -35
- data/lib/spade/exports.rb +0 -71
- data/lib/spade/installer.rb +0 -40
- data/lib/spade/loader.rb +0 -238
- data/lib/spade/local.rb +0 -46
- data/lib/spade/package.rb +0 -157
- data/lib/spade/reactor.rb +0 -159
- data/lib/spade/remote.rb +0 -99
- data/lib/spade/repository.rb +0 -18
- data/lib/spade/server.rb +0 -66
- data/lib/spade/shell.rb +0 -36
- data/lib/spade/templates/project/LICENSE +0 -19
- data/lib/spade/templates/project/README.md +0 -21
- data/lib/spade/templates/project/lib/main.js +0 -9
- data/lib/spade/templates/project/project.json +0 -31
- data/lib/spade/templates/project/tests/main-test.js +0 -8
- data/package.json +0 -27
- data/packages/coffee-script/.gitignore +0 -8
- data/packages/coffee-script/.npmignore +0 -11
- data/packages/coffee-script/Cakefile +0 -229
- data/packages/coffee-script/LICENSE +0 -22
- data/packages/coffee-script/README +0 -47
- data/packages/coffee-script/Rakefile +0 -78
- data/packages/coffee-script/bin/cake +0 -7
- data/packages/coffee-script/bin/coffee +0 -7
- data/packages/coffee-script/documentation/coffee/aliases.coffee +0 -11
- data/packages/coffee-script/documentation/coffee/array_comprehensions.coffee +0 -2
- data/packages/coffee-script/documentation/coffee/block_comment.coffee +0 -6
- data/packages/coffee-script/documentation/coffee/cake_tasks.coffee +0 -9
- data/packages/coffee-script/documentation/coffee/classes.coffee +0 -25
- data/packages/coffee-script/documentation/coffee/comparisons.coffee +0 -5
- data/packages/coffee-script/documentation/coffee/conditionals.coffee +0 -13
- data/packages/coffee-script/documentation/coffee/default_args.coffee +0 -8
- data/packages/coffee-script/documentation/coffee/do.coffee +0 -4
- data/packages/coffee-script/documentation/coffee/embedded.coffee +0 -5
- data/packages/coffee-script/documentation/coffee/existence.coffee +0 -10
- data/packages/coffee-script/documentation/coffee/expressions.coffee +0 -9
- data/packages/coffee-script/documentation/coffee/expressions_assignment.coffee +0 -3
- data/packages/coffee-script/documentation/coffee/expressions_comprehension.coffee +0 -3
- data/packages/coffee-script/documentation/coffee/expressions_try.coffee +0 -7
- data/packages/coffee-script/documentation/coffee/fat_arrow.coffee +0 -6
- data/packages/coffee-script/documentation/coffee/functions.coffee +0 -2
- data/packages/coffee-script/documentation/coffee/heredocs.coffee +0 -7
- data/packages/coffee-script/documentation/coffee/heregexes.coffee +0 -11
- data/packages/coffee-script/documentation/coffee/interpolation.coffee +0 -6
- data/packages/coffee-script/documentation/coffee/multiple_return_values.coffee +0 -7
- data/packages/coffee-script/documentation/coffee/object_comprehensions.coffee +0 -4
- data/packages/coffee-script/documentation/coffee/object_extraction.coffee +0 -13
- data/packages/coffee-script/documentation/coffee/objects_and_arrays.coffee +0 -19
- data/packages/coffee-script/documentation/coffee/objects_reserved.coffee +0 -5
- data/packages/coffee-script/documentation/coffee/overview.coffee +0 -28
- data/packages/coffee-script/documentation/coffee/parallel_assignment.coffee +0 -6
- data/packages/coffee-script/documentation/coffee/patterns_and_splats.coffee +0 -7
- data/packages/coffee-script/documentation/coffee/prototypes.coffee +0 -3
- data/packages/coffee-script/documentation/coffee/range_comprehensions.coffee +0 -2
- data/packages/coffee-script/documentation/coffee/scope.coffee +0 -5
- data/packages/coffee-script/documentation/coffee/slices.coffee +0 -7
- data/packages/coffee-script/documentation/coffee/soaks.coffee +0 -1
- data/packages/coffee-script/documentation/coffee/splats.coffee +0 -27
- data/packages/coffee-script/documentation/coffee/splices.coffee +0 -5
- data/packages/coffee-script/documentation/coffee/strings.coffee +0 -8
- data/packages/coffee-script/documentation/coffee/switch.coffee +0 -10
- data/packages/coffee-script/documentation/coffee/try.coffee +0 -8
- data/packages/coffee-script/documentation/coffee/while.coffee +0 -10
- data/packages/coffee-script/documentation/css/docs.css +0 -374
- data/packages/coffee-script/documentation/css/idle.css +0 -64
- data/packages/coffee-script/documentation/docs/browser.html +0 -25
- data/packages/coffee-script/documentation/docs/cake.html +0 -43
- data/packages/coffee-script/documentation/docs/coffee-script.html +0 -51
- data/packages/coffee-script/documentation/docs/command.html +0 -161
- data/packages/coffee-script/documentation/docs/docco.css +0 -186
- data/packages/coffee-script/documentation/docs/grammar.html +0 -399
- data/packages/coffee-script/documentation/docs/helpers.html +0 -31
- data/packages/coffee-script/documentation/docs/index.html +0 -3
- data/packages/coffee-script/documentation/docs/lexer.html +0 -490
- data/packages/coffee-script/documentation/docs/nodes.html +0 -1338
- data/packages/coffee-script/documentation/docs/optparse.html +0 -78
- data/packages/coffee-script/documentation/docs/repl.html +0 -24
- data/packages/coffee-script/documentation/docs/rewriter.html +0 -251
- data/packages/coffee-script/documentation/docs/scope.html +0 -54
- data/packages/coffee-script/documentation/docs/underscore.html +0 -295
- data/packages/coffee-script/documentation/images/background.png +0 -0
- data/packages/coffee-script/documentation/images/banding.png +0 -0
- data/packages/coffee-script/documentation/images/button_bg.png +0 -0
- data/packages/coffee-script/documentation/images/button_bg_dark.gif +0 -0
- data/packages/coffee-script/documentation/images/button_bg_green.gif +0 -0
- data/packages/coffee-script/documentation/images/favicon.ico +0 -0
- data/packages/coffee-script/documentation/images/logo.png +0 -0
- data/packages/coffee-script/documentation/images/screenshadow.png +0 -0
- data/packages/coffee-script/documentation/index.html.erb +0 -1607
- data/packages/coffee-script/documentation/js/aliases.js +0 -17
- data/packages/coffee-script/documentation/js/array_comprehensions.js +0 -6
- data/packages/coffee-script/documentation/js/block_comment.js +0 -4
- data/packages/coffee-script/documentation/js/cake_tasks.js +0 -10
- data/packages/coffee-script/documentation/js/classes.js +0 -44
- data/packages/coffee-script/documentation/js/comparisons.js +0 -3
- data/packages/coffee-script/documentation/js/conditionals.js +0 -12
- data/packages/coffee-script/documentation/js/default_args.js +0 -7
- data/packages/coffee-script/documentation/js/do.js +0 -10
- data/packages/coffee-script/documentation/js/embedded.js +0 -4
- data/packages/coffee-script/documentation/js/existence.js +0 -6
- data/packages/coffee-script/documentation/js/expressions.js +0 -15
- data/packages/coffee-script/documentation/js/expressions_assignment.js +0 -2
- data/packages/coffee-script/documentation/js/expressions_comprehension.js +0 -9
- data/packages/coffee-script/documentation/js/expressions_try.js +0 -7
- data/packages/coffee-script/documentation/js/fat_arrow.js +0 -9
- data/packages/coffee-script/documentation/js/functions.js +0 -7
- data/packages/coffee-script/documentation/js/heredocs.js +0 -2
- data/packages/coffee-script/documentation/js/heregexes.js +0 -2
- data/packages/coffee-script/documentation/js/interpolation.js +0 -4
- data/packages/coffee-script/documentation/js/multiple_return_values.js +0 -5
- data/packages/coffee-script/documentation/js/object_comprehensions.js +0 -15
- data/packages/coffee-script/documentation/js/object_extraction.js +0 -10
- data/packages/coffee-script/documentation/js/objects_and_arrays.js +0 -17
- data/packages/coffee-script/documentation/js/objects_reserved.js +0 -4
- data/packages/coffee-script/documentation/js/overview.js +0 -35
- data/packages/coffee-script/documentation/js/parallel_assignment.js +0 -4
- data/packages/coffee-script/documentation/js/patterns_and_splats.js +0 -4
- data/packages/coffee-script/documentation/js/prototypes.js +0 -3
- data/packages/coffee-script/documentation/js/range_comprehensions.js +0 -9
- data/packages/coffee-script/documentation/js/scope.js +0 -8
- data/packages/coffee-script/documentation/js/slices.js +0 -4
- data/packages/coffee-script/documentation/js/soaks.js +0 -2
- data/packages/coffee-script/documentation/js/splats.js +0 -15
- data/packages/coffee-script/documentation/js/splices.js +0 -3
- data/packages/coffee-script/documentation/js/strings.js +0 -2
- data/packages/coffee-script/documentation/js/switch.js +0 -23
- data/packages/coffee-script/documentation/js/try.js +0 -8
- data/packages/coffee-script/documentation/js/while.js +0 -18
- data/packages/coffee-script/documentation/vendor/jquery-1.4.2.js +0 -6240
- data/packages/coffee-script/examples/beautiful_code/binary_search.coffee +0 -16
- data/packages/coffee-script/examples/beautiful_code/quicksort_runtime.coffee +0 -13
- data/packages/coffee-script/examples/beautiful_code/regular_expression_matcher.coffee +0 -34
- data/packages/coffee-script/examples/blocks.coffee +0 -54
- data/packages/coffee-script/examples/code.coffee +0 -167
- data/packages/coffee-script/examples/computer_science/README +0 -4
- data/packages/coffee-script/examples/computer_science/binary_search.coffee +0 -25
- data/packages/coffee-script/examples/computer_science/bubble_sort.coffee +0 -11
- data/packages/coffee-script/examples/computer_science/linked_list.coffee +0 -108
- data/packages/coffee-script/examples/computer_science/luhn_algorithm.coffee +0 -36
- data/packages/coffee-script/examples/computer_science/merge_sort.coffee +0 -19
- data/packages/coffee-script/examples/computer_science/selection_sort.coffee +0 -23
- data/packages/coffee-script/examples/poignant.coffee +0 -181
- data/packages/coffee-script/examples/potion.coffee +0 -206
- data/packages/coffee-script/examples/underscore.coffee +0 -682
- data/packages/coffee-script/examples/web_server.coffee +0 -12
- data/packages/coffee-script/extras/EXTRAS +0 -7
- data/packages/coffee-script/extras/coffee-script.js +0 -8
- data/packages/coffee-script/extras/jsl.conf +0 -44
- data/packages/coffee-script/index.html +0 -2515
- data/packages/coffee-script/lib/browser.js +0 -52
- data/packages/coffee-script/lib/cake.js +0 -76
- data/packages/coffee-script/lib/coffee-script.js +0 -82
- data/packages/coffee-script/lib/command.js +0 -263
- data/packages/coffee-script/lib/grammar.js +0 -581
- data/packages/coffee-script/lib/helpers.js +0 -66
- data/packages/coffee-script/lib/index.js +0 -8
- data/packages/coffee-script/lib/lexer.js +0 -633
- data/packages/coffee-script/lib/nodes.js +0 -2165
- data/packages/coffee-script/lib/optparse.js +0 -111
- data/packages/coffee-script/lib/parser.js +0 -649
- data/packages/coffee-script/lib/repl.js +0 -42
- data/packages/coffee-script/lib/rewriter.js +0 -353
- data/packages/coffee-script/lib/scope.js +0 -120
- data/packages/coffee-script/lib/spade-format.js +0 -45
- data/packages/coffee-script/package.json +0 -26
- data/packages/coffee-script/src/browser.coffee +0 -43
- data/packages/coffee-script/src/cake.coffee +0 -69
- data/packages/coffee-script/src/coffee-script.coffee +0 -92
- data/packages/coffee-script/src/command.coffee +0 -214
- data/packages/coffee-script/src/grammar.coffee +0 -590
- data/packages/coffee-script/src/helpers.coffee +0 -56
- data/packages/coffee-script/src/index.coffee +0 -2
- data/packages/coffee-script/src/lexer.coffee +0 -653
- data/packages/coffee-script/src/nodes.coffee +0 -1754
- data/packages/coffee-script/src/optparse.coffee +0 -99
- data/packages/coffee-script/src/repl.coffee +0 -42
- data/packages/coffee-script/src/rewriter.coffee +0 -326
- data/packages/coffee-script/src/scope.coffee +0 -94
- data/packages/coffee-script/test/arguments.coffee +0 -127
- data/packages/coffee-script/test/assignment.coffee +0 -98
- data/packages/coffee-script/test/break.coffee +0 -18
- data/packages/coffee-script/test/comments.coffee +0 -201
- data/packages/coffee-script/test/conditionals.coffee +0 -181
- data/packages/coffee-script/test/exception_handling.coffee +0 -90
- data/packages/coffee-script/test/helpers.coffee +0 -96
- data/packages/coffee-script/test/importing.coffee +0 -18
- data/packages/coffee-script/test/operators.coffee +0 -225
- data/packages/coffee-script/test/ranges_slices_and_splices.coffee +0 -186
- data/packages/coffee-script/test/regular_expressions.coffee +0 -56
- data/packages/coffee-script/test/test.html +0 -123
- data/packages/coffee-script/test/test_chaining.coffee +0 -77
- data/packages/coffee-script/test/test_classes.coffee +0 -372
- data/packages/coffee-script/test/test_compilation.coffee +0 -26
- data/packages/coffee-script/test/test_comprehensions.coffee +0 -318
- data/packages/coffee-script/test/test_existence.coffee +0 -165
- data/packages/coffee-script/test/test_functions.coffee +0 -379
- data/packages/coffee-script/test/test_heredocs.coffee +0 -111
- data/packages/coffee-script/test/test_literals.coffee +0 -270
- data/packages/coffee-script/test/test_option_parser.coffee +0 -27
- data/packages/coffee-script/test/test_pattern_matching.coffee +0 -162
- data/packages/coffee-script/test/test_returns.coffee +0 -63
- data/packages/coffee-script/test/test_splats.coffee +0 -102
- data/packages/coffee-script/test/test_strings.coffee +0 -118
- data/packages/coffee-script/test/test_switch.coffee +0 -103
- data/packages/coffee-script/test/test_while.coffee +0 -71
- data/packages/ivory/LICENSE.txt +0 -1
- data/packages/ivory/README.md +0 -19
- data/packages/ivory/lib/buffer.js +0 -111
- data/packages/ivory/lib/events.js +0 -137
- data/packages/ivory/lib/fs.js +0 -266
- data/packages/ivory/lib/main.js +0 -13
- data/packages/ivory/lib/path.js +0 -158
- data/packages/ivory/lib/ruby/buffer.rb +0 -145
- data/packages/ivory/lib/ruby/constants.rb +0 -585
- data/packages/ivory/lib/ruby/events.rb +0 -32
- data/packages/ivory/lib/ruby/fs.rb +0 -245
- data/packages/ivory/lib/ruby/process.rb +0 -28
- data/packages/ivory/lib/stream.js +0 -115
- data/packages/ivory/lib/util.js +0 -414
- data/packages/ivory/package.json +0 -11
- data/packages/jquery/main.js +0 -7179
- data/packages/jquery/package.json +0 -10
- data/packages/json/lib/main.js +0 -14
- data/packages/json/package.json +0 -8
- data/packages/lproj/README.md +0 -77
- data/packages/lproj/examples/demo-app/en.lproj/localized.strings +0 -2
- data/packages/lproj/examples/demo-app/fr.lproj/localized.strings +0 -3
- data/packages/lproj/examples/demo-app/index.html +0 -8
- data/packages/lproj/examples/demo-app/lib/main.js +0 -7
- data/packages/lproj/examples/demo-app/package.json +0 -9
- data/packages/lproj/lib/main.js +0 -78
- data/packages/lproj/lib/strings-format.js +0 -6
- data/packages/lproj/package.json +0 -9
- data/packages/optparse/README.md +0 -161
- data/packages/optparse/TODO +0 -1
- data/packages/optparse/examples/browser-test.html +0 -75
- data/packages/optparse/examples/nodejs-test.js +0 -90
- data/packages/optparse/lib/optparse.js +0 -309
- data/packages/optparse/package.json +0 -13
- data/packages/optparse/seed.yml +0 -5
- data/packages/text/lib/main.js +0 -8
- data/packages/text/package.json +0 -9
- data/packages/web-file/README.md +0 -7
- data/packages/web-file/lib/errors.js +0 -32
- data/packages/web-file/lib/file-reader.js +0 -10
- data/packages/web-file/lib/file-system.js +0 -234
- data/packages/web-file/lib/file-writer.js +0 -10
- data/packages/web-file/lib/file.js +0 -9
- data/packages/web-file/lib/main.js +0 -34
- data/packages/web-file/lib/platform.js +0 -25
- data/packages/web-file/lib/ruby/file.rb +0 -252
- data/packages/web-file/lib/ruby/file_reader.rb +0 -69
- data/packages/web-file/lib/ruby/file_system.rb +0 -134
- data/packages/web-file/lib/ruby/file_writer.rb +0 -78
- data/packages/web-file/package.json +0 -12
- data/packages/web-typed-array/README.md +0 -7
- data/packages/web-typed-array/lib/array-buffer-view.js +0 -9
- data/packages/web-typed-array/lib/array-buffer.js +0 -7
- data/packages/web-typed-array/lib/main.js +0 -33
- data/packages/web-typed-array/lib/platform.js +0 -20
- data/packages/web-typed-array/lib/ruby/array_buffer.rb +0 -31
- data/packages/web-typed-array/lib/ruby/array_buffer_view.rb +0 -130
- data/packages/web-typed-array/lib/ruby/typed_array.rb +0 -133
- data/packages/web-typed-array/lib/typed-array.js +0 -26
- data/packages/web-typed-array/package.json +0 -9
- data/spec/cli/build_spec.rb +0 -59
- data/spec/cli/install_spec.rb +0 -120
- data/spec/cli/installed_spec.rb +0 -55
- data/spec/cli/list_spec.rb +0 -75
- data/spec/cli/login_spec.rb +0 -76
- data/spec/cli/new_spec.rb +0 -5
- data/spec/cli/owner_spec.rb +0 -115
- data/spec/cli/push_spec.rb +0 -74
- data/spec/cli/uninstall_spec.rb +0 -58
- data/spec/cli/unpack_spec.rb +0 -73
- data/spec/cli/unyank_spec.rb +0 -74
- data/spec/cli/update_spec.rb +0 -8
- data/spec/cli/yank_spec.rb +0 -74
- data/spec/credentials_spec.rb +0 -24
- data/spec/fixtures/coffee-1.0.1.pre.spd +0 -0
- data/spec/fixtures/core-test-0.4.3.spd +0 -0
- data/spec/fixtures/core-test/bin/cot +0 -3
- data/spec/fixtures/core-test/lib/main.js +0 -1
- data/spec/fixtures/core-test/resources/runner.css +0 -0
- data/spec/fixtures/core-test/tests/test.js +0 -1
- data/spec/fixtures/ivory-0.0.1.spd +0 -0
- data/spec/fixtures/jquery-1.4.3.spd +0 -0
- data/spec/fixtures/optparse-1.0.1.spd +0 -0
- data/spec/fixtures/package.json +0 -30
- data/spec/gauntlet_spec.rb +0 -27
- data/spec/javascript/async-test.js +0 -123
- data/spec/javascript/compiler/javascript.js +0 -13
- data/spec/javascript/compiler/ruby.js +0 -14
- data/spec/javascript/loader-test.js +0 -64
- data/spec/javascript/normalize-test.js +0 -73
- data/spec/javascript/packages-test.js +0 -44
- data/spec/javascript/relative-require-test.js +0 -72
- data/spec/javascript/require-test.js +0 -117
- data/spec/javascript/sandbox/compile.js +0 -37
- data/spec/javascript/sandbox/creation.js +0 -44
- data/spec/javascript/sandbox/format.js +0 -79
- data/spec/javascript/sandbox/misc.js +0 -57
- data/spec/javascript/sandbox/preprocessor.js +0 -81
- data/spec/javascript/sandbox/require.js +0 -48
- data/spec/javascript/sandbox/run-command.js +0 -21
- data/spec/javascript/spade/externs.js +0 -14
- data/spec/javascript/spade/load-factory.js +0 -15
- data/spec/javascript/spade/misc.js +0 -23
- data/spec/javascript/spade/ready.js +0 -12
- data/spec/javascript/spade/register.js +0 -13
- data/spec/javascript_spec.rb +0 -7
- data/spec/package_spec.rb +0 -267
- data/spec/spec_helper.rb +0 -30
- data/spec/support/cli.rb +0 -94
- data/spec/support/core_test.rb +0 -59
- data/spec/support/fake.rb +0 -44
- data/spec/support/fake_gem_server.rb +0 -66
- data/spec/support/fake_gemcutter.rb +0 -49
- data/spec/support/matchers.rb +0 -31
- data/spec/support/path.rb +0 -54
- data/test-spade.html +0 -8
@@ -1,78 +0,0 @@
|
|
1
|
-
<!DOCTYPE html> <html> <head> <title>optparse.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To … <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="browser.html"> browser.coffee </a> <a class="source" href="cake.html"> cake.coffee </a> <a class="source" href="coffee-script.html"> coffee-script.coffee </a> <a class="source" href="command.html"> command.coffee </a> <a class="source" href="grammar.html"> grammar.coffee </a> <a class="source" href="helpers.html"> helpers.coffee </a> <a class="source" href="index.html"> index.coffee </a> <a class="source" href="lexer.html"> lexer.coffee </a> <a class="source" href="nodes.html"> nodes.coffee </a> <a class="source" href="optparse.html"> optparse.coffee </a> <a class="source" href="repl.html"> repl.coffee </a> <a class="source" href="rewriter.html"> rewriter.coffee </a> <a class="source" href="scope.html"> scope.coffee </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> optparse.coffee </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> <p>A simple <strong>OptionParser</strong> class to parse option flags from the command-line.
|
2
|
-
Use it like so:</p>
|
3
|
-
|
4
|
-
<pre><code>parser = new OptionParser switches, helpBanner
|
5
|
-
options = parser.parse process.argv
|
6
|
-
</code></pre>
|
7
|
-
|
8
|
-
<p>The first non-option is considered to be the start of the file (and file
|
9
|
-
option) list, and all subsequent arguments are left unparsed.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.OptionParser = </span><span class="nx">class</span> <span class="nx">OptionParser</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>Initialize with a list of valid options, in the form:</p>
|
10
|
-
|
11
|
-
<pre><code>[short-flag, long-flag, description]
|
12
|
-
</code></pre>
|
13
|
-
|
14
|
-
<p>Along with an an optional banner for the usage help.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">rules</span><span class="p">,</span> <span class="nx">@banner</span><span class="p">)</span> <span class="o">-></span>
|
15
|
-
<span class="vi">@rules = </span><span class="nx">buildRules</span> <span class="nx">rules</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <p>Parse the list of arguments, populating an <code>options</code> object with all of the
|
16
|
-
specified options, and returning it. <code>options.arguments</code> will be an array
|
17
|
-
containing the remaining non-option arguments. <code>options.literals</code> will be
|
18
|
-
an array of options that are meant to be passed through directly to the
|
19
|
-
executing script. This is a simpler API than many option parsers that allow
|
20
|
-
you to attach callback actions for every flag. Instead, you're responsible
|
21
|
-
for interpreting the options object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">parse</span><span class="o">:</span> <span class="p">(</span><span class="nx">args</span><span class="p">)</span> <span class="o">-></span>
|
22
|
-
<span class="nv">options = </span><span class="nx">arguments</span><span class="o">:</span> <span class="p">[],</span> <span class="nx">literals</span><span class="o">:</span> <span class="p">[]</span>
|
23
|
-
<span class="nv">args = </span><span class="nx">normalizeArguments</span> <span class="nx">args</span>
|
24
|
-
<span class="k">for</span> <span class="nx">arg</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">args</span>
|
25
|
-
<span class="k">if</span> <span class="nx">arg</span> <span class="o">is</span> <span class="s1">'--'</span>
|
26
|
-
<span class="nv">options.literals = </span><span class="nx">args</span><span class="p">[(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)..]</span>
|
27
|
-
<span class="k">break</span>
|
28
|
-
<span class="nv">isOption = </span><span class="o">!!</span><span class="p">(</span><span class="nx">arg</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">LONG_FLAG</span><span class="p">)</span> <span class="o">or</span> <span class="nx">arg</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">SHORT_FLAG</span><span class="p">))</span>
|
29
|
-
<span class="nv">matchedRule = </span><span class="kc">no</span>
|
30
|
-
<span class="k">for</span> <span class="nx">rule</span> <span class="k">in</span> <span class="nx">@rules</span>
|
31
|
-
<span class="k">if</span> <span class="nx">rule</span><span class="p">.</span><span class="nx">shortFlag</span> <span class="o">is</span> <span class="nx">arg</span> <span class="o">or</span> <span class="nx">rule</span><span class="p">.</span><span class="nx">longFlag</span> <span class="o">is</span> <span class="nx">arg</span>
|
32
|
-
<span class="nv">value = </span><span class="k">if</span> <span class="nx">rule</span><span class="p">.</span><span class="nx">hasArgument</span> <span class="k">then</span> <span class="nx">args</span><span class="p">[</span><span class="nx">i</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">]</span> <span class="k">else</span> <span class="kc">true</span>
|
33
|
-
<span class="nx">options</span><span class="p">[</span><span class="nx">rule</span><span class="p">.</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="k">if</span> <span class="nx">rule</span><span class="p">.</span><span class="nx">isList</span> <span class="k">then</span> <span class="p">(</span><span class="nx">options</span><span class="p">[</span><span class="nx">rule</span><span class="p">.</span><span class="nx">name</span><span class="p">]</span> <span class="o">or</span> <span class="p">[]).</span><span class="nx">concat</span> <span class="nx">value</span> <span class="k">else</span> <span class="nx">value</span>
|
34
|
-
<span class="nv">matchedRule = </span><span class="kc">yes</span>
|
35
|
-
<span class="k">break</span>
|
36
|
-
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s2">"unrecognized option: #{arg}"</span> <span class="k">if</span> <span class="nx">isOption</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">matchedRule</span>
|
37
|
-
<span class="k">if</span> <span class="o">not</span> <span class="nx">isOption</span>
|
38
|
-
<span class="nv">options.arguments = </span><span class="nx">args</span><span class="p">.</span><span class="nx">slice</span> <span class="nx">i</span>
|
39
|
-
<span class="k">break</span>
|
40
|
-
<span class="nx">options</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>Return the help text for this <strong>OptionParser</strong>, listing and describing all
|
41
|
-
of the valid options, for <code>--help</code> and such.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">help</span><span class="o">:</span> <span class="o">-></span>
|
42
|
-
<span class="nv">lines = </span><span class="p">[</span><span class="s1">'Available options:'</span><span class="p">]</span>
|
43
|
-
<span class="nx">lines</span><span class="p">.</span><span class="nx">unshift</span> <span class="s2">"#{@banner}\n"</span> <span class="k">if</span> <span class="nx">@banner</span>
|
44
|
-
<span class="k">for</span> <span class="nx">rule</span> <span class="k">in</span> <span class="nx">@rules</span>
|
45
|
-
<span class="nv">spaces = </span><span class="mi">15</span> <span class="o">-</span> <span class="nx">rule</span><span class="p">.</span><span class="nx">longFlag</span><span class="p">.</span><span class="nx">length</span>
|
46
|
-
<span class="nv">spaces = </span><span class="k">if</span> <span class="nx">spaces</span> <span class="o">></span> <span class="mi">0</span> <span class="k">then</span> <span class="nb">Array</span><span class="p">(</span><span class="nx">spaces</span> <span class="o">+</span> <span class="mi">1</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s1">' '</span><span class="p">)</span> <span class="k">else</span> <span class="s1">''</span>
|
47
|
-
<span class="nv">letPart = </span><span class="k">if</span> <span class="nx">rule</span><span class="p">.</span><span class="nx">shortFlag</span> <span class="k">then</span> <span class="nx">rule</span><span class="p">.</span><span class="nx">shortFlag</span> <span class="o">+</span> <span class="s1">', '</span> <span class="k">else</span> <span class="s1">' '</span>
|
48
|
-
<span class="nx">lines</span><span class="p">.</span><span class="nx">push</span> <span class="s1">' '</span> <span class="o">+</span> <span class="nx">letPart</span> <span class="o">+</span> <span class="nx">rule</span><span class="p">.</span><span class="nx">longFlag</span> <span class="o">+</span> <span class="nx">spaces</span> <span class="o">+</span> <span class="nx">rule</span><span class="p">.</span><span class="nx">description</span>
|
49
|
-
<span class="s2">"\n#{ lines.join('\n') }\n"</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <h2>Helpers</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>Regex matchers for option flags.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">LONG_FLAG = </span><span class="sr">/^(--\w[\w\-]+)/</span>
|
50
|
-
<span class="nv">SHORT_FLAG = </span><span class="sr">/^(-\w)/</span>
|
51
|
-
<span class="nv">MULTI_FLAG = </span><span class="sr">/^-(\w{2,})/</span>
|
52
|
-
<span class="nv">OPTIONAL = </span><span class="sr">/\[(\w+(\*?))\]/</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>Build and return the list of option rules. If the optional <em>short-flag</em> is
|
53
|
-
unspecified, leave it out by padding with <code>null</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">buildRules = </span><span class="p">(</span><span class="nx">rules</span><span class="p">)</span> <span class="o">-></span>
|
54
|
-
<span class="k">for</span> <span class="nx">tuple</span> <span class="k">in</span> <span class="nx">rules</span>
|
55
|
-
<span class="nx">tuple</span><span class="p">.</span><span class="nx">unshift</span> <span class="kc">null</span> <span class="k">if</span> <span class="nx">tuple</span><span class="p">.</span><span class="nx">length</span> <span class="o"><</span> <span class="mi">3</span>
|
56
|
-
<span class="nx">buildRule</span> <span class="nx">tuple</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>Build a rule from a <code>-o</code> short flag, a <code>--output [DIR]</code> long flag, and the
|
57
|
-
description of what the option does.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">buildRule = </span><span class="p">(</span><span class="nx">shortFlag</span><span class="p">,</span> <span class="nx">longFlag</span><span class="p">,</span> <span class="nx">description</span><span class="p">,</span> <span class="nv">options = </span><span class="p">{})</span> <span class="o">-></span>
|
58
|
-
<span class="nv">match = </span><span class="nx">longFlag</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">OPTIONAL</span><span class="p">)</span>
|
59
|
-
<span class="nv">longFlag = </span><span class="nx">longFlag</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">LONG_FLAG</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
|
60
|
-
<span class="p">{</span>
|
61
|
-
<span class="nx">name</span><span class="o">:</span> <span class="nx">longFlag</span><span class="p">.</span><span class="nx">substr</span> <span class="mi">2</span>
|
62
|
-
<span class="nx">shortFlag</span><span class="o">:</span> <span class="nx">shortFlag</span>
|
63
|
-
<span class="nx">longFlag</span><span class="o">:</span> <span class="nx">longFlag</span>
|
64
|
-
<span class="nx">description</span><span class="o">:</span> <span class="nx">description</span>
|
65
|
-
<span class="nx">hasArgument</span><span class="o">:</span> <span class="o">!!</span><span class="p">(</span><span class="nx">match</span> <span class="o">and</span> <span class="nx">match</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
|
66
|
-
<span class="nx">isList</span><span class="o">:</span> <span class="o">!!</span><span class="p">(</span><span class="nx">match</span> <span class="o">and</span> <span class="nx">match</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
|
67
|
-
<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>Normalize arguments by expanding merged flags into multiple flags. This allows
|
68
|
-
you to have <code>-wl</code> be the same as <code>--watch --lint</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">normalizeArguments = </span><span class="p">(</span><span class="nx">args</span><span class="p">)</span> <span class="o">-></span>
|
69
|
-
<span class="nv">args = </span><span class="nx">args</span><span class="p">.</span><span class="nx">slice</span> <span class="mi">0</span>
|
70
|
-
<span class="nv">result = </span><span class="p">[]</span>
|
71
|
-
<span class="k">for</span> <span class="nx">arg</span> <span class="k">in</span> <span class="nx">args</span>
|
72
|
-
<span class="k">if</span> <span class="nv">match = </span><span class="nx">arg</span><span class="p">.</span><span class="nx">match</span> <span class="nx">MULTI_FLAG</span>
|
73
|
-
<span class="nx">result</span><span class="p">.</span><span class="nx">push</span> <span class="s1">'-'</span> <span class="o">+</span> <span class="nx">l</span> <span class="k">for</span> <span class="nx">l</span> <span class="k">in</span> <span class="nx">match</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">split</span> <span class="s1">''</span>
|
74
|
-
<span class="k">else</span>
|
75
|
-
<span class="nx">result</span><span class="p">.</span><span class="nx">push</span> <span class="nx">arg</span>
|
76
|
-
<span class="nx">result</span>
|
77
|
-
|
78
|
-
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|
@@ -1,24 +0,0 @@
|
|
1
|
-
<!DOCTYPE html> <html> <head> <title>repl.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To … <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="browser.html"> browser.coffee </a> <a class="source" href="cake.html"> cake.coffee </a> <a class="source" href="coffee-script.html"> coffee-script.coffee </a> <a class="source" href="command.html"> command.coffee </a> <a class="source" href="grammar.html"> grammar.coffee </a> <a class="source" href="helpers.html"> helpers.coffee </a> <a class="source" href="index.html"> index.coffee </a> <a class="source" href="lexer.html"> lexer.coffee </a> <a class="source" href="nodes.html"> nodes.coffee </a> <a class="source" href="optparse.html"> optparse.coffee </a> <a class="source" href="repl.html"> repl.coffee </a> <a class="source" href="rewriter.html"> rewriter.coffee </a> <a class="source" href="scope.html"> scope.coffee </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> repl.coffee </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> <p>A very simple Read-Eval-Print-Loop. Compiles one line at a time to JavaScript
|
2
|
-
and evaluates it. Good for simple tests, or poking around the <strong>Node.js</strong> API.
|
3
|
-
Using it looks like this:</p>
|
4
|
-
|
5
|
-
<pre><code>coffee> console.log "#{num} bottles of beer" for num in [99..1]
|
6
|
-
</code></pre> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>Require the <strong>coffee-script</strong> module to get access to the compiler.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">CoffeeScript = </span><span class="nx">require</span> <span class="s1">'./coffee-script'</span>
|
7
|
-
<span class="nv">helpers = </span><span class="nx">require</span> <span class="s1">'./helpers'</span>
|
8
|
-
<span class="nv">readline = </span><span class="nx">require</span> <span class="s1">'readline'</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <p>Start by opening up <strong>stdio</strong>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">stdio = </span><span class="nx">process</span><span class="p">.</span><span class="nx">openStdin</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>Log an error.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">error = </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="o">-></span>
|
9
|
-
<span class="nx">stdio</span><span class="p">.</span><span class="nx">write</span> <span class="p">(</span><span class="nx">err</span><span class="p">.</span><span class="nx">stack</span> <span class="o">or</span> <span class="nx">err</span><span class="p">.</span><span class="nx">toString</span><span class="p">())</span> <span class="o">+</span> <span class="s1">'\n\n'</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>Quick alias for quitting the REPL.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">helpers</span><span class="p">.</span><span class="nx">extend</span> <span class="nx">global</span><span class="p">,</span> <span class="nx">quit</span><span class="o">:</span> <span class="o">-></span> <span class="nx">process</span><span class="p">.</span><span class="nx">exit</span><span class="p">(</span><span class="mi">0</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>The main REPL function. <strong>run</strong> is called every time a line of code is entered.
|
10
|
-
Attempt to evaluate the command. If there's an exception, print it out instead
|
11
|
-
of exiting.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">run = </span><span class="p">(</span><span class="nx">buffer</span><span class="p">)</span> <span class="o">-></span>
|
12
|
-
<span class="k">try</span>
|
13
|
-
<span class="nv">val = </span><span class="nx">CoffeeScript</span><span class="p">.</span><span class="nb">eval</span> <span class="nx">buffer</span><span class="p">.</span><span class="nx">toString</span><span class="p">(),</span> <span class="nx">bare</span><span class="o">:</span> <span class="kc">on</span><span class="p">,</span> <span class="nx">globals</span><span class="o">:</span> <span class="kc">on</span><span class="p">,</span> <span class="nx">fileName</span><span class="o">:</span> <span class="s1">'repl'</span>
|
14
|
-
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="nx">val</span> <span class="k">if</span> <span class="nx">val</span> <span class="o">isnt</span> <span class="kc">undefined</span>
|
15
|
-
<span class="k">catch</span> <span class="nx">err</span>
|
16
|
-
<span class="nx">error</span> <span class="nx">err</span>
|
17
|
-
<span class="nx">repl</span><span class="p">.</span><span class="nx">prompt</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>Make sure that uncaught exceptions don't kill the REPL.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">process</span><span class="p">.</span><span class="kc">on</span> <span class="s1">'uncaughtException'</span><span class="p">,</span> <span class="nx">error</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>Create the REPL by listening to <strong>stdin</strong>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">repl = </span><span class="nx">readline</span><span class="p">.</span><span class="nx">createInterface</span> <span class="nx">stdio</span>
|
18
|
-
<span class="nx">repl</span><span class="p">.</span><span class="nx">setPrompt</span> <span class="s1">'coffee> '</span>
|
19
|
-
<span class="nx">stdio</span><span class="p">.</span><span class="kc">on</span> <span class="s1">'data'</span><span class="p">,</span> <span class="p">(</span><span class="nx">buffer</span><span class="p">)</span> <span class="o">-></span> <span class="nx">repl</span><span class="p">.</span><span class="nx">write</span> <span class="nx">buffer</span>
|
20
|
-
<span class="nx">repl</span><span class="p">.</span><span class="kc">on</span> <span class="s1">'close'</span><span class="p">,</span> <span class="o">-></span> <span class="nx">stdio</span><span class="p">.</span><span class="nx">destroy</span><span class="p">()</span>
|
21
|
-
<span class="nx">repl</span><span class="p">.</span><span class="kc">on</span> <span class="s1">'line'</span><span class="p">,</span> <span class="nx">run</span>
|
22
|
-
<span class="nx">repl</span><span class="p">.</span><span class="nx">prompt</span><span class="p">()</span>
|
23
|
-
|
24
|
-
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|
@@ -1,251 +0,0 @@
|
|
1
|
-
<!DOCTYPE html> <html> <head> <title>rewriter.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To … <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="browser.html"> browser.coffee </a> <a class="source" href="cake.html"> cake.coffee </a> <a class="source" href="coffee-script.html"> coffee-script.coffee </a> <a class="source" href="command.html"> command.coffee </a> <a class="source" href="grammar.html"> grammar.coffee </a> <a class="source" href="helpers.html"> helpers.coffee </a> <a class="source" href="index.html"> index.coffee </a> <a class="source" href="lexer.html"> lexer.coffee </a> <a class="source" href="nodes.html"> nodes.coffee </a> <a class="source" href="optparse.html"> optparse.coffee </a> <a class="source" href="repl.html"> repl.coffee </a> <a class="source" href="rewriter.html"> rewriter.coffee </a> <a class="source" href="scope.html"> scope.coffee </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> rewriter.coffee </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> <p>The CoffeeScript language has a good deal of optional syntax, implicit syntax,
|
2
|
-
and shorthand syntax. This can greatly complicate a grammar and bloat
|
3
|
-
the resulting parse table. Instead of making the parser handle it all, we take
|
4
|
-
a series of passes over the token stream, using this <strong>Rewriter</strong> to convert
|
5
|
-
shorthand into the unambiguous long form, add implicit indentation and
|
6
|
-
parentheses, balance incorrect nestings, and generally clean things up.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>The <strong>Rewriter</strong> class is used by the <a href="lexer.html">Lexer</a>, directly against
|
7
|
-
its internal array of tokens.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">class</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">Rewriter</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <p>Helpful snippet for debugging:
|
8
|
-
console.log (t[0] + '/' + t[1] for t in @tokens).join ' '</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>Rewrite the token stream in multiple passes, one logical filter at
|
9
|
-
a time. This could certainly be changed into a single pass through the
|
10
|
-
stream, with a big ol' efficient switch, but it's much nicer to work with
|
11
|
-
like this. The order of these passes matters -- indentation must be
|
12
|
-
corrected before implicit parentheses can be wrapped around blocks of code.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">rewrite</span><span class="o">:</span> <span class="p">(</span><span class="nx">@tokens</span><span class="p">)</span> <span class="o">-></span>
|
13
|
-
<span class="nx">@removeLeadingNewlines</span><span class="p">()</span>
|
14
|
-
<span class="nx">@removeMidExpressionNewlines</span><span class="p">()</span>
|
15
|
-
<span class="nx">@closeOpenCalls</span><span class="p">()</span>
|
16
|
-
<span class="nx">@closeOpenIndexes</span><span class="p">()</span>
|
17
|
-
<span class="nx">@addImplicitIndentation</span><span class="p">()</span>
|
18
|
-
<span class="nx">@tagPostfixConditionals</span><span class="p">()</span>
|
19
|
-
<span class="nx">@addImplicitBraces</span><span class="p">()</span>
|
20
|
-
<span class="nx">@addImplicitParentheses</span><span class="p">()</span>
|
21
|
-
<span class="nx">@ensureBalance</span> <span class="nx">BALANCED_PAIRS</span>
|
22
|
-
<span class="nx">@rewriteClosingParens</span><span class="p">()</span>
|
23
|
-
<span class="nx">@tokens</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>Rewrite the token stream, looking one token ahead and behind.
|
24
|
-
Allow the return value of the block to tell us how many tokens to move
|
25
|
-
forwards (or backwards) in the stream, to make sure we don't miss anything
|
26
|
-
as tokens are inserted and removed, and the stream changes length under
|
27
|
-
our feet.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">scanTokens</span><span class="o">:</span> <span class="p">(</span><span class="nx">block</span><span class="p">)</span> <span class="o">-></span>
|
28
|
-
<span class="p">{</span><span class="nx">tokens</span><span class="p">}</span> <span class="o">=</span> <span class="k">this</span>
|
29
|
-
<span class="nv">i = </span><span class="mi">0</span>
|
30
|
-
<span class="nx">i</span> <span class="o">+=</span> <span class="nx">block</span><span class="p">.</span><span class="nx">call</span> <span class="k">this</span><span class="p">,</span> <span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">tokens</span> <span class="k">while</span> <span class="nv">token = </span><span class="nx">tokens</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span>
|
31
|
-
<span class="kc">true</span>
|
32
|
-
|
33
|
-
<span class="nx">detectEnd</span><span class="o">:</span> <span class="p">(</span><span class="nx">i</span><span class="p">,</span> <span class="nx">condition</span><span class="p">,</span> <span class="nx">action</span><span class="p">)</span> <span class="o">-></span>
|
34
|
-
<span class="p">{</span><span class="nx">tokens</span><span class="p">}</span> <span class="o">=</span> <span class="k">this</span>
|
35
|
-
<span class="nv">levels = </span><span class="mi">0</span>
|
36
|
-
<span class="k">while</span> <span class="nv">token = </span><span class="nx">tokens</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span>
|
37
|
-
<span class="k">return</span> <span class="nx">action</span><span class="p">.</span><span class="nx">call</span> <span class="k">this</span><span class="p">,</span> <span class="nx">token</span><span class="p">,</span> <span class="nx">i</span> <span class="k">if</span> <span class="nx">levels</span> <span class="o">is</span> <span class="mi">0</span> <span class="o">and</span> <span class="nx">condition</span><span class="p">.</span><span class="nx">call</span> <span class="k">this</span><span class="p">,</span> <span class="nx">token</span><span class="p">,</span> <span class="nx">i</span>
|
38
|
-
<span class="k">return</span> <span class="nx">action</span><span class="p">.</span><span class="nx">call</span> <span class="k">this</span><span class="p">,</span> <span class="nx">token</span><span class="p">,</span> <span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">token</span> <span class="o">or</span> <span class="nx">levels</span> <span class="o"><</span> <span class="mi">0</span>
|
39
|
-
<span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="nx">EXPRESSION_START</span>
|
40
|
-
<span class="nx">levels</span> <span class="o">+=</span> <span class="mi">1</span>
|
41
|
-
<span class="k">else</span> <span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="nx">EXPRESSION_END</span>
|
42
|
-
<span class="nx">levels</span> <span class="o">-=</span> <span class="mi">1</span>
|
43
|
-
<span class="nx">i</span> <span class="o">+=</span> <span class="mi">1</span>
|
44
|
-
<span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>Leading newlines would introduce an ambiguity in the grammar, so we
|
45
|
-
dispatch them here.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">removeLeadingNewlines</span><span class="o">:</span> <span class="o">-></span>
|
46
|
-
<span class="k">break</span> <span class="k">for</span> <span class="p">[</span><span class="nx">tag</span><span class="p">],</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">@tokens</span> <span class="k">when</span> <span class="nx">tag</span> <span class="o">isnt</span> <span class="s1">'TERMINATOR'</span>
|
47
|
-
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">i</span> <span class="k">if</span> <span class="nx">i</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>Some blocks occur in the middle of expressions -- when we're expecting
|
48
|
-
this, remove their trailing newlines.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">removeMidExpressionNewlines</span><span class="o">:</span> <span class="o">-></span>
|
49
|
-
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">tokens</span><span class="p">)</span> <span class="o">-></span>
|
50
|
-
<span class="k">return</span> <span class="mi">1</span> <span class="nx">unless</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'TERMINATOR'</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="k">in</span> <span class="nx">EXPRESSION_CLOSE</span>
|
51
|
-
<span class="nx">tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">1</span>
|
52
|
-
<span class="mi">0</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>The lexer has tagged the opening parenthesis of a method call. Match it with
|
53
|
-
its paired close. We have the mis-nested outdent case included here for
|
54
|
-
calls that close on the same line, just before their outdent.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">closeOpenCalls</span><span class="o">:</span> <span class="o">-></span>
|
55
|
-
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
56
|
-
<span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="p">[</span><span class="s1">')'</span><span class="p">,</span> <span class="s1">'CALL_END'</span><span class="p">]</span> <span class="o">or</span>
|
57
|
-
<span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'OUTDENT'</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">is</span> <span class="s1">')'</span>
|
58
|
-
<span class="nv">action = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
59
|
-
<span class="nx">@tokens</span><span class="p">[</span><span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'OUTDENT'</span> <span class="k">then</span> <span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span> <span class="k">else</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="s1">'CALL_END'</span>
|
60
|
-
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
61
|
-
<span class="nx">@detectEnd</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">condition</span><span class="p">,</span> <span class="nx">action</span> <span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'CALL_START'</span>
|
62
|
-
<span class="mi">1</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <p>The lexer has tagged the opening parenthesis of an indexing operation call.
|
63
|
-
Match it with its paired close.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">closeOpenIndexes</span><span class="o">:</span> <span class="o">-></span>
|
64
|
-
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="p">[</span><span class="s1">']'</span><span class="p">,</span> <span class="s1">'INDEX_END'</span><span class="p">]</span>
|
65
|
-
<span class="nv">action = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'INDEX_END'</span>
|
66
|
-
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
67
|
-
<span class="nx">@detectEnd</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">condition</span><span class="p">,</span> <span class="nx">action</span> <span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'INDEX_START'</span>
|
68
|
-
<span class="mi">1</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>Object literals may be written with implicit braces, for simple cases.
|
69
|
-
Insert the missing braces here, so that the parser doesn't have to.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addImplicitBraces</span><span class="o">:</span> <span class="o">-></span>
|
70
|
-
<span class="nv">stack = </span><span class="p">[]</span>
|
71
|
-
<span class="nv">start = </span><span class="kc">null</span>
|
72
|
-
<span class="nv">startIndent = </span><span class="mi">0</span>
|
73
|
-
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
74
|
-
<span class="p">[</span><span class="nx">one</span><span class="p">,</span> <span class="nx">two</span><span class="p">,</span> <span class="nx">three</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</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">3</span><span class="p">]</span>
|
75
|
-
<span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="s1">'HERECOMMENT'</span> <span class="o">is</span> <span class="nx">one</span><span class="o">?</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
76
|
-
<span class="p">[</span><span class="nx">tag</span><span class="p">]</span> <span class="o">=</span> <span class="nx">token</span>
|
77
|
-
<span class="p">(</span><span class="nx">tag</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">]</span> <span class="o">and</span>
|
78
|
-
<span class="o">not</span> <span class="p">(</span><span class="nx">two</span><span class="o">?</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">':'</span> <span class="o">or</span> <span class="nx">one</span><span class="o">?</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'@'</span> <span class="o">and</span> <span class="nx">three</span><span class="o">?</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">':'</span><span class="p">))</span> <span class="o">or</span>
|
79
|
-
<span class="p">(</span><span class="nx">tag</span> <span class="o">is</span> <span class="s1">','</span> <span class="o">and</span> <span class="nx">one</span> <span class="o">and</span>
|
80
|
-
<span class="nx">one</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">not</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'IDENTIFIER'</span><span class="p">,</span> <span class="s1">'NUMBER'</span><span class="p">,</span> <span class="s1">'STRING'</span><span class="p">,</span> <span class="s1">'@'</span><span class="p">,</span> <span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">])</span>
|
81
|
-
<span class="nv">action = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span> <span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="s1">'}'</span><span class="p">,</span> <span class="s1">'}'</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]]</span>
|
82
|
-
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">tokens</span><span class="p">)</span> <span class="o">-></span>
|
83
|
-
<span class="k">if</span> <span class="p">(</span><span class="nv">tag = </span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="k">in</span> <span class="nx">EXPRESSION_START</span>
|
84
|
-
<span class="nx">stack</span><span class="p">.</span><span class="nx">push</span> <span class="p">[(</span><span class="k">if</span> <span class="nx">tag</span> <span class="o">is</span> <span class="s1">'INDENT'</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'{'</span> <span class="k">then</span> <span class="s1">'{'</span> <span class="k">else</span> <span class="nx">tag</span><span class="p">),</span> <span class="nx">i</span><span class="p">]</span>
|
85
|
-
<span class="k">return</span> <span class="mi">1</span>
|
86
|
-
<span class="k">if</span> <span class="nx">tag</span> <span class="k">in</span> <span class="nx">EXPRESSION_END</span>
|
87
|
-
<span class="nv">start = </span><span class="nx">stack</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span>
|
88
|
-
<span class="k">return</span> <span class="mi">1</span>
|
89
|
-
<span class="k">return</span> <span class="mi">1</span> <span class="nx">unless</span> <span class="nx">tag</span> <span class="o">is</span> <span class="s1">':'</span> <span class="o">and</span>
|
90
|
-
<span class="p">((</span><span class="nv">ago = </span><span class="nx">@tag</span> <span class="nx">i</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">is</span> <span class="s1">':'</span> <span class="o">or</span> <span class="nx">stack</span><span class="p">[</span><span class="nx">stack</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="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">isnt</span> <span class="s1">'{'</span><span class="p">)</span>
|
91
|
-
<span class="nx">stack</span><span class="p">.</span><span class="nx">push</span> <span class="p">[</span><span class="s1">'{'</span><span class="p">]</span>
|
92
|
-
<span class="nv">idx = </span> <span class="k">if</span> <span class="nx">ago</span> <span class="o">is</span> <span class="s1">'@'</span> <span class="k">then</span> <span class="nx">i</span> <span class="o">-</span> <span class="mi">2</span> <span class="k">else</span> <span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span>
|
93
|
-
<span class="nx">idx</span> <span class="o">-=</span> <span class="mi">2</span> <span class="k">while</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">idx</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'HERECOMMENT'</span>
|
94
|
-
<span class="nv">value = </span><span class="k">new</span> <span class="nb">String</span><span class="p">(</span><span class="s1">'{'</span><span class="p">)</span>
|
95
|
-
<span class="nv">value.generated = </span><span class="kc">yes</span>
|
96
|
-
<span class="nv">tok = </span><span class="p">[</span><span class="s1">'{'</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]]</span>
|
97
|
-
<span class="nv">tok.generated = </span><span class="kc">yes</span>
|
98
|
-
<span class="nx">tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">idx</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">tok</span>
|
99
|
-
<span class="nx">@detectEnd</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">,</span> <span class="nx">condition</span><span class="p">,</span> <span class="nx">action</span>
|
100
|
-
<span class="mi">2</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <p>Methods may be optionally called without parentheses, for simple cases.
|
101
|
-
Insert the implicit parentheses here, so that the parser doesn't have to
|
102
|
-
deal with them.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addImplicitParentheses</span><span class="o">:</span> <span class="o">-></span>
|
103
|
-
<span class="nv">noCall = </span><span class="kc">no</span>
|
104
|
-
<span class="nv">action = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
105
|
-
<span class="nv">idx = </span><span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'OUTDENT'</span> <span class="k">then</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">else</span> <span class="nx">i</span>
|
106
|
-
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">idx</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="s1">'CALL_END'</span><span class="p">,</span> <span class="s1">')'</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]]</span>
|
107
|
-
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">tokens</span><span class="p">)</span> <span class="o">-></span>
|
108
|
-
<span class="nv">tag = </span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
109
|
-
<span class="nv">noCall = </span><span class="kc">yes</span> <span class="k">if</span> <span class="nx">tag</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'CLASS'</span><span class="p">,</span> <span class="s1">'IF'</span><span class="p">]</span>
|
110
|
-
<span class="p">[</span><span class="nx">prev</span><span class="p">,</span> <span class="nx">current</span><span class="p">,</span> <span class="nx">next</span><span class="p">]</span> <span class="o">=</span> <span class="nx">tokens</span><span class="p">[</span><span class="nx">i</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">1</span><span class="p">]</span>
|
111
|
-
<span class="nv">callObject = </span><span class="o">not</span> <span class="nx">noCall</span> <span class="o">and</span> <span class="nx">tag</span> <span class="o">is</span> <span class="s1">'INDENT'</span> <span class="o">and</span>
|
112
|
-
<span class="nx">next</span> <span class="o">and</span> <span class="nx">next</span><span class="p">.</span><span class="nx">generated</span> <span class="o">and</span> <span class="nx">next</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'{'</span> <span class="o">and</span>
|
113
|
-
<span class="nx">prev</span> <span class="o">and</span> <span class="nx">prev</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="nx">IMPLICIT_FUNC</span>
|
114
|
-
<span class="nv">seenSingle = </span><span class="kc">no</span>
|
115
|
-
<span class="nv">noCall = </span><span class="kc">no</span> <span class="k">if</span> <span class="nx">tag</span> <span class="k">in</span> <span class="nx">LINEBREAKS</span>
|
116
|
-
<span class="nv">token.call = </span><span class="kc">yes</span> <span class="k">if</span> <span class="nx">prev</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">prev</span><span class="p">.</span><span class="nx">spaced</span> <span class="o">and</span> <span class="nx">tag</span> <span class="o">is</span> <span class="s1">'?'</span>
|
117
|
-
<span class="k">return</span> <span class="mi">1</span> <span class="nx">unless</span> <span class="nx">callObject</span> <span class="o">or</span>
|
118
|
-
<span class="nx">prev</span><span class="o">?</span><span class="p">.</span><span class="nx">spaced</span> <span class="o">and</span> <span class="p">(</span><span class="nx">prev</span><span class="p">.</span><span class="nx">call</span> <span class="o">or</span> <span class="nx">prev</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="nx">IMPLICIT_FUNC</span><span class="p">)</span> <span class="o">and</span>
|
119
|
-
<span class="p">(</span><span class="nx">tag</span> <span class="k">in</span> <span class="nx">IMPLICIT_CALL</span> <span class="o">or</span> <span class="o">not</span> <span class="p">(</span><span class="nx">token</span><span class="p">.</span><span class="nx">spaced</span> <span class="o">or</span> <span class="nx">token</span><span class="p">.</span><span class="nx">newLine</span><span class="p">)</span> <span class="o">and</span> <span class="nx">tag</span> <span class="k">in</span> <span class="nx">IMPLICIT_UNSPACED_CALL</span><span class="p">)</span>
|
120
|
-
<span class="nx">tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="s1">'CALL_START'</span><span class="p">,</span> <span class="s1">'('</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]]</span>
|
121
|
-
<span class="nx">@detectEnd</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
122
|
-
<span class="p">[</span><span class="nx">tag</span><span class="p">]</span> <span class="o">=</span> <span class="nx">token</span>
|
123
|
-
<span class="k">return</span> <span class="kc">yes</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">seenSingle</span> <span class="o">and</span> <span class="nx">token</span><span class="p">.</span><span class="nx">fromThen</span>
|
124
|
-
<span class="nv">seenSingle = </span><span class="kc">yes</span> <span class="k">if</span> <span class="nx">tag</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'IF'</span><span class="p">,</span> <span class="s1">'ELSE'</span><span class="p">,</span> <span class="s1">'->'</span><span class="p">,</span> <span class="s1">'=>'</span><span class="p">]</span>
|
125
|
-
<span class="k">return</span> <span class="kc">yes</span> <span class="k">if</span> <span class="nx">tag</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'.'</span><span class="p">,</span> <span class="s1">'?.'</span><span class="p">,</span> <span class="s1">'::'</span><span class="p">]</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'OUTDENT'</span>
|
126
|
-
<span class="o">not</span> <span class="nx">token</span><span class="p">.</span><span class="nx">generated</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">isnt</span> <span class="s1">','</span> <span class="o">and</span> <span class="nx">tag</span> <span class="k">in</span> <span class="nx">IMPLICIT_END</span> <span class="o">and</span>
|
127
|
-
<span class="p">(</span><span class="nx">tag</span> <span class="o">isnt</span> <span class="s1">'INDENT'</span> <span class="o">or</span>
|
128
|
-
<span class="p">(</span><span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">isnt</span> <span class="s1">'CLASS'</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">not</span> <span class="k">in</span> <span class="nx">IMPLICIT_BLOCK</span> <span class="o">and</span>
|
129
|
-
<span class="o">not</span> <span class="p">((</span><span class="nv">post = </span><span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span> <span class="o">and</span> <span class="nx">post</span><span class="p">.</span><span class="nx">generated</span> <span class="o">and</span> <span class="nx">post</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'{'</span><span class="p">)))</span>
|
130
|
-
<span class="p">,</span> <span class="nx">action</span>
|
131
|
-
<span class="nx">prev</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'FUNC_EXIST'</span> <span class="k">if</span> <span class="nx">prev</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'?'</span>
|
132
|
-
<span class="mi">2</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <p>Because our grammar is LALR(1), it can't handle some single-line
|
133
|
-
expressions that lack ending delimiters. The <strong>Rewriter</strong> adds the implicit
|
134
|
-
blocks, so it doesn't need to. ')' can close a single-line block,
|
135
|
-
but we need to make sure it's balanced.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addImplicitIndentation</span><span class="o">:</span> <span class="o">-></span>
|
136
|
-
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">tokens</span><span class="p">)</span> <span class="o">-></span>
|
137
|
-
<span class="p">[</span><span class="nx">tag</span><span class="p">]</span> <span class="o">=</span> <span class="nx">token</span>
|
138
|
-
<span class="k">if</span> <span class="nx">tag</span> <span class="o">is</span> <span class="s1">'TERMINATOR'</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'THEN'</span>
|
139
|
-
<span class="nx">tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">1</span>
|
140
|
-
<span class="k">return</span> <span class="mi">0</span>
|
141
|
-
<span class="k">if</span> <span class="nx">tag</span> <span class="o">is</span> <span class="s1">'ELSE'</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">isnt</span> <span class="s1">'OUTDENT'</span>
|
142
|
-
<span class="nx">tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">@indentation</span><span class="p">(</span><span class="nx">token</span><span class="p">)...</span>
|
143
|
-
<span class="k">return</span> <span class="mi">2</span>
|
144
|
-
<span class="k">if</span> <span class="nx">tag</span> <span class="o">is</span> <span class="s1">'CATCH'</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'OUTDENT'</span><span class="p">,</span> <span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'FINALLY'</span><span class="p">]</span>
|
145
|
-
<span class="nx">tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">@indentation</span><span class="p">(</span><span class="nx">token</span><span class="p">)...</span>
|
146
|
-
<span class="k">return</span> <span class="mi">4</span>
|
147
|
-
<span class="k">if</span> <span class="nx">tag</span> <span class="k">in</span> <span class="nx">SINGLE_LINERS</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">isnt</span> <span class="s1">'INDENT'</span> <span class="o">and</span>
|
148
|
-
<span class="o">not</span> <span class="p">(</span><span class="nx">tag</span> <span class="o">is</span> <span class="s1">'ELSE'</span> <span class="o">and</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'IF'</span><span class="p">)</span>
|
149
|
-
<span class="nv">starter = </span><span class="nx">tag</span>
|
150
|
-
<span class="p">[</span><span class="nx">indent</span><span class="p">,</span> <span class="nx">outdent</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@indentation</span> <span class="nx">token</span>
|
151
|
-
<span class="nv">indent.fromThen = </span><span class="kc">true</span> <span class="k">if</span> <span class="nx">starter</span> <span class="o">is</span> <span class="s1">'THEN'</span>
|
152
|
-
<span class="nv">indent.generated = outdent.generated = </span><span class="kc">true</span>
|
153
|
-
<span class="nx">tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">indent</span>
|
154
|
-
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
155
|
-
<span class="nx">token</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">isnt</span> <span class="s1">';'</span> <span class="o">and</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="nx">SINGLE_CLOSERS</span> <span class="o">and</span>
|
156
|
-
<span class="o">not</span> <span class="p">(</span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'ELSE'</span> <span class="o">and</span> <span class="nx">starter</span> <span class="o">not</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'IF'</span><span class="p">,</span> <span class="s1">'THEN'</span><span class="p">])</span>
|
157
|
-
<span class="nv">action = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
158
|
-
<span class="nx">@tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="p">(</span><span class="k">if</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">is</span> <span class="s1">','</span> <span class="k">then</span> <span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span> <span class="k">else</span> <span class="nx">i</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">outdent</span>
|
159
|
-
<span class="nx">@detectEnd</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">,</span> <span class="nx">condition</span><span class="p">,</span> <span class="nx">action</span>
|
160
|
-
<span class="nx">tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">1</span> <span class="k">if</span> <span class="nx">tag</span> <span class="o">is</span> <span class="s1">'THEN'</span>
|
161
|
-
<span class="k">return</span> <span class="mi">1</span>
|
162
|
-
<span class="k">return</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>Tag postfix conditionals as such, so that we can parse them with a
|
163
|
-
different precedence.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">tagPostfixConditionals</span><span class="o">:</span> <span class="o">-></span>
|
164
|
-
<span class="nv">condition = </span><span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'INDENT'</span><span class="p">]</span>
|
165
|
-
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
166
|
-
<span class="k">return</span> <span class="mi">1</span> <span class="nx">unless</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">is</span> <span class="s1">'IF'</span>
|
167
|
-
<span class="nv">original = </span><span class="nx">token</span>
|
168
|
-
<span class="nx">@detectEnd</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">condition</span><span class="p">,</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">-></span>
|
169
|
-
<span class="nx">original</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'POST_'</span> <span class="o">+</span> <span class="nx">original</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">isnt</span> <span class="s1">'INDENT'</span>
|
170
|
-
<span class="mi">1</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <p>Ensure that all listed pairs of tokens are correctly balanced throughout
|
171
|
-
the course of the token stream.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ensureBalance</span><span class="o">:</span> <span class="p">(</span><span class="nx">pairs</span><span class="p">)</span> <span class="o">-></span>
|
172
|
-
<span class="nv">levels = </span><span class="p">{}</span>
|
173
|
-
<span class="nv">openLine = </span><span class="p">{}</span>
|
174
|
-
<span class="k">for</span> <span class="nx">token</span> <span class="k">in</span> <span class="nx">@tokens</span>
|
175
|
-
<span class="p">[</span><span class="nx">tag</span><span class="p">]</span> <span class="o">=</span> <span class="nx">token</span>
|
176
|
-
<span class="k">for</span> <span class="p">[</span><span class="nx">open</span><span class="p">,</span> <span class="nx">close</span><span class="p">]</span> <span class="k">in</span> <span class="nx">pairs</span>
|
177
|
-
<span class="nx">levels</span><span class="p">[</span><span class="nx">open</span><span class="p">]</span> <span class="o">|=</span> <span class="mi">0</span>
|
178
|
-
<span class="k">if</span> <span class="nx">tag</span> <span class="o">is</span> <span class="nx">open</span>
|
179
|
-
<span class="nx">openLine</span><span class="p">[</span><span class="nx">open</span><span class="p">]</span> <span class="o">=</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="k">if</span> <span class="nx">levels</span><span class="p">[</span><span class="nx">open</span><span class="p">]</span><span class="o">++</span> <span class="o">is</span> <span class="mi">0</span>
|
180
|
-
<span class="k">else</span> <span class="k">if</span> <span class="nx">tag</span> <span class="o">is</span> <span class="nx">close</span> <span class="o">and</span> <span class="o">--</span><span class="nx">levels</span><span class="p">[</span><span class="nx">open</span><span class="p">]</span> <span class="o"><</span> <span class="mi">0</span>
|
181
|
-
<span class="k">throw</span> <span class="nb">Error</span> <span class="s2">"too many #{token[1]} on line #{token[2] + 1}"</span>
|
182
|
-
<span class="k">for</span> <span class="nx">open</span><span class="p">,</span> <span class="nx">level</span> <span class="k">of</span> <span class="nx">levels</span> <span class="k">when</span> <span class="nx">level</span> <span class="o">></span> <span class="mi">0</span>
|
183
|
-
<span class="k">throw</span> <span class="nb">Error</span> <span class="s2">"unclosed #{ open } on line #{openLine[open] + 1}"</span>
|
184
|
-
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p>We'd like to support syntax like this:</p>
|
185
|
-
|
186
|
-
<pre><code>el.click((event) ->
|
187
|
-
el.hide())
|
188
|
-
</code></pre>
|
189
|
-
|
190
|
-
<p>In order to accomplish this, move outdents that follow closing parens
|
191
|
-
inwards, safely. The steps to accomplish this are:</p>
|
192
|
-
|
193
|
-
<ol>
|
194
|
-
<li>Check that all paired tokens are balanced and in order.</li>
|
195
|
-
<li>Rewrite the stream with a stack: if you see an <code>EXPRESSION_START</code>, add it
|
196
|
-
to the stack. If you see an <code>EXPRESSION_END</code>, pop the stack and replace
|
197
|
-
it with the inverse of what we've just popped.</li>
|
198
|
-
<li>Keep track of "debt" for tokens that we manufacture, to make sure we end
|
199
|
-
up balanced in the end.</li>
|
200
|
-
<li>Be careful not to alter array or parentheses delimiters with overzealous
|
201
|
-
rewriting.</li>
|
202
|
-
</ol> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">rewriteClosingParens</span><span class="o">:</span> <span class="o">-></span>
|
203
|
-
<span class="nv">stack = </span><span class="p">[]</span>
|
204
|
-
<span class="nv">debt = </span><span class="p">{}</span>
|
205
|
-
<span class="nx">debt</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="k">for</span> <span class="nx">key</span> <span class="k">of</span> <span class="nx">INVERSES</span>
|
206
|
-
<span class="nx">@scanTokens</span> <span class="p">(</span><span class="nx">token</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">tokens</span><span class="p">)</span> <span class="o">-></span>
|
207
|
-
<span class="k">if</span> <span class="p">(</span><span class="nv">tag = </span><span class="nx">token</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="k">in</span> <span class="nx">EXPRESSION_START</span>
|
208
|
-
<span class="nx">stack</span><span class="p">.</span><span class="nx">push</span> <span class="nx">token</span>
|
209
|
-
<span class="k">return</span> <span class="mi">1</span>
|
210
|
-
<span class="k">return</span> <span class="mi">1</span> <span class="nx">unless</span> <span class="nx">tag</span> <span class="k">in</span> <span class="nx">EXPRESSION_END</span>
|
211
|
-
<span class="k">if</span> <span class="nx">debt</span><span class="p">[</span><span class="nv">inv = </span><span class="nx">INVERSES</span><span class="p">[</span><span class="nx">tag</span><span class="p">]]</span> <span class="o">></span> <span class="mi">0</span>
|
212
|
-
<span class="nx">debt</span><span class="p">[</span><span class="nx">inv</span><span class="p">]</span> <span class="o">-=</span> <span class="mi">1</span>
|
213
|
-
<span class="nx">tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">1</span>
|
214
|
-
<span class="k">return</span> <span class="mi">0</span>
|
215
|
-
<span class="nv">match = </span><span class="nx">stack</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span>
|
216
|
-
<span class="nv">mtag = </span><span class="nx">match</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
217
|
-
<span class="nv">oppos = </span><span class="nx">INVERSES</span><span class="p">[</span><span class="nx">mtag</span><span class="p">]</span>
|
218
|
-
<span class="k">return</span> <span class="mi">1</span> <span class="k">if</span> <span class="nx">tag</span> <span class="o">is</span> <span class="nx">oppos</span>
|
219
|
-
<span class="nx">debt</span><span class="p">[</span><span class="nx">mtag</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
|
220
|
-
<span class="nv">val = </span><span class="p">[</span><span class="nx">oppos</span><span class="p">,</span> <span class="k">if</span> <span class="nx">mtag</span> <span class="o">is</span> <span class="s1">'INDENT'</span> <span class="k">then</span> <span class="nx">match</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">else</span> <span class="nx">oppos</span><span class="p">]</span>
|
221
|
-
<span class="k">if</span> <span class="nx">@tag</span><span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span> <span class="o">is</span> <span class="nx">mtag</span>
|
222
|
-
<span class="nx">tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span> <span class="o">+</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">val</span>
|
223
|
-
<span class="nx">stack</span><span class="p">.</span><span class="nx">push</span> <span class="nx">match</span>
|
224
|
-
<span class="k">else</span>
|
225
|
-
<span class="nx">tokens</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">val</span>
|
226
|
-
<span class="mi">1</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <p>Generate the indentation tokens, based on another token on the same line.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">indentation</span><span class="o">:</span> <span class="p">(</span><span class="nx">token</span><span class="p">)</span> <span class="o">-></span>
|
227
|
-
<span class="p">[[</span><span class="s1">'INDENT'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><span class="p">]],</span> <span class="p">[</span><span class="s1">'OUTDENT'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="nx">token</span><span class="p">[</span><span class="mi">2</span><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>Look up a tag by token index.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">tag</span><span class="o">:</span> <span class="p">(</span><span class="nx">i</span><span class="p">)</span> <span class="o">-></span> <span class="nx">@tokens</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="o">?</span><span class="p">[</span><span class="mi">0</span><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> <h2>Constants</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div> <p>List of the token pairs that must be balanced.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">BALANCED_PAIRS = </span><span class="p">[</span>
|
228
|
-
<span class="p">[</span><span class="s1">'('</span><span class="p">,</span> <span class="s1">')'</span><span class="p">]</span>
|
229
|
-
<span class="p">[</span><span class="s1">'['</span><span class="p">,</span> <span class="s1">']'</span><span class="p">]</span>
|
230
|
-
<span class="p">[</span><span class="s1">'{'</span><span class="p">,</span> <span class="s1">'}'</span><span class="p">]</span>
|
231
|
-
<span class="p">[</span><span class="s1">'INDENT'</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">],</span>
|
232
|
-
<span class="p">[</span><span class="s1">'CALL_START'</span><span class="p">,</span> <span class="s1">'CALL_END'</span><span class="p">]</span>
|
233
|
-
<span class="p">[</span><span class="s1">'PARAM_START'</span><span class="p">,</span> <span class="s1">'PARAM_END'</span><span class="p">]</span>
|
234
|
-
<span class="p">[</span><span class="s1">'INDEX_START'</span><span class="p">,</span> <span class="s1">'INDEX_END'</span><span class="p">]</span>
|
235
|
-
<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>The inverse mappings of <code>BALANCED_PAIRS</code> we're trying to fix up, so we can
|
236
|
-
look things up from either end.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">INVERSES = </span><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>The tokens that signal the start/end of a balanced pair.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">EXPRESSION_START = </span><span class="p">[]</span>
|
237
|
-
<span class="nv">EXPRESSION_END = </span><span class="p">[]</span>
|
238
|
-
|
239
|
-
<span class="k">for</span> <span class="p">[</span><span class="nx">left</span><span class="p">,</span> <span class="nx">rite</span><span class="p">]</span> <span class="k">in</span> <span class="nx">BALANCED_PAIRS</span>
|
240
|
-
<span class="nx">EXPRESSION_START</span><span class="p">.</span><span class="nx">push</span> <span class="nx">INVERSES</span><span class="p">[</span><span class="nx">rite</span><span class="p">]</span> <span class="o">=</span> <span class="nx">left</span>
|
241
|
-
<span class="nx">EXPRESSION_END</span> <span class="p">.</span><span class="nx">push</span> <span class="nx">INVERSES</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span> <span class="o">=</span> <span class="nx">rite</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <p>Tokens that indicate the close of a clause of an expression.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">EXPRESSION_CLOSE = </span><span class="p">[</span><span class="s1">'CATCH'</span><span class="p">,</span> <span class="s1">'WHEN'</span><span class="p">,</span> <span class="s1">'ELSE'</span><span class="p">,</span> <span class="s1">'FINALLY'</span><span class="p">].</span><span class="nx">concat</span> <span class="nx">EXPRESSION_END</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <p>Tokens that, if followed by an <code>IMPLICIT_CALL</code>, indicate a function invocation.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_FUNC = </span><span class="p">[</span><span class="s1">'IDENTIFIER'</span><span class="p">,</span> <span class="s1">'SUPER'</span><span class="p">,</span> <span class="s1">')'</span><span class="p">,</span> <span class="s1">'CALL_END'</span><span class="p">,</span> <span class="s1">']'</span><span class="p">,</span> <span class="s1">'INDEX_END'</span><span class="p">,</span> <span class="s1">'@'</span><span class="p">,</span> <span class="s1">'THIS'</span><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>If preceded by an <code>IMPLICIT_FUNC</code>, indicates a function invocation.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_CALL = </span><span class="p">[</span>
|
242
|
-
<span class="s1">'IDENTIFIER'</span><span class="p">,</span> <span class="s1">'NUMBER'</span><span class="p">,</span> <span class="s1">'STRING'</span><span class="p">,</span> <span class="s1">'JS'</span><span class="p">,</span> <span class="s1">'REGEX'</span><span class="p">,</span> <span class="s1">'NEW'</span><span class="p">,</span> <span class="s1">'PARAM_START'</span><span class="p">,</span> <span class="s1">'CLASS'</span>
|
243
|
-
<span class="s1">'IF'</span><span class="p">,</span> <span class="s1">'TRY'</span><span class="p">,</span> <span class="s1">'SWITCH'</span><span class="p">,</span> <span class="s1">'THIS'</span><span class="p">,</span> <span class="s1">'BOOL'</span><span class="p">,</span> <span class="s1">'UNARY'</span><span class="p">,</span> <span class="s1">'SUPER'</span>
|
244
|
-
<span class="s1">'@'</span><span class="p">,</span> <span class="s1">'->'</span><span class="p">,</span> <span class="s1">'=>'</span><span class="p">,</span> <span class="s1">'['</span><span class="p">,</span> <span class="s1">'('</span><span class="p">,</span> <span class="s1">'{'</span><span class="p">,</span> <span class="s1">'--'</span><span class="p">,</span> <span class="s1">'++'</span>
|
245
|
-
<span class="p">]</span>
|
246
|
-
|
247
|
-
<span class="nv">IMPLICIT_UNSPACED_CALL = </span><span class="p">[</span><span class="s1">'+'</span><span class="p">,</span> <span class="s1">'-'</span><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>Tokens indicating that the implicit call must enclose a block of expressions.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_BLOCK = </span><span class="p">[</span><span class="s1">'->'</span><span class="p">,</span> <span class="s1">'=>'</span><span class="p">,</span> <span class="s1">'{'</span><span class="p">,</span> <span class="s1">'['</span><span class="p">,</span> <span class="s1">','</span><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>Tokens that always mark the end of an implicit call for single-liners.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IMPLICIT_END = </span><span class="p">[</span><span class="s1">'POST_IF'</span><span class="p">,</span> <span class="s1">'FOR'</span><span class="p">,</span> <span class="s1">'WHILE'</span><span class="p">,</span> <span class="s1">'UNTIL'</span><span class="p">,</span> <span class="s1">'WHEN'</span><span class="p">,</span> <span class="s1">'BY'</span><span class="p">,</span> <span class="s1">'LOOP'</span><span class="p">,</span> <span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'INDENT'</span><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>Single-line flavors of block expressions that have unclosed endings.
|
248
|
-
The grammar can't disambiguate them, so we insert the implicit indentation.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">SINGLE_LINERS = </span><span class="p">[</span><span class="s1">'ELSE'</span><span class="p">,</span> <span class="s1">'->'</span><span class="p">,</span> <span class="s1">'=>'</span><span class="p">,</span> <span class="s1">'TRY'</span><span class="p">,</span> <span class="s1">'FINALLY'</span><span class="p">,</span> <span class="s1">'THEN'</span><span class="p">]</span>
|
249
|
-
<span class="nv">SINGLE_CLOSERS = </span><span class="p">[</span><span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'CATCH'</span><span class="p">,</span> <span class="s1">'FINALLY'</span><span class="p">,</span> <span class="s1">'ELSE'</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">,</span> <span class="s1">'LEADING_WHEN'</span><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>Tokens that end a line.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">LINEBREAKS = </span><span class="p">[</span><span class="s1">'TERMINATOR'</span><span class="p">,</span> <span class="s1">'INDENT'</span><span class="p">,</span> <span class="s1">'OUTDENT'</span><span class="p">]</span>
|
250
|
-
|
251
|
-
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|
@@ -1,54 +0,0 @@
|
|
1
|
-
<!DOCTYPE html> <html> <head> <title>scope.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To … <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="browser.html"> browser.coffee </a> <a class="source" href="cake.html"> cake.coffee </a> <a class="source" href="coffee-script.html"> coffee-script.coffee </a> <a class="source" href="command.html"> command.coffee </a> <a class="source" href="grammar.html"> grammar.coffee </a> <a class="source" href="helpers.html"> helpers.coffee </a> <a class="source" href="index.html"> index.coffee </a> <a class="source" href="lexer.html"> lexer.coffee </a> <a class="source" href="nodes.html"> nodes.coffee </a> <a class="source" href="optparse.html"> optparse.coffee </a> <a class="source" href="repl.html"> repl.coffee </a> <a class="source" href="rewriter.html"> rewriter.coffee </a> <a class="source" href="scope.html"> scope.coffee </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> scope.coffee </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> <p>The <strong>Scope</strong> class regulates lexical scoping within CoffeeScript. As you
|
2
|
-
generate code, you create a tree of scopes in the same shape as the nested
|
3
|
-
function bodies. Each scope knows about the variables declared within it,
|
4
|
-
and has a reference to its parent enclosing scope. In this way, we know which
|
5
|
-
variables are new and need to be declared with <code>var</code>, and which are shared
|
6
|
-
with the outside.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>Import the helpers we plan to use.</p> </td> <td class="code"> <div class="highlight"><pre><span class="p">{</span><span class="nx">extend</span><span class="p">,</span> <span class="nx">last</span><span class="p">}</span> <span class="o">=</span> <span class="nx">require</span> <span class="s1">'./helpers'</span>
|
7
|
-
|
8
|
-
<span class="nv">exports.Scope = </span><span class="nx">class</span> <span class="nx">Scope</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <p>The top-level <strong>Scope</strong> object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@root</span><span class="o">:</span> <span class="kc">null</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>Initialize a scope with its parent, for lookups up the chain,
|
9
|
-
as well as a reference to the <strong>Expressions</strong> node is belongs to, which is
|
10
|
-
where it should declare its variables, and a reference to the function that
|
11
|
-
it wraps.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">constructor</span><span class="o">:</span><span class="p">(</span><span class="nx">@parent</span><span class="p">,</span> <span class="nx">@expressions</span><span class="p">,</span> <span class="nx">@method</span><span class="p">)</span> <span class="o">-></span>
|
12
|
-
<span class="vi">@variables = </span><span class="p">[{</span><span class="nx">name</span><span class="o">:</span> <span class="s1">'arguments'</span><span class="p">,</span> <span class="nx">type</span><span class="o">:</span> <span class="s1">'arguments'</span><span class="p">}]</span>
|
13
|
-
<span class="vi">@positions = </span><span class="p">{}</span>
|
14
|
-
<span class="nv">Scope.root = </span><span class="k">this</span> <span class="nx">unless</span> <span class="nx">@parent</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>Adds a new variable or overrides an existing one.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">add</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">type</span><span class="p">,</span> <span class="nx">immediate</span><span class="p">)</span> <span class="o">-></span>
|
15
|
-
<span class="k">return</span> <span class="nx">@parent</span><span class="p">.</span><span class="nx">add</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">type</span><span class="p">,</span> <span class="nx">immediate</span> <span class="k">if</span> <span class="nx">@shared</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">immediate</span>
|
16
|
-
<span class="k">if</span> <span class="k">typeof</span> <span class="p">(</span><span class="nv">pos = </span><span class="nx">@positions</span><span class="p">[</span><span class="nx">name</span><span class="p">])</span> <span class="o">is</span> <span class="s1">'number'</span>
|
17
|
-
<span class="nx">@variables</span><span class="p">[</span><span class="nx">pos</span><span class="p">].</span><span class="nv">type = </span><span class="nx">type</span>
|
18
|
-
<span class="k">else</span>
|
19
|
-
<span class="nx">@positions</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@variables</span><span class="p">.</span><span class="nx">push</span><span class="p">({</span><span class="nx">name</span><span class="p">,</span> <span class="nx">type</span><span class="p">})</span> <span class="o">-</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>Look up a variable name in lexical scope, and declare it if it does not
|
20
|
-
already exist.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">find</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">-></span>
|
21
|
-
<span class="k">return</span> <span class="kc">yes</span> <span class="k">if</span> <span class="nx">@check</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">options</span>
|
22
|
-
<span class="nx">@add</span> <span class="nx">name</span><span class="p">,</span> <span class="s1">'var'</span>
|
23
|
-
<span class="kc">no</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>Reserve a variable name as originating from a function parameter for this
|
24
|
-
scope. No <code>var</code> required for internal references.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">parameter</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
25
|
-
<span class="k">return</span> <span class="k">if</span> <span class="nx">@shared</span> <span class="o">and</span> <span class="nx">@parent</span><span class="p">.</span><span class="nx">check</span> <span class="nx">name</span><span class="p">,</span> <span class="kc">yes</span>
|
26
|
-
<span class="nx">@add</span> <span class="nx">name</span><span class="p">,</span> <span class="s1">'param'</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>Just check to see if a variable has already been declared, without reserving,
|
27
|
-
walks up to the root scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">check</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">immediate</span><span class="p">)</span> <span class="o">-></span>
|
28
|
-
<span class="nv">found = </span><span class="o">!!</span><span class="nx">@type</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span>
|
29
|
-
<span class="k">return</span> <span class="nx">found</span> <span class="k">if</span> <span class="nx">found</span> <span class="o">or</span> <span class="nx">immediate</span>
|
30
|
-
<span class="o">!!</span><span class="nx">@parent</span><span class="o">?</span><span class="p">.</span><span class="nx">check</span> <span class="nx">name</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <p>Generate a temporary variable name at the given index.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">temporary</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">index</span><span class="p">)</span> <span class="o">-></span>
|
31
|
-
<span class="k">if</span> <span class="nx">name</span><span class="p">.</span><span class="nx">length</span> <span class="o">></span> <span class="mi">1</span>
|
32
|
-
<span class="s1">'_'</span> <span class="o">+</span> <span class="nx">name</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">index</span> <span class="o">></span> <span class="mi">1</span> <span class="k">then</span> <span class="nx">index</span> <span class="k">else</span> <span class="s1">''</span>
|
33
|
-
<span class="k">else</span>
|
34
|
-
<span class="s1">'_'</span> <span class="o">+</span> <span class="p">(</span><span class="nx">index</span> <span class="o">+</span> <span class="nb">parseInt</span> <span class="nx">name</span><span class="p">,</span> <span class="mi">36</span><span class="p">).</span><span class="nx">toString</span><span class="p">(</span><span class="mi">36</span><span class="p">).</span><span class="nx">replace</span> <span class="sr">/\d/g</span><span class="p">,</span> <span class="s1">'a'</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>Gets the type of a variable.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">type</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
35
|
-
<span class="k">return</span> <span class="nx">v</span><span class="p">.</span><span class="nx">type</span> <span class="k">for</span> <span class="nx">v</span> <span class="k">in</span> <span class="nx">@variables</span> <span class="k">when</span> <span class="nx">v</span><span class="p">.</span><span class="nx">name</span> <span class="o">is</span> <span class="nx">name</span>
|
36
|
-
<span class="kc">null</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <p>If we need to store an intermediate result, find an available name for a
|
37
|
-
compiler-generated variable. <code>_var</code>, <code>_var2</code>, and so on...</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">freeVariable</span><span class="o">:</span> <span class="p">(</span><span class="nx">type</span><span class="p">)</span> <span class="o">-></span>
|
38
|
-
<span class="nv">index = </span><span class="mi">0</span>
|
39
|
-
<span class="nx">index</span><span class="o">++</span> <span class="k">while</span> <span class="nx">@check</span><span class="p">((</span><span class="nv">temp = </span><span class="nx">@temporary</span> <span class="nx">type</span><span class="p">,</span> <span class="nx">index</span><span class="p">),</span> <span class="kc">true</span><span class="p">)</span>
|
40
|
-
<span class="nx">@add</span> <span class="nx">temp</span><span class="p">,</span> <span class="s1">'var'</span><span class="p">,</span> <span class="kc">yes</span>
|
41
|
-
<span class="nx">temp</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <p>Ensure that an assignment is made at the top of this scope
|
42
|
-
(or at the top-level scope, if requested).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">assign</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="o">-></span>
|
43
|
-
<span class="nx">@add</span> <span class="nx">name</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">assigned</span><span class="o">:</span> <span class="kc">true</span>
|
44
|
-
<span class="vi">@hasAssignments = </span><span class="kc">yes</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>Does this scope have any declared variables?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">hasDeclarations</span><span class="o">:</span> <span class="o">-></span>
|
45
|
-
<span class="o">!!</span><span class="nx">@declaredVariables</span><span class="p">().</span><span class="nx">length</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 list of variables first declared in this scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">declaredVariables</span><span class="o">:</span> <span class="o">-></span>
|
46
|
-
<span class="nv">realVars = </span><span class="p">[]</span>
|
47
|
-
<span class="nv">tempVars = </span><span class="p">[]</span>
|
48
|
-
<span class="k">for</span> <span class="nx">v</span> <span class="k">in</span> <span class="nx">@variables</span> <span class="k">when</span> <span class="nx">v</span><span class="p">.</span><span class="nx">type</span> <span class="o">is</span> <span class="s1">'var'</span>
|
49
|
-
<span class="p">(</span><span class="k">if</span> <span class="nx">v</span><span class="p">.</span><span class="nx">name</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'_'</span> <span class="k">then</span> <span class="nx">tempVars</span> <span class="k">else</span> <span class="nx">realVars</span><span class="p">).</span><span class="nx">push</span> <span class="nx">v</span><span class="p">.</span><span class="nx">name</span>
|
50
|
-
<span class="nx">realVars</span><span class="p">.</span><span class="nx">sort</span><span class="p">().</span><span class="nx">concat</span> <span class="nx">tempVars</span><span class="p">.</span><span class="nx">sort</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>Return the list of assignments that are supposed to be made at the top
|
51
|
-
of this scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">assignedVariables</span><span class="o">:</span> <span class="o">-></span>
|
52
|
-
<span class="s2">"#{v.name} = #{v.type.value}"</span> <span class="k">for</span> <span class="nx">v</span> <span class="k">in</span> <span class="nx">@variables</span> <span class="k">when</span> <span class="nx">v</span><span class="p">.</span><span class="nx">type</span><span class="p">.</span><span class="nx">assigned</span>
|
53
|
-
|
54
|
-
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|
@@ -1,295 +0,0 @@
|
|
1
|
-
<!DOCTYPE html> <html> <head> <title>underscore.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> underscore.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p><strong>Underscore.coffee
|
2
|
-
(c) 2010 Jeremy Ashkenas, DocumentCloud Inc.</strong>
|
3
|
-
Underscore is freely distributable under the terms of the
|
4
|
-
<a href="http://en.wikipedia.org/wiki/MIT_License">MIT license</a>.
|
5
|
-
Portions of Underscore are inspired by or borrowed from
|
6
|
-
<a href="http://prototypejs.org/api">Prototype.js</a>, Oliver Steele's
|
7
|
-
<a href="http://osteele.com">Functional</a>, and John Resig's
|
8
|
-
<a href="http://ejohn.org">Micro-Templating</a>.
|
9
|
-
For all details and documentation:
|
10
|
-
http://documentcloud.github.com/underscore/</p> </td> <td class="code"> <div class="highlight"><pre></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="nv">root = </span><span class="k">this</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="nv">previousUnderscore = </span><span class="nx">root</span><span class="p">.</span><span class="nx">_</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 thrown to break out of a loop iteration.
|
11
|
-
<code>StopIteration</code> is SOP on Mozilla.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">breaker = </span><span class="k">if</span> <span class="k">typeof</span><span class="p">(</span><span class="nx">StopIteration</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'undefined'</span> <span class="k">then</span> <span class="s1">'__break__'</span> <span class="k">else</span> <span class="nx">StopIteration</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>Helper function to escape <strong>RegExp</strong> contents, because JS doesn't have one.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">escapeRegExp = </span><span class="p">(</span><span class="nx">string</span><span class="p">)</span> <span class="o">-></span> <span class="nx">string</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/([.*+?^${}()|[\]\/\\])/g</span><span class="p">,</span> <span class="s1">'\\$1'</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>Save bytes in the minified (but not gzipped) version:</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">ArrayProto = </span><span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span>
|
12
|
-
<span class="nv">ObjProto = </span><span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>Create quick reference variables for speed access to core prototypes.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">slice = </span><span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">slice</span>
|
13
|
-
<span class="nv">unshift = </span><span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">unshift</span>
|
14
|
-
<span class="nv">toString = </span><span class="nx">ObjProto</span><span class="p">.</span><span class="nx">toString</span>
|
15
|
-
<span class="nv">hasOwnProperty = </span><span class="nx">ObjProto</span><span class="p">.</span><span class="nx">hasOwnProperty</span>
|
16
|
-
<span class="nv">propertyIsEnumerable = </span><span class="nx">ObjProto</span><span class="p">.</span><span class="nx">propertyIsEnumerable</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <p>All <strong>ECMA5</strong> native implementations we hope to use are declared here.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">nativeForEach = </span><span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">forEach</span>
|
17
|
-
<span class="nv">nativeMap = </span><span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">map</span>
|
18
|
-
<span class="nv">nativeReduce = </span><span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">reduce</span>
|
19
|
-
<span class="nv">nativeReduceRight = </span><span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">reduceRight</span>
|
20
|
-
<span class="nv">nativeFilter = </span><span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">filter</span>
|
21
|
-
<span class="nv">nativeEvery = </span><span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">every</span>
|
22
|
-
<span class="nv">nativeSome = </span><span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">some</span>
|
23
|
-
<span class="nv">nativeIndexOf = </span><span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">indexOf</span>
|
24
|
-
<span class="nv">nativeLastIndexOf = </span><span class="nx">ArrayProto</span><span class="p">.</span><span class="nx">lastIndexOf</span>
|
25
|
-
<span class="nv">nativeIsArray = </span><span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span>
|
26
|
-
<span class="nv">nativeKeys = </span><span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>Create a safe reference to the Underscore object for use below.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_ = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="k">new</span> <span class="nx">wrapper</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <p>Export the Underscore object for <strong>CommonJS</strong>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">if</span> <span class="k">typeof</span><span class="p">(</span><span class="nx">exports</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">'undefined'</span> <span class="k">then</span> <span class="nv">exports._ = </span><span class="nx">_</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <p>Export Underscore to global scope.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">root._ = </span><span class="nx">_</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>Current version.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.VERSION = </span><span class="s1">'1.1.0'</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <h2>Collection Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p>The cornerstone, an <strong>each</strong> implementation.
|
27
|
-
Handles objects implementing <strong>forEach</strong>, arrays, and raw objects.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.each = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
28
|
-
<span class="k">try</span>
|
29
|
-
<span class="k">if</span> <span class="nx">nativeForEach</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">forEach</span> <span class="o">is</span> <span class="nx">nativeForEach</span>
|
30
|
-
<span class="nx">obj</span><span class="p">.</span><span class="nx">forEach</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span>
|
31
|
-
<span class="k">else</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isNumber</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span>
|
32
|
-
<span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">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="k">for</span> <span class="nx">i</span> <span class="k">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">...</span><span class="nx">obj</span><span class="p">.</span><span class="nx">length</span><span class="p">]</span>
|
33
|
-
<span class="k">else</span>
|
34
|
-
<span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">val</span><span class="p">,</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">obj</span> <span class="k">for</span> <span class="nx">own</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">obj</span>
|
35
|
-
<span class="k">catch</span> <span class="nx">e</span>
|
36
|
-
<span class="k">throw</span> <span class="nx">e</span> <span class="k">if</span> <span class="nx">e</span> <span class="o">isnt</span> <span class="nx">breaker</span>
|
37
|
-
<span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <p>Return the results of applying the iterator to each element. Use JavaScript
|
38
|
-
1.6's version of <strong>map</strong>, if possible.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.map = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
39
|
-
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="k">if</span> <span class="nx">nativeMap</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">map</span> <span class="o">is</span> <span class="nx">nativeMap</span>
|
40
|
-
<span class="nv">results = </span><span class="p">[]</span>
|
41
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
42
|
-
<span class="nx">results</span><span class="p">.</span><span class="nx">push</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span>
|
43
|
-
<span class="nx">results</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div> <p><strong>Reduce</strong> builds up a single result from a list of values. Also known as
|
44
|
-
<strong>inject</strong>, or <strong>foldl</strong>. Uses JavaScript 1.8's version of <strong>reduce</strong>, if possible.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.reduce = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">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="o">-></span>
|
45
|
-
<span class="k">if</span> <span class="nx">nativeReduce</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduce</span> <span class="o">is</span> <span class="nx">nativeReduce</span>
|
46
|
-
<span class="nv">iterator = </span><span class="nx">_</span><span class="p">.</span><span class="nx">bind</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span> <span class="k">if</span> <span class="nx">context</span>
|
47
|
-
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduce</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">memo</span>
|
48
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
49
|
-
<span class="nv">memo = </span><span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">memo</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span>
|
50
|
-
<span class="nx">memo</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <p>The right-associative version of <strong>reduce</strong>, also known as <strong>foldr</strong>. Uses
|
51
|
-
JavaScript 1.8's version of <strong>reduceRight</strong>, if available.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.reduceRight = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">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="o">-></span>
|
52
|
-
<span class="k">if</span> <span class="nx">nativeReduceRight</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduceRight</span> <span class="o">is</span> <span class="nx">nativeReduceRight</span>
|
53
|
-
<span class="nv">iterator = </span><span class="nx">_</span><span class="p">.</span><span class="nx">bind</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span> <span class="k">if</span> <span class="nx">context</span>
|
54
|
-
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">reduceRight</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">memo</span>
|
55
|
-
<span class="nv">reversed = </span><span class="nx">_</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">toArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">)).</span><span class="nx">reverse</span><span class="p">()</span>
|
56
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">reduce</span> <span class="nx">reversed</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></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div> <p>Return the first value which passes a truth test.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.detect = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
57
|
-
<span class="nv">result = </span><span class="kc">null</span>
|
58
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
59
|
-
<span class="k">if</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span>
|
60
|
-
<span class="nv">result = </span><span class="nx">value</span>
|
61
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">breakLoop</span><span class="p">()</span>
|
62
|
-
<span class="nx">result</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</a> </div> <p>Return all the elements that pass a truth test. Use JavaScript 1.6's
|
63
|
-
<strong>filter</strong>, if it exists.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.filter = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
64
|
-
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">filter</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span> <span class="k">if</span> <span class="nx">nativeFilter</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">filter</span> <span class="o">is</span> <span class="nx">nativeFilter</span>
|
65
|
-
<span class="nv">results = </span><span class="p">[]</span>
|
66
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
67
|
-
<span class="nx">results</span><span class="p">.</span><span class="nx">push</span> <span class="nx">value</span> <span class="k">if</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span>
|
68
|
-
<span class="nx">results</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">¶</a> </div> <p>Return all the elements for which a truth test fails.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.reject = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
69
|
-
<span class="nv">results = </span><span class="p">[]</span>
|
70
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
71
|
-
<span class="nx">results</span><span class="p">.</span><span class="nx">push</span> <span class="nx">value</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span>
|
72
|
-
<span class="nx">results</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 whether all of the elements match a truth test. Delegate to
|
73
|
-
JavaScript 1.6's <strong>every</strong>, if it is present.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.every = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
74
|
-
<span class="nx">iterator</span> <span class="o">||=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span>
|
75
|
-
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">every</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span> <span class="k">if</span> <span class="nx">nativeEvery</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">every</span> <span class="o">is</span> <span class="nx">nativeEvery</span>
|
76
|
-
<span class="nv">result = </span><span class="kc">true</span>
|
77
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
78
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">breakLoop</span><span class="p">()</span> <span class="nx">unless</span> <span class="p">(</span><span class="nv">result = </span><span class="nx">result</span> <span class="o">and</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">))</span>
|
79
|
-
<span class="nx">result</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <p>Determine if at least one element in the object matches a truth test. Use
|
80
|
-
JavaScript 1.6's <strong>some</strong>, if it exists.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.some = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
81
|
-
<span class="nx">iterator</span> <span class="o">||=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span>
|
82
|
-
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">some</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span> <span class="k">if</span> <span class="nx">nativeSome</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">some</span> <span class="o">is</span> <span class="nx">nativeSome</span>
|
83
|
-
<span class="nv">result = </span><span class="kc">false</span>
|
84
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
85
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">breakLoop</span><span class="p">()</span> <span class="k">if</span> <span class="p">(</span><span class="nv">result = </span><span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">))</span>
|
86
|
-
<span class="nx">result</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">¶</a> </div> <p>Determine if a given value is included in the array or object,
|
87
|
-
based on <code>===</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.include = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">target</span><span class="p">)</span> <span class="o">-></span>
|
88
|
-
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">target</span><span class="p">)</span> <span class="o">isnt</span> <span class="o">-</span><span class="mi">1</span> <span class="k">if</span> <span class="nx">nativeIndexOf</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">indexOf</span> <span class="o">is</span> <span class="nx">nativeIndexOf</span>
|
89
|
-
<span class="k">return</span> <span class="kc">true</span> <span class="k">for</span> <span class="nx">own</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">obj</span> <span class="k">when</span> <span class="nx">val</span> <span class="o">is</span> <span class="nx">target</span>
|
90
|
-
<span class="kc">false</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">¶</a> </div> <p>Invoke a method with arguments on every item in a collection.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.invoke = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">method</span><span class="p">)</span> <span class="o">-></span>
|
91
|
-
<span class="nv">args = </span><span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span><span class="p">,</span> <span class="mi">2</span>
|
92
|
-
<span class="p">(</span><span class="k">if</span> <span class="nx">method</span> <span class="k">then</span> <span class="nx">val</span><span class="p">[</span><span class="nx">method</span><span class="p">]</span> <span class="k">else</span> <span class="nx">val</span><span class="p">).</span><span class="nx">apply</span><span class="p">(</span><span class="nx">val</span><span class="p">,</span> <span class="nx">args</span><span class="p">)</span> <span class="k">for</span> <span class="nx">val</span> <span class="k">in</span> <span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">¶</a> </div> <p>Convenience version of a common use case of <strong>map</strong>: fetching a property.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.pluck = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">key</span><span class="p">)</span> <span class="o">-></span>
|
93
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="o">-></span> <span class="nx">val</span><span class="p">[</span><span class="nx">key</span><span class="p">])</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">¶</a> </div> <p>Return the maximum item or (item-based computation).</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.max = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
94
|
-
<span class="k">return</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">max</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nb">Math</span><span class="p">,</span> <span class="nx">obj</span><span class="p">)</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">iterator</span> <span class="o">and</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span>
|
95
|
-
<span class="nv">result = </span><span class="nx">computed</span><span class="o">:</span> <span class="o">-</span><span class="kc">Infinity</span>
|
96
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
97
|
-
<span class="nv">computed = </span><span class="k">if</span> <span class="nx">iterator</span> <span class="k">then</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="k">else</span> <span class="nx">value</span>
|
98
|
-
<span class="nx">computed</span> <span class="o">>=</span> <span class="nx">result</span><span class="p">.</span><span class="nx">computed</span> <span class="o">and</span> <span class="p">(</span><span class="nv">result = </span><span class="p">{</span><span class="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>
|
99
|
-
<span class="nx">result</span><span class="p">.</span><span class="nx">value</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="nv">_.min = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
100
|
-
<span class="k">return</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">min</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nb">Math</span><span class="p">,</span> <span class="nx">obj</span><span class="p">)</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">iterator</span> <span class="o">and</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span>
|
101
|
-
<span class="nv">result = </span><span class="nx">computed</span><span class="o">:</span> <span class="kc">Infinity</span>
|
102
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
103
|
-
<span class="nv">computed = </span><span class="k">if</span> <span class="nx">iterator</span> <span class="k">then</span> <span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="k">else</span> <span class="nx">value</span>
|
104
|
-
<span class="nx">computed</span> <span class="o"><</span> <span class="nx">result</span><span class="p">.</span><span class="nx">computed</span> <span class="o">and</span> <span class="p">(</span><span class="nv">result = </span><span class="p">{</span><span class="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>
|
105
|
-
<span class="nx">result</span><span class="p">.</span><span class="nx">value</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">¶</a> </div> <p>Sort the object's values by a criterion produced by an iterator.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.sortBy = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
106
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">pluck</span><span class="p">(((</span><span class="nx">_</span><span class="p">.</span><span class="nx">map</span> <span class="nx">obj</span><span class="p">,</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
107
|
-
<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">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>
|
108
|
-
<span class="p">).</span><span class="nx">sort</span><span class="p">((</span><span class="nx">left</span><span class="p">,</span> <span class="nx">right</span><span class="p">)</span> <span class="o">-></span>
|
109
|
-
<span class="nv">a = </span><span class="nx">left</span><span class="p">.</span><span class="nx">criteria</span><span class="p">;</span> <span class="nv">b = </span><span class="nx">right</span><span class="p">.</span><span class="nx">criteria</span>
|
110
|
-
<span class="k">if</span> <span class="nx">a</span> <span class="o"><</span> <span class="nx">b</span> <span class="k">then</span> <span class="o">-</span><span class="mi">1</span> <span class="k">else</span> <span class="k">if</span> <span class="nx">a</span> <span class="o">></span> <span class="nx">b</span> <span class="k">then</span> <span class="mi">1</span> <span class="k">else</span> <span class="mi">0</span>
|
111
|
-
<span class="p">)),</span> <span class="s1">'value'</span><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>Use a comparator function to figure out at what index an object should
|
112
|
-
be inserted so as to maintain order. Uses binary search.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.sortedIndex = </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">)</span> <span class="o">-></span>
|
113
|
-
<span class="nx">iterator</span> <span class="o">||=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span>
|
114
|
-
<span class="nv">low = </span> <span class="mi">0</span>
|
115
|
-
<span class="nv">high = </span><span class="nx">array</span><span class="p">.</span><span class="nx">length</span>
|
116
|
-
<span class="k">while</span> <span class="nx">low</span> <span class="o"><</span> <span class="nx">high</span>
|
117
|
-
<span class="nv">mid = </span><span class="p">(</span><span class="nx">low</span> <span class="o">+</span> <span class="nx">high</span><span class="p">)</span> <span class="o">>></span> <span class="mi">1</span>
|
118
|
-
<span class="k">if</span> <span class="nx">iterator</span><span class="p">(</span><span class="nx">array</span><span class="p">[</span><span class="nx">mid</span><span class="p">])</span> <span class="o"><</span> <span class="nx">iterator</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="k">then</span> <span class="nv">low = </span><span class="nx">mid</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">else</span> <span class="nv">high = </span><span class="nx">mid</span>
|
119
|
-
<span class="nx">low</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">¶</a> </div> <p>Convert anything iterable into a real, live array.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.toArray = </span><span class="p">(</span><span class="nx">iterable</span><span class="p">)</span> <span class="o">-></span>
|
120
|
-
<span class="k">return</span> <span class="p">[]</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">iterable</span><span class="p">)</span>
|
121
|
-
<span class="k">return</span> <span class="nx">iterable</span><span class="p">.</span><span class="nx">toArray</span><span class="p">()</span> <span class="k">if</span> <span class="p">(</span><span class="nx">iterable</span><span class="p">.</span><span class="nx">toArray</span><span class="p">)</span>
|
122
|
-
<span class="k">return</span> <span class="nx">iterable</span> <span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">iterable</span><span class="p">))</span>
|
123
|
-
<span class="k">return</span> <span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">iterable</span><span class="p">)</span> <span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">isArguments</span><span class="p">(</span><span class="nx">iterable</span><span class="p">))</span>
|
124
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">values</span><span class="p">(</span><span class="nx">iterable</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">¶</a> </div> <p>Return the number of elements in an object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.size = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">_</span><span class="p">.</span><span class="nx">toArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">).</span><span class="nx">length</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</a> </div> <h2>Array Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">¶</a> </div> <p>Get the first element of an array. Passing <code>n</code> will return the first N
|
125
|
-
values in the array. Aliased as <strong>head</strong>. The <code>guard</code> check allows it to work
|
126
|
-
with <strong>map</strong>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.first = </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">n</span><span class="p">,</span> <span class="nx">guard</span><span class="p">)</span> <span class="o">-></span>
|
127
|
-
<span class="k">if</span> <span class="nx">n</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">guard</span> <span class="k">then</span> <span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">n</span><span class="p">)</span> <span class="k">else</span> <span class="nx">array</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">¶</a> </div> <p>Returns everything but the first entry of the array. Aliased as <strong>tail</strong>.
|
128
|
-
Especially useful on the arguments object. Passing an <code>index</code> will return
|
129
|
-
the rest of the values in the array from that index onward. The <code>guard</code>
|
130
|
-
check allows it to work with <strong>map</strong>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.rest = </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">index</span><span class="p">,</span> <span class="nx">guard</span><span class="p">)</span> <span class="o">-></span>
|
131
|
-
<span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isUndefined</span><span class="p">(</span><span class="nx">index</span><span class="p">)</span> <span class="o">or</span> <span class="nx">guard</span> <span class="k">then</span> <span class="mi">1</span> <span class="k">else</span> <span class="nx">index</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">¶</a> </div> <p>Get the last element of an array.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.last = </span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="o">-></span> <span class="nx">array</span><span class="p">[</span><span class="nx">array</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">¶</a> </div> <p>Trim out all falsy values from an array.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.compact = </span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="o">-></span> <span class="nx">item</span> <span class="k">for</span> <span class="nx">item</span> <span class="k">in</span> <span class="nx">array</span> <span class="k">when</span> <span class="nx">item</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">¶</a> </div> <p>Return a completely flattened version of an array.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.flatten = </span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="o">-></span>
|
132
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">reduce</span> <span class="nx">array</span><span class="p">,</span> <span class="p">(</span><span class="nx">memo</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="o">-></span>
|
133
|
-
<span class="k">return</span> <span class="nx">memo</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">flatten</span><span class="p">(</span><span class="nx">value</span><span class="p">))</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span> <span class="nx">value</span>
|
134
|
-
<span class="nx">memo</span><span class="p">.</span><span class="nx">push</span> <span class="nx">value</span>
|
135
|
-
<span class="nx">memo</span>
|
136
|
-
<span class="p">,</span> <span class="p">[]</span></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-39">¶</a> </div> <p>Return a version of the array that does not contain the specified value(s).</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.without = </span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="o">-></span>
|
137
|
-
<span class="nv">values = </span><span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span>
|
138
|
-
<span class="nx">val</span> <span class="k">for</span> <span class="nx">val</span> <span class="k">in</span> <span class="nx">_</span><span class="p">.</span><span class="nx">toArray</span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="k">when</span> <span class="o">not</span> <span class="nx">_</span><span class="p">.</span><span class="nx">include</span> <span class="nx">values</span><span class="p">,</span> <span class="nx">val</span></pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-40">¶</a> </div> <p>Produce a duplicate-free version of the array. If the array has already
|
139
|
-
been sorted, you have the option of using a faster algorithm.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.uniq = </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">isSorted</span><span class="p">)</span> <span class="o">-></span>
|
140
|
-
<span class="nv">memo = </span><span class="p">[]</span>
|
141
|
-
<span class="k">for</span> <span class="nx">el</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">_</span><span class="p">.</span><span class="nx">toArray</span> <span class="nx">array</span>
|
142
|
-
<span class="nx">memo</span><span class="p">.</span><span class="nx">push</span> <span class="nx">el</span> <span class="k">if</span> <span class="nx">i</span> <span class="o">is</span> <span class="mi">0</span> <span class="o">||</span> <span class="p">(</span><span class="k">if</span> <span class="nx">isSorted</span> <span class="o">is</span> <span class="kc">true</span> <span class="k">then</span> <span class="nx">_</span><span class="p">.</span><span class="nx">last</span><span class="p">(</span><span class="nx">memo</span><span class="p">)</span> <span class="o">isnt</span> <span class="nx">el</span> <span class="k">else</span> <span class="o">not</span> <span class="nx">_</span><span class="p">.</span><span class="nx">include</span><span class="p">(</span><span class="nx">memo</span><span class="p">,</span> <span class="nx">el</span><span class="p">))</span>
|
143
|
-
<span class="nx">memo</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-41">¶</a> </div> <p>Produce an array that contains every item shared between all the
|
144
|
-
passed-in arrays.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.intersect = </span><span class="p">(</span><span class="nx">array</span><span class="p">)</span> <span class="o">-></span>
|
145
|
-
<span class="nv">rest = </span><span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span>
|
146
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">select</span> <span class="nx">_</span><span class="p">.</span><span class="nx">uniq</span><span class="p">(</span><span class="nx">array</span><span class="p">),</span> <span class="p">(</span><span class="nx">item</span><span class="p">)</span> <span class="o">-></span>
|
147
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">all</span> <span class="nx">rest</span><span class="p">,</span> <span class="p">(</span><span class="nx">other</span><span class="p">)</span> <span class="o">-></span>
|
148
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">other</span><span class="p">,</span> <span class="nx">item</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">0</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-42">¶</a> </div> <p>Zip together multiple lists into a single array -- elements that share
|
149
|
-
an index go together.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.zip = </span><span class="o">-></span>
|
150
|
-
<span class="nv">length = </span> <span class="nx">_</span><span class="p">.</span><span class="nx">max</span> <span class="nx">_</span><span class="p">.</span><span class="nx">pluck</span> <span class="nx">arguments</span><span class="p">,</span> <span class="s1">'length'</span>
|
151
|
-
<span class="nv">results = </span><span class="k">new</span> <span class="nb">Array</span> <span class="nx">length</span>
|
152
|
-
<span class="k">for</span> <span class="nx">i</span> <span class="k">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">...</span><span class="nx">length</span><span class="p">]</span>
|
153
|
-
<span class="nx">results</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">pluck</span> <span class="nx">arguments</span><span class="p">,</span> <span class="nb">String</span> <span class="nx">i</span>
|
154
|
-
<span class="nx">results</span></pre></div> </td> </tr> <tr id="section-43"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-43">¶</a> </div> <p>If the browser doesn't supply us with <strong>indexOf</strong> (I'm looking at you, MSIE),
|
155
|
-
we need this function. Return the position of the first occurrence of an
|
156
|
-
item in an array, or -1 if the item is not included in the array.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.indexOf = </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">item</span><span class="p">)</span> <span class="o">-></span>
|
157
|
-
<span class="k">return</span> <span class="nx">array</span><span class="p">.</span><span class="nx">indexOf</span> <span class="nx">item</span> <span class="k">if</span> <span class="nx">nativeIndexOf</span> <span class="o">and</span> <span class="nx">array</span><span class="p">.</span><span class="nx">indexOf</span> <span class="o">is</span> <span class="nx">nativeIndexOf</span>
|
158
|
-
<span class="nv">i = </span><span class="mi">0</span><span class="p">;</span> <span class="nv">l = </span><span class="nx">array</span><span class="p">.</span><span class="nx">length</span>
|
159
|
-
<span class="k">while</span> <span class="nx">l</span> <span class="o">-</span> <span class="nx">i</span>
|
160
|
-
<span class="k">if</span> <span class="nx">array</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">is</span> <span class="nx">item</span> <span class="k">then</span> <span class="k">return</span> <span class="nx">i</span> <span class="k">else</span> <span class="nx">i</span><span class="o">++</span>
|
161
|
-
<span class="o">-</span><span class="mi">1</span></pre></div> </td> </tr> <tr id="section-44"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-44">¶</a> </div> <p>Provide JavaScript 1.6's <strong>lastIndexOf</strong>, delegating to the native function,
|
162
|
-
if possible.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.lastIndexOf = </span><span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">item</span><span class="p">)</span> <span class="o">-></span>
|
163
|
-
<span class="k">return</span> <span class="nx">array</span><span class="p">.</span><span class="nx">lastIndexOf</span><span class="p">(</span><span class="nx">item</span><span class="p">)</span> <span class="k">if</span> <span class="nx">nativeLastIndexOf</span> <span class="o">and</span> <span class="nx">array</span><span class="p">.</span><span class="nx">lastIndexOf</span> <span class="o">is</span> <span class="nx">nativeLastIndexOf</span>
|
164
|
-
<span class="nv">i = </span><span class="nx">array</span><span class="p">.</span><span class="nx">length</span>
|
165
|
-
<span class="k">while</span> <span class="nx">i</span>
|
166
|
-
<span class="k">if</span> <span class="nx">array</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">is</span> <span class="nx">item</span> <span class="k">then</span> <span class="k">return</span> <span class="nx">i</span> <span class="k">else</span> <span class="nx">i</span><span class="o">--</span>
|
167
|
-
<span class="o">-</span><span class="mi">1</span></pre></div> </td> </tr> <tr id="section-45"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-45">¶</a> </div> <p>Generate an integer Array containing an arithmetic progression. A port of
|
168
|
-
<a href="http://docs.python.org/library/functions.html#range">the native Python <strong>range</strong> function</a>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.range = </span><span class="p">(</span><span class="nx">start</span><span class="p">,</span> <span class="nx">stop</span><span class="p">,</span> <span class="nx">step</span><span class="p">)</span> <span class="o">-></span>
|
169
|
-
<span class="nv">a = </span><span class="nx">arguments</span>
|
170
|
-
<span class="nv">solo = </span><span class="nx">a</span><span class="p">.</span><span class="nx">length</span> <span class="o"><=</span> <span class="mi">1</span>
|
171
|
-
<span class="nv">i = start = </span><span class="k">if</span> <span class="nx">solo</span> <span class="k">then</span> <span class="mi">0</span> <span class="k">else</span> <span class="nx">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
172
|
-
<span class="nv">stop = </span><span class="k">if</span> <span class="nx">solo</span> <span class="k">then</span> <span class="nx">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">else</span> <span class="nx">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
173
|
-
<span class="nv">step = </span><span class="nx">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">or</span> <span class="mi">1</span>
|
174
|
-
<span class="nv">len = </span><span class="nb">Math</span><span class="p">.</span><span class="nx">ceil</span><span class="p">((</span><span class="nx">stop</span> <span class="o">-</span> <span class="nx">start</span><span class="p">)</span> <span class="o">/</span> <span class="nx">step</span><span class="p">)</span>
|
175
|
-
<span class="k">return</span> <span class="p">[]</span> <span class="k">if</span> <span class="nx">len</span> <span class="o"><=</span> <span class="mi">0</span>
|
176
|
-
<span class="nv">range = </span><span class="k">new</span> <span class="nb">Array</span> <span class="nx">len</span>
|
177
|
-
<span class="nv">idx = </span><span class="mi">0</span>
|
178
|
-
<span class="nx">loop</span>
|
179
|
-
<span class="k">return</span> <span class="nx">range</span> <span class="k">if</span> <span class="p">(</span><span class="k">if</span> <span class="nx">step</span> <span class="o">></span> <span class="mi">0</span> <span class="k">then</span> <span class="nx">i</span> <span class="o">-</span> <span class="nx">stop</span> <span class="k">else</span> <span class="nx">stop</span> <span class="o">-</span> <span class="nx">i</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">0</span>
|
180
|
-
<span class="nx">range</span><span class="p">[</span><span class="nx">idx</span><span class="p">]</span> <span class="o">=</span> <span class="nx">i</span>
|
181
|
-
<span class="nx">idx</span><span class="o">++</span>
|
182
|
-
<span class="nx">i</span><span class="o">+=</span> <span class="nx">step</span></pre></div> </td> </tr> <tr id="section-46"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-46">¶</a> </div> <h2>Function Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-47"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-47">¶</a> </div> <p>Create a function bound to a given object (assigning <code>this</code>, and arguments,
|
183
|
-
optionally). Binding with arguments is also known as <strong>curry</strong>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.bind = </span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
184
|
-
<span class="nv">args = </span><span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span><span class="p">,</span> <span class="mi">2</span>
|
185
|
-
<span class="o">-></span> <span class="nx">func</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">obj</span> <span class="o">or</span> <span class="nx">root</span><span class="p">,</span> <span class="nx">args</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">arguments</span></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-48">¶</a> </div> <p>Bind all of an object's methods to that object. Useful for ensuring that
|
186
|
-
all callbacks defined on an object belong to it.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.bindAll = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
187
|
-
<span class="nv">funcs = </span><span class="k">if</span> <span class="nx">arguments</span><span class="p">.</span><span class="nx">length</span> <span class="o">></span> <span class="mi">1</span> <span class="k">then</span> <span class="nx">_</span><span class="p">.</span><span class="nx">rest</span><span class="p">(</span><span class="nx">arguments</span><span class="p">)</span> <span class="k">else</span> <span class="nx">_</span><span class="p">.</span><span class="nx">functions</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span>
|
188
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="nx">funcs</span><span class="p">,</span> <span class="p">(</span><span class="nx">f</span><span class="p">)</span> <span class="o">-></span> <span class="nx">obj</span><span class="p">[</span><span class="nx">f</span><span class="p">]</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">bind</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">f</span><span class="p">],</span> <span class="nx">obj</span>
|
189
|
-
<span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-49"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-49">¶</a> </div> <p>Delays a function for the given number of milliseconds, and then calls
|
190
|
-
it with the arguments supplied.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.delay = </span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">wait</span><span class="p">)</span> <span class="o">-></span>
|
191
|
-
<span class="nv">args = </span><span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span><span class="p">,</span> <span class="mi">2</span>
|
192
|
-
<span class="nx">setTimeout</span><span class="p">((</span><span class="o">-></span> <span class="nx">func</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">args</span><span class="p">)),</span> <span class="nx">wait</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">¶</a> </div> <p>Memoize an expensive function by storing its results.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.memoize = </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="o">-></span>
|
193
|
-
<span class="nv">memo = </span><span class="p">{}</span>
|
194
|
-
<span class="nx">hasher</span> <span class="o">or=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span>
|
195
|
-
<span class="o">-></span>
|
196
|
-
<span class="nv">key = </span><span class="nx">hasher</span><span class="p">.</span><span class="nx">apply</span> <span class="k">this</span><span class="p">,</span> <span class="nx">arguments</span>
|
197
|
-
<span class="k">return</span> <span class="nx">memo</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="k">if</span> <span class="nx">key</span> <span class="k">of</span> <span class="nx">memo</span>
|
198
|
-
<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="k">this</span><span class="p">,</span> <span class="nx">arguments</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">¶</a> </div> <p>Defers a function, scheduling it to run after the current call stack has
|
199
|
-
cleared.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.defer = </span><span class="p">(</span><span class="nx">func</span><span class="p">)</span> <span class="o">-></span>
|
200
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">delay</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">_</span><span class="p">,</span> <span class="p">[</span><span class="nx">func</span><span class="p">,</span> <span class="mi">1</span><span class="p">].</span><span class="nx">concat</span> <span class="nx">_</span><span class="p">.</span><span class="nx">rest</span> <span class="nx">arguments</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-52">¶</a> </div> <p>Returns the first function passed as an argument to the second,
|
201
|
-
allowing you to adjust arguments, run code before and after, and
|
202
|
-
conditionally execute the original function.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.wrap = </span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">wrapper</span><span class="p">)</span> <span class="o">-></span>
|
203
|
-
<span class="o">-></span> <span class="nx">wrapper</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">wrapper</span><span class="p">,</span> <span class="p">[</span><span class="nx">func</span><span class="p">].</span><span class="nx">concat</span> <span class="nx">arguments</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-53">¶</a> </div> <p>Returns a function that is the composition of a list of functions, each
|
204
|
-
consuming the return value of the function that follows.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.compose = </span><span class="o">-></span>
|
205
|
-
<span class="nv">funcs = </span><span class="nx">arguments</span>
|
206
|
-
<span class="o">-></span>
|
207
|
-
<span class="nv">args = </span><span class="nx">arguments</span>
|
208
|
-
<span class="k">for</span> <span class="nx">i</span> <span class="k">in</span> <span class="p">[</span><span class="nx">funcs</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">..</span><span class="mi">0</span><span class="p">]</span> <span class="nx">by</span> <span class="o">-</span><span class="mi">1</span>
|
209
|
-
<span class="nv">args = </span><span class="p">[</span><span class="nx">funcs</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">args</span><span class="p">)]</span>
|
210
|
-
<span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-54">¶</a> </div> <h2>Object Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-55">¶</a> </div> <p>Retrieve the names of an object's properties.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.keys = </span><span class="nx">nativeKeys</span> <span class="o">or</span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
211
|
-
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">range</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span>
|
212
|
-
<span class="nx">key</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-56">¶</a> </div> <p>Retrieve the values of an object's properties.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.values = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
213
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">map</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">_</span><span class="p">.</span><span class="nx">identity</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-57">¶</a> </div> <p>Return a sorted list of the function names available in Underscore.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.functions = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
214
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">obj</span><span class="p">),</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="o">-></span> <span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">obj</span><span class="p">[</span><span class="nx">key</span><span class="p">])).</span><span class="nx">sort</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-58">¶</a> </div> <p>Extend a given object with all of the properties in a source object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.extend = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
215
|
-
<span class="k">for</span> <span class="nx">source</span> <span class="k">in</span> <span class="nx">_</span><span class="p">.</span><span class="nx">rest</span><span class="p">(</span><span class="nx">arguments</span><span class="p">)</span>
|
216
|
-
<span class="nx">obj</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">val</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">source</span>
|
217
|
-
<span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">¶</a> </div> <p>Create a (shallow-cloned) duplicate of an object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.clone = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
218
|
-
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">slice</span> <span class="mi">0</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span> <span class="nx">obj</span>
|
219
|
-
<span class="nx">_</span><span class="p">.</span><span class="nx">extend</span> <span class="p">{},</span> <span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-60"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-60">¶</a> </div> <p>Invokes interceptor with the obj, and then returns obj.
|
220
|
-
The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.tap = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">interceptor</span><span class="p">)</span> <span class="o">-></span>
|
221
|
-
<span class="nx">interceptor</span> <span class="nx">obj</span>
|
222
|
-
<span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">¶</a> </div> <p>Perform a deep comparison to check if two objects are equal.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isEqual = </span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="o">-></span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">¶</a> </div> <p>Check object identity.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">a</span> <span class="o">is</span> <span class="nx">b</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-63">¶</a> </div> <p>Different types?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">atype = </span><span class="k">typeof</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span> <span class="nv">btype = </span><span class="k">typeof</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span>
|
223
|
-
<span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">atype</span> <span class="o">isnt</span> <span class="nx">btype</span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-64">¶</a> </div> <p>Basic equality test (watch out for coercions).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="o">`</span><span class="nx">a</span> <span class="o">==</span> <span class="nx">b</span><span class="o">`</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">¶</a> </div> <p>One is falsy and the other truthy.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">a</span> <span class="o">and</span> <span class="nx">b</span><span class="p">)</span> <span class="o">or</span> <span class="p">(</span><span class="nx">a</span> <span class="o">and</span> <span class="o">!</span><span class="nx">b</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-66">¶</a> </div> <p>One of them implements an <code>isEqual()</code>?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">a</span><span class="p">.</span><span class="nx">isEqual</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span> <span class="k">if</span> <span class="nx">a</span><span class="p">.</span><span class="nx">isEqual</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-67">¶</a> </div> <p>Check dates' integer values.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">a</span><span class="p">.</span><span class="nx">getTime</span><span class="p">()</span> <span class="o">is</span> <span class="nx">b</span><span class="p">.</span><span class="nx">getTime</span><span class="p">()</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isDate</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span> <span class="o">and</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isDate</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-68">¶</a> </div> <p>Both are NaN?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span> <span class="o">and</span> <span class="nx">_</span><span class="p">.</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-69">¶</a> </div> <p>Compare regular expressions.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isRegExp</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span> <span class="o">and</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isRegExp</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span>
|
224
|
-
<span class="k">return</span> <span class="nx">a</span><span class="p">.</span><span class="nx">source</span> <span class="o">is</span> <span class="nx">b</span><span class="p">.</span><span class="nx">source</span> <span class="o">and</span>
|
225
|
-
<span class="nx">a</span><span class="p">.</span><span class="nx">global</span> <span class="o">is</span> <span class="nx">b</span><span class="p">.</span><span class="nx">global</span> <span class="o">and</span>
|
226
|
-
<span class="nx">a</span><span class="p">.</span><span class="nx">ignoreCase</span> <span class="o">is</span> <span class="nx">b</span><span class="p">.</span><span class="nx">ignoreCase</span> <span class="o">and</span>
|
227
|
-
<span class="nx">a</span><span class="p">.</span><span class="nx">multiline</span> <span class="o">is</span> <span class="nx">b</span><span class="p">.</span><span class="nx">multiline</span></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-70">¶</a> </div> <p>If a is not an object by this point, we can't handle it.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">atype</span> <span class="o">isnt</span> <span class="s1">'object'</span></pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-71">¶</a> </div> <p>Check for different array lengths before comparing contents.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">a</span><span class="p">.</span><span class="nx">length</span> <span class="o">and</span> <span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">length</span> <span class="o">isnt</span> <span class="nx">b</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-72"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-72">¶</a> </div> <p>Nothing else worked, deep compare the contents.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">aKeys = </span><span class="nx">_</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span> <span class="nv">bKeys = </span><span class="nx">_</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">b</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-73"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-73">¶</a> </div> <p>Different object sizes?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">aKeys</span><span class="p">.</span><span class="nx">length</span> <span class="o">isnt</span> <span class="nx">bKeys</span><span class="p">.</span><span class="nx">length</span></pre></div> </td> </tr> <tr id="section-74"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-74">¶</a> </div> <p>Recursive comparison of contents.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">false</span> <span class="k">for</span> <span class="nx">key</span><span class="p">,</span> <span class="nx">val</span> <span class="k">of</span> <span class="nx">a</span> <span class="k">when</span> <span class="o">!</span><span class="p">(</span><span class="nx">key</span> <span class="k">of</span> <span class="nx">b</span><span class="p">)</span> <span class="o">or</span> <span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">isEqual</span><span class="p">(</span><span class="nx">val</span><span class="p">,</span> <span class="nx">b</span><span class="p">[</span><span class="nx">key</span><span class="p">])</span>
|
228
|
-
<span class="kc">true</span></pre></div> </td> </tr> <tr id="section-75"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-75">¶</a> </div> <p>Is a given array or object empty?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isEmpty = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
229
|
-
<span class="k">return</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">0</span> <span class="k">if</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">or</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>
|
230
|
-
<span class="k">return</span> <span class="kc">false</span> <span class="k">for</span> <span class="nx">own</span> <span class="nx">key</span> <span class="k">of</span> <span class="nx">obj</span>
|
231
|
-
<span class="kc">true</span></pre></div> </td> </tr> <tr id="section-76"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-76">¶</a> </div> <p>Is a given value a DOM element?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isElement = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">nodeType</span> <span class="o">is</span> <span class="mi">1</span></pre></div> </td> </tr> <tr id="section-77"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-77">¶</a> </div> <p>Is a given value an array?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isArray = </span><span class="nx">nativeIsArray</span> <span class="o">or</span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">concat</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">unshift</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">callee</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-78"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-78">¶</a> </div> <p>Is a given variable an arguments object?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isArguments = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">callee</span></pre></div> </td> </tr> <tr id="section-79"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-79">¶</a> </div> <p>Is the given value a function?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isFunction = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">constructor</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">call</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">apply</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-80"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-80">¶</a> </div> <p>Is the given value a string?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isString = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">is</span> <span class="s1">''</span> <span class="o">or</span> <span class="p">(</span><span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">charCodeAt</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">substr</span><span class="p">))</span></pre></div> </td> </tr> <tr id="section-81"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-81">¶</a> </div> <p>Is a given value a number?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isNumber = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="p">(</span><span class="nx">obj</span> <span class="o">is</span> <span class="o">+</span><span class="nx">obj</span><span class="p">)</span> <span class="o">or</span> <span class="nx">toString</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">is</span> <span class="s1">'[object Number]'</span></pre></div> </td> </tr> <tr id="section-82"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-82">¶</a> </div> <p>Is a given value a boolean?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isBoolean = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">obj</span> <span class="o">is</span> <span class="kc">true</span> <span class="o">or</span> <span class="nx">obj</span> <span class="o">is</span> <span class="kc">false</span></pre></div> </td> </tr> <tr id="section-83"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-83">¶</a> </div> <p>Is a given value a Date?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isDate = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">getTimezoneOffset</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">setUTCFullYear</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-84"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-84">¶</a> </div> <p>Is the given value a regular expression?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isRegExp = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="o">!!</span><span class="p">(</span><span class="nx">obj</span> <span class="o">and</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">exec</span> <span class="o">and</span> <span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">ignoreCase</span> <span class="o">or</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">ignoreCase</span> <span class="o">is</span> <span class="kc">false</span><span class="p">))</span></pre></div> </td> </tr> <tr id="section-85"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-85">¶</a> </div> <p>Is the given value NaN -- this one is interesting. <code>NaN != NaN</code>, and
|
232
|
-
<code>isNaN(undefined) == true</code>, so we make sure it's a number first.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isNaN = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">_</span><span class="p">.</span><span class="nx">isNumber</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">and</span> <span class="nb">window</span><span class="p">.</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-86"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-86">¶</a> </div> <p>Is a given value equal to null?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isNull = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="nx">obj</span> <span class="o">is</span> <span class="kc">null</span></pre></div> </td> </tr> <tr id="section-87"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-87">¶</a> </div> <p>Is a given variable undefined?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.isUndefined = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span> <span class="k">typeof</span> <span class="nx">obj</span> <span class="o">is</span> <span class="s1">'undefined'</span></pre></div> </td> </tr> <tr id="section-88"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-88">¶</a> </div> <h2>Utility Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-89"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-89">¶</a> </div> <p>Run Underscore.js in noConflict mode, returning the <code>_</code> variable to its
|
233
|
-
previous owner. Returns a reference to the Underscore object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.noConflict = </span><span class="o">-></span>
|
234
|
-
<span class="nv">root._ = </span><span class="nx">previousUnderscore</span>
|
235
|
-
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-90"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-90">¶</a> </div> <p>Keep the identity function around for default iterators.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.identity = </span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">-></span> <span class="nx">value</span></pre></div> </td> </tr> <tr id="section-91"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-91">¶</a> </div> <p>Run a function <code>n</code> times.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.times = </span><span class="p">(</span><span class="nx">n</span><span class="p">,</span> <span class="nx">iterator</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="o">-></span>
|
236
|
-
<span class="nx">iterator</span><span class="p">.</span><span class="nx">call</span> <span class="nx">context</span><span class="p">,</span> <span class="nx">i</span> <span class="k">for</span> <span class="nx">i</span> <span class="k">in</span> <span class="p">[</span><span class="mi">0</span><span class="p">...</span><span class="nx">n</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-92"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-92">¶</a> </div> <p>Break out of the middle of an iteration.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.breakLoop = </span><span class="o">-></span> <span class="k">throw</span> <span class="nx">breaker</span></pre></div> </td> </tr> <tr id="section-93"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-93">¶</a> </div> <p>Add your own custom functions to the Underscore object, ensuring that
|
237
|
-
they're correctly added to the OOP wrapper as well.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.mixin = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
238
|
-
<span class="k">for</span> <span class="nx">name</span> <span class="k">in</span> <span class="nx">_</span><span class="p">.</span><span class="nx">functions</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span>
|
239
|
-
<span class="nx">addToWrapper</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">_</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-94"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-94">¶</a> </div> <p>Generate a unique integer id (unique within the entire client session).
|
240
|
-
Useful for temporary DOM ids.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">idCounter = </span><span class="mi">0</span>
|
241
|
-
<span class="nv">_.uniqueId = </span><span class="p">(</span><span class="nx">prefix</span><span class="p">)</span> <span class="o">-></span>
|
242
|
-
<span class="p">(</span><span class="nx">prefix</span> <span class="o">or</span> <span class="s1">''</span><span class="p">)</span> <span class="o">+</span> <span class="nx">idCounter</span><span class="o">++</span></pre></div> </td> </tr> <tr id="section-95"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-95">¶</a> </div> <p>By default, Underscore uses <strong>ERB</strong>-style template delimiters, change the
|
243
|
-
following template settings to use alternative delimiters.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.templateSettings = </span><span class="p">{</span>
|
244
|
-
<span class="nx">start</span><span class="o">:</span> <span class="s1">'<%'</span>
|
245
|
-
<span class="nx">end</span><span class="o">:</span> <span class="s1">'%>'</span>
|
246
|
-
<span class="nx">interpolate</span><span class="o">:</span> <span class="sr">/<%=(.+?)%>/g</span>
|
247
|
-
<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>JavaScript templating a-la <strong>ERB</strong>, pilfered from John Resig's
|
248
|
-
<em>Secrets of the JavaScript Ninja</em>, page 83.
|
249
|
-
Single-quote fix from Rick Strahl.
|
250
|
-
With alterations for arbitrary delimiters, and to preserve whitespace.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.template = </span><span class="p">(</span><span class="nx">str</span><span class="p">,</span> <span class="nx">data</span><span class="p">)</span> <span class="o">-></span>
|
251
|
-
<span class="nv">c = </span><span class="nx">_</span><span class="p">.</span><span class="nx">templateSettings</span>
|
252
|
-
<span class="nv">endMatch = </span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s2">"'(?=[^"</span><span class="o">+</span><span class="nx">c</span><span class="p">.</span><span class="nx">end</span><span class="p">.</span><span class="nx">substr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span><span class="o">+</span><span class="s2">"]*"</span><span class="o">+</span><span class="nx">escapeRegExp</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">end</span><span class="p">)</span><span class="o">+</span><span class="s2">")"</span><span class="p">,</span><span class="s2">"g"</span><span class="p">)</span>
|
253
|
-
<span class="nv">fn = </span><span class="k">new</span> <span class="nb">Function</span> <span class="s1">'obj'</span><span class="p">,</span>
|
254
|
-
<span class="s1">'var p=[],print=function(){p.push.apply(p,arguments);};'</span> <span class="o">+</span>
|
255
|
-
<span class="s1">'with(obj||{}){p.push(\''</span> <span class="o">+</span>
|
256
|
-
<span class="nx">str</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\r/g</span><span class="p">,</span> <span class="s1">'\\r'</span><span class="p">)</span>
|
257
|
-
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\n/g</span><span class="p">,</span> <span class="s1">'\\n'</span><span class="p">)</span>
|
258
|
-
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\t/g</span><span class="p">,</span> <span class="s1">'\\t'</span><span class="p">)</span>
|
259
|
-
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">endMatch</span><span class="p">,</span><span class="s2">"✄"</span><span class="p">)</span>
|
260
|
-
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s2">"'"</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s2">"\\'"</span><span class="p">)</span>
|
261
|
-
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s2">"✄"</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s2">"'"</span><span class="p">)</span>
|
262
|
-
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">interpolate</span><span class="p">,</span> <span class="s2">"',$1,'"</span><span class="p">)</span>
|
263
|
-
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">start</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s2">"');"</span><span class="p">)</span>
|
264
|
-
<span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">end</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s2">"p.push('"</span><span class="p">)</span> <span class="o">+</span>
|
265
|
-
<span class="s2">"');}return p.join('');"</span>
|
266
|
-
<span class="k">if</span> <span class="nx">data</span> <span class="k">then</span> <span class="nx">fn</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="k">else</span> <span class="nx">fn</span></pre></div> </td> </tr> <tr id="section-97"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-97">¶</a> </div> <h2>Aliases</h2> </td> <td class="code"> <div class="highlight"><pre><span class="nv">_.forEach = </span><span class="nx">_</span><span class="p">.</span><span class="nx">each</span>
|
267
|
-
<span class="nv">_.foldl = _.inject = </span><span class="nx">_</span><span class="p">.</span><span class="nx">reduce</span>
|
268
|
-
<span class="nv">_.foldr = </span><span class="nx">_</span><span class="p">.</span><span class="nx">reduceRight</span>
|
269
|
-
<span class="nv">_.select = </span><span class="nx">_</span><span class="p">.</span><span class="nx">filter</span>
|
270
|
-
<span class="nv">_.all = </span><span class="nx">_</span><span class="p">.</span><span class="nx">every</span>
|
271
|
-
<span class="nv">_.any = </span><span class="nx">_</span><span class="p">.</span><span class="nx">some</span>
|
272
|
-
<span class="nv">_.contains = </span><span class="nx">_</span><span class="p">.</span><span class="nx">include</span>
|
273
|
-
<span class="nv">_.head = </span><span class="nx">_</span><span class="p">.</span><span class="nx">first</span>
|
274
|
-
<span class="nv">_.tail = </span><span class="nx">_</span><span class="p">.</span><span class="nx">rest</span>
|
275
|
-
<span class="nv">_.methods = </span><span class="nx">_</span><span class="p">.</span><span class="nx">functions</span></pre></div> </td> </tr> <tr id="section-98"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-98">¶</a> </div> <h2>Setup the OOP Wrapper</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-99"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-99">¶</a> </div> <p>If Underscore is called as a function, it returns a wrapped object that
|
276
|
-
can be used OO-style. This wrapper holds altered versions of all the
|
277
|
-
underscore functions. Wrapped objects may be chained.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">wrapper = </span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">-></span>
|
278
|
-
<span class="k">this</span><span class="p">.</span><span class="nv">_wrapped = </span><span class="nx">obj</span>
|
279
|
-
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-100"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-100">¶</a> </div> <p>Helper function to continue chaining intermediate results.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">result = </span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">chain</span><span class="p">)</span> <span class="o">-></span>
|
280
|
-
<span class="k">if</span> <span class="nx">chain</span> <span class="k">then</span> <span class="nx">_</span><span class="p">(</span><span class="nx">obj</span><span class="p">).</span><span class="nx">chain</span><span class="p">()</span> <span class="k">else</span> <span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-101"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-101">¶</a> </div> <p>A method to easily add functions to the OOP wrapper.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">addToWrapper = </span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">func</span><span class="p">)</span> <span class="o">-></span>
|
281
|
-
<span class="nx">wrapper</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="o">-></span>
|
282
|
-
<span class="nv">args = </span><span class="nx">_</span><span class="p">.</span><span class="nx">toArray</span> <span class="nx">arguments</span>
|
283
|
-
<span class="nx">unshift</span><span class="p">.</span><span class="nx">call</span> <span class="nx">args</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span>
|
284
|
-
<span class="nx">result</span> <span class="nx">func</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">_</span><span class="p">,</span> <span class="nx">args</span><span class="p">),</span> <span class="k">this</span><span class="p">.</span><span class="nx">_chain</span></pre></div> </td> </tr> <tr id="section-102"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-102">¶</a> </div> <p>Add all ofthe Underscore functions to the wrapper object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">_</span><span class="p">.</span><span class="nx">mixin</span> <span class="nx">_</span></pre></div> </td> </tr> <tr id="section-103"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-103">¶</a> </div> <p>Add all mutator Array functions to the wrapper.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="p">[</span><span class="s1">'pop'</span><span class="p">,</span> <span class="s1">'push'</span><span class="p">,</span> <span class="s1">'reverse'</span><span class="p">,</span> <span class="s1">'shift'</span><span class="p">,</span> <span class="s1">'sort'</span><span class="p">,</span> <span class="s1">'splice'</span><span class="p">,</span> <span class="s1">'unshift'</span><span class="p">],</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
285
|
-
<span class="nv">method = </span><span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span>
|
286
|
-
<span class="nx">wrapper</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="o">-></span>
|
287
|
-
<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>
|
288
|
-
<span class="nx">result</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">_chain</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-104"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-104">¶</a> </div> <p>Add all accessor Array functions to the wrapper.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">_</span><span class="p">.</span><span class="nx">each</span> <span class="p">[</span><span class="s1">'concat'</span><span class="p">,</span> <span class="s1">'join'</span><span class="p">,</span> <span class="s1">'slice'</span><span class="p">],</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
289
|
-
<span class="nv">method = </span><span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span>
|
290
|
-
<span class="nx">wrapper</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="o">-></span>
|
291
|
-
<span class="nx">result</span><span class="p">(</span><span class="nx">method</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">),</span> <span class="k">this</span><span class="p">.</span><span class="nx">_chain</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-105"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-105">¶</a> </div> <p>Start chaining a wrapped Underscore object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">wrapper::chain = </span><span class="o">-></span>
|
292
|
-
<span class="k">this</span><span class="p">.</span><span class="nv">_chain = </span><span class="kc">true</span>
|
293
|
-
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-106"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-106">¶</a> </div> <p>Extracts the result from a wrapped and chained object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">wrapper::value = </span><span class="o">-></span> <span class="k">this</span><span class="p">.</span><span class="nx">_wrapped</span>
|
294
|
-
|
295
|
-
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|