spade-packager 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.gitmodules +6 -0
- data/bin/spadepkg +8 -0
- data/lib/libgems_ext.rb +8 -0
- data/lib/libgems_ext/config_file.rb +33 -0
- data/lib/libgems_ext/dependency_installer.rb +150 -0
- data/lib/libgems_ext/installer.rb +39 -0
- data/lib/libgems_ext/libgems.rb +39 -0
- data/lib/libgems_ext/spec_fetcher.rb +11 -0
- data/lib/spade-packager.rb +1 -0
- data/lib/spade/packager.rb +18 -0
- data/lib/spade/packager/cli.rb +9 -0
- data/lib/spade/packager/cli/base.rb +196 -0
- data/lib/spade/packager/cli/owner.rb +46 -0
- data/lib/spade/packager/cli/project_generator.rb +117 -0
- data/lib/spade/packager/credentials.rb +38 -0
- data/lib/spade/packager/local.rb +50 -0
- data/lib/spade/packager/package.rb +160 -0
- data/lib/spade/packager/remote.rb +98 -0
- data/lib/spade/packager/repository.rb +18 -0
- data/lib/spade/packager/version.rb +5 -0
- data/packages/coffee-script/.gitignore +8 -0
- data/packages/coffee-script/.npmignore +11 -0
- data/packages/coffee-script/Cakefile +229 -0
- data/packages/coffee-script/LICENSE +22 -0
- data/packages/coffee-script/README +47 -0
- data/packages/coffee-script/Rakefile +78 -0
- data/packages/coffee-script/bin/cake +7 -0
- data/packages/coffee-script/bin/coffee +7 -0
- data/packages/coffee-script/documentation/coffee/aliases.coffee +11 -0
- data/packages/coffee-script/documentation/coffee/array_comprehensions.coffee +2 -0
- data/packages/coffee-script/documentation/coffee/block_comment.coffee +6 -0
- data/packages/coffee-script/documentation/coffee/cake_tasks.coffee +9 -0
- data/packages/coffee-script/documentation/coffee/classes.coffee +25 -0
- data/packages/coffee-script/documentation/coffee/comparisons.coffee +5 -0
- data/packages/coffee-script/documentation/coffee/conditionals.coffee +13 -0
- data/packages/coffee-script/documentation/coffee/default_args.coffee +8 -0
- data/packages/coffee-script/documentation/coffee/do.coffee +4 -0
- data/packages/coffee-script/documentation/coffee/embedded.coffee +5 -0
- data/packages/coffee-script/documentation/coffee/existence.coffee +10 -0
- data/packages/coffee-script/documentation/coffee/expressions.coffee +9 -0
- data/packages/coffee-script/documentation/coffee/expressions_assignment.coffee +3 -0
- data/packages/coffee-script/documentation/coffee/expressions_comprehension.coffee +3 -0
- data/packages/coffee-script/documentation/coffee/expressions_try.coffee +7 -0
- data/packages/coffee-script/documentation/coffee/fat_arrow.coffee +6 -0
- data/packages/coffee-script/documentation/coffee/functions.coffee +2 -0
- data/packages/coffee-script/documentation/coffee/heredocs.coffee +7 -0
- data/packages/coffee-script/documentation/coffee/heregexes.coffee +11 -0
- data/packages/coffee-script/documentation/coffee/interpolation.coffee +6 -0
- data/packages/coffee-script/documentation/coffee/multiple_return_values.coffee +7 -0
- data/packages/coffee-script/documentation/coffee/object_comprehensions.coffee +4 -0
- data/packages/coffee-script/documentation/coffee/object_extraction.coffee +13 -0
- data/packages/coffee-script/documentation/coffee/objects_and_arrays.coffee +19 -0
- data/packages/coffee-script/documentation/coffee/objects_reserved.coffee +5 -0
- data/packages/coffee-script/documentation/coffee/overview.coffee +28 -0
- data/packages/coffee-script/documentation/coffee/parallel_assignment.coffee +6 -0
- data/packages/coffee-script/documentation/coffee/patterns_and_splats.coffee +7 -0
- data/packages/coffee-script/documentation/coffee/prototypes.coffee +3 -0
- data/packages/coffee-script/documentation/coffee/range_comprehensions.coffee +2 -0
- data/packages/coffee-script/documentation/coffee/scope.coffee +5 -0
- data/packages/coffee-script/documentation/coffee/slices.coffee +7 -0
- data/packages/coffee-script/documentation/coffee/soaks.coffee +1 -0
- data/packages/coffee-script/documentation/coffee/splats.coffee +27 -0
- data/packages/coffee-script/documentation/coffee/splices.coffee +5 -0
- data/packages/coffee-script/documentation/coffee/strings.coffee +8 -0
- data/packages/coffee-script/documentation/coffee/switch.coffee +10 -0
- data/packages/coffee-script/documentation/coffee/try.coffee +8 -0
- data/packages/coffee-script/documentation/coffee/while.coffee +10 -0
- data/packages/coffee-script/documentation/css/docs.css +374 -0
- data/packages/coffee-script/documentation/css/idle.css +64 -0
- data/packages/coffee-script/documentation/docs/browser.html +25 -0
- data/packages/coffee-script/documentation/docs/cake.html +43 -0
- data/packages/coffee-script/documentation/docs/coffee-script.html +51 -0
- data/packages/coffee-script/documentation/docs/command.html +161 -0
- data/packages/coffee-script/documentation/docs/docco.css +186 -0
- data/packages/coffee-script/documentation/docs/grammar.html +399 -0
- data/packages/coffee-script/documentation/docs/helpers.html +31 -0
- data/packages/coffee-script/documentation/docs/index.html +3 -0
- data/packages/coffee-script/documentation/docs/lexer.html +490 -0
- data/packages/coffee-script/documentation/docs/nodes.html +1338 -0
- data/packages/coffee-script/documentation/docs/optparse.html +78 -0
- data/packages/coffee-script/documentation/docs/repl.html +24 -0
- data/packages/coffee-script/documentation/docs/rewriter.html +251 -0
- data/packages/coffee-script/documentation/docs/scope.html +54 -0
- data/packages/coffee-script/documentation/docs/underscore.html +295 -0
- 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 +1607 -0
- data/packages/coffee-script/documentation/js/aliases.js +17 -0
- data/packages/coffee-script/documentation/js/array_comprehensions.js +6 -0
- data/packages/coffee-script/documentation/js/block_comment.js +4 -0
- data/packages/coffee-script/documentation/js/cake_tasks.js +10 -0
- data/packages/coffee-script/documentation/js/classes.js +44 -0
- data/packages/coffee-script/documentation/js/comparisons.js +3 -0
- data/packages/coffee-script/documentation/js/conditionals.js +12 -0
- data/packages/coffee-script/documentation/js/default_args.js +7 -0
- data/packages/coffee-script/documentation/js/do.js +10 -0
- data/packages/coffee-script/documentation/js/embedded.js +4 -0
- data/packages/coffee-script/documentation/js/existence.js +6 -0
- data/packages/coffee-script/documentation/js/expressions.js +15 -0
- data/packages/coffee-script/documentation/js/expressions_assignment.js +2 -0
- data/packages/coffee-script/documentation/js/expressions_comprehension.js +9 -0
- data/packages/coffee-script/documentation/js/expressions_try.js +7 -0
- data/packages/coffee-script/documentation/js/fat_arrow.js +9 -0
- data/packages/coffee-script/documentation/js/functions.js +7 -0
- data/packages/coffee-script/documentation/js/heredocs.js +2 -0
- data/packages/coffee-script/documentation/js/heregexes.js +2 -0
- data/packages/coffee-script/documentation/js/interpolation.js +4 -0
- data/packages/coffee-script/documentation/js/multiple_return_values.js +5 -0
- data/packages/coffee-script/documentation/js/object_comprehensions.js +15 -0
- data/packages/coffee-script/documentation/js/object_extraction.js +10 -0
- data/packages/coffee-script/documentation/js/objects_and_arrays.js +17 -0
- data/packages/coffee-script/documentation/js/objects_reserved.js +4 -0
- data/packages/coffee-script/documentation/js/overview.js +35 -0
- data/packages/coffee-script/documentation/js/parallel_assignment.js +4 -0
- data/packages/coffee-script/documentation/js/patterns_and_splats.js +4 -0
- data/packages/coffee-script/documentation/js/prototypes.js +3 -0
- data/packages/coffee-script/documentation/js/range_comprehensions.js +9 -0
- data/packages/coffee-script/documentation/js/scope.js +8 -0
- data/packages/coffee-script/documentation/js/slices.js +4 -0
- data/packages/coffee-script/documentation/js/soaks.js +2 -0
- data/packages/coffee-script/documentation/js/splats.js +15 -0
- data/packages/coffee-script/documentation/js/splices.js +3 -0
- data/packages/coffee-script/documentation/js/strings.js +2 -0
- data/packages/coffee-script/documentation/js/switch.js +23 -0
- data/packages/coffee-script/documentation/js/try.js +8 -0
- data/packages/coffee-script/documentation/js/while.js +18 -0
- data/packages/coffee-script/documentation/vendor/jquery-1.4.2.js +6240 -0
- data/packages/coffee-script/examples/beautiful_code/binary_search.coffee +16 -0
- data/packages/coffee-script/examples/beautiful_code/quicksort_runtime.coffee +13 -0
- data/packages/coffee-script/examples/beautiful_code/regular_expression_matcher.coffee +34 -0
- data/packages/coffee-script/examples/blocks.coffee +54 -0
- data/packages/coffee-script/examples/code.coffee +167 -0
- data/packages/coffee-script/examples/computer_science/README +4 -0
- data/packages/coffee-script/examples/computer_science/binary_search.coffee +25 -0
- data/packages/coffee-script/examples/computer_science/bubble_sort.coffee +11 -0
- data/packages/coffee-script/examples/computer_science/linked_list.coffee +108 -0
- data/packages/coffee-script/examples/computer_science/luhn_algorithm.coffee +36 -0
- data/packages/coffee-script/examples/computer_science/merge_sort.coffee +19 -0
- data/packages/coffee-script/examples/computer_science/selection_sort.coffee +23 -0
- data/packages/coffee-script/examples/poignant.coffee +181 -0
- data/packages/coffee-script/examples/potion.coffee +206 -0
- data/packages/coffee-script/examples/underscore.coffee +682 -0
- data/packages/coffee-script/examples/web_server.coffee +12 -0
- data/packages/coffee-script/extras/EXTRAS +7 -0
- data/packages/coffee-script/extras/coffee-script.js +8 -0
- data/packages/coffee-script/extras/jsl.conf +44 -0
- data/packages/coffee-script/index.html +2515 -0
- data/packages/coffee-script/lib/browser.js +52 -0
- data/packages/coffee-script/lib/cake.js +76 -0
- data/packages/coffee-script/lib/coffee-script.js +82 -0
- data/packages/coffee-script/lib/command.js +263 -0
- data/packages/coffee-script/lib/grammar.js +581 -0
- data/packages/coffee-script/lib/helpers.js +66 -0
- data/packages/coffee-script/lib/index.js +8 -0
- data/packages/coffee-script/lib/lexer.js +633 -0
- data/packages/coffee-script/lib/nodes.js +2165 -0
- data/packages/coffee-script/lib/optparse.js +111 -0
- data/packages/coffee-script/lib/parser.js +649 -0
- data/packages/coffee-script/lib/repl.js +42 -0
- data/packages/coffee-script/lib/rewriter.js +353 -0
- data/packages/coffee-script/lib/scope.js +120 -0
- data/packages/coffee-script/lib/spade-format.js +45 -0
- data/packages/coffee-script/package.json +26 -0
- data/packages/coffee-script/src/browser.coffee +43 -0
- data/packages/coffee-script/src/cake.coffee +69 -0
- data/packages/coffee-script/src/coffee-script.coffee +92 -0
- data/packages/coffee-script/src/command.coffee +214 -0
- data/packages/coffee-script/src/grammar.coffee +590 -0
- data/packages/coffee-script/src/helpers.coffee +56 -0
- data/packages/coffee-script/src/index.coffee +2 -0
- data/packages/coffee-script/src/lexer.coffee +653 -0
- data/packages/coffee-script/src/nodes.coffee +1754 -0
- data/packages/coffee-script/src/optparse.coffee +99 -0
- data/packages/coffee-script/src/repl.coffee +42 -0
- data/packages/coffee-script/src/rewriter.coffee +326 -0
- data/packages/coffee-script/src/scope.coffee +94 -0
- data/packages/coffee-script/test/arguments.coffee +127 -0
- data/packages/coffee-script/test/assignment.coffee +98 -0
- data/packages/coffee-script/test/break.coffee +18 -0
- data/packages/coffee-script/test/comments.coffee +201 -0
- data/packages/coffee-script/test/conditionals.coffee +181 -0
- data/packages/coffee-script/test/exception_handling.coffee +90 -0
- data/packages/coffee-script/test/helpers.coffee +96 -0
- data/packages/coffee-script/test/importing.coffee +18 -0
- data/packages/coffee-script/test/operators.coffee +225 -0
- data/packages/coffee-script/test/ranges_slices_and_splices.coffee +186 -0
- data/packages/coffee-script/test/regular_expressions.coffee +56 -0
- data/packages/coffee-script/test/test.html +123 -0
- data/packages/coffee-script/test/test_chaining.coffee +77 -0
- data/packages/coffee-script/test/test_classes.coffee +372 -0
- data/packages/coffee-script/test/test_compilation.coffee +26 -0
- data/packages/coffee-script/test/test_comprehensions.coffee +318 -0
- data/packages/coffee-script/test/test_existence.coffee +165 -0
- data/packages/coffee-script/test/test_functions.coffee +379 -0
- data/packages/coffee-script/test/test_heredocs.coffee +111 -0
- data/packages/coffee-script/test/test_literals.coffee +270 -0
- data/packages/coffee-script/test/test_option_parser.coffee +27 -0
- data/packages/coffee-script/test/test_pattern_matching.coffee +162 -0
- data/packages/coffee-script/test/test_returns.coffee +63 -0
- data/packages/coffee-script/test/test_splats.coffee +102 -0
- data/packages/coffee-script/test/test_strings.coffee +118 -0
- data/packages/coffee-script/test/test_switch.coffee +103 -0
- data/packages/coffee-script/test/test_while.coffee +71 -0
- data/packages/ivory/LICENSE.txt +1 -0
- data/packages/ivory/README.md +19 -0
- data/packages/ivory/lib/buffer.js +111 -0
- data/packages/ivory/lib/events.js +137 -0
- data/packages/ivory/lib/fs.js +266 -0
- data/packages/ivory/lib/main.js +13 -0
- data/packages/ivory/lib/path.js +158 -0
- data/packages/ivory/lib/ruby/buffer.rb +145 -0
- data/packages/ivory/lib/ruby/constants.rb +585 -0
- data/packages/ivory/lib/ruby/events.rb +32 -0
- data/packages/ivory/lib/ruby/fs.rb +245 -0
- data/packages/ivory/lib/ruby/process.rb +28 -0
- data/packages/ivory/lib/stream.js +115 -0
- data/packages/ivory/lib/util.js +414 -0
- data/packages/ivory/package.json +11 -0
- data/packages/ivory/spade-boot.js +78 -0
- data/packages/jquery/main.js +7179 -0
- data/packages/jquery/package.json +10 -0
- data/packages/json/lib/main.js +14 -0
- data/packages/json/package.json +8 -0
- data/packages/lproj/README.md +77 -0
- data/packages/lproj/examples/demo-app/en.lproj/localized.strings +2 -0
- data/packages/lproj/examples/demo-app/fr.lproj/localized.strings +3 -0
- data/packages/lproj/examples/demo-app/index.html +8 -0
- data/packages/lproj/examples/demo-app/lib/main.js +7 -0
- data/packages/lproj/examples/demo-app/package.json +9 -0
- data/packages/lproj/lib/main.js +78 -0
- data/packages/lproj/lib/strings-format.js +6 -0
- data/packages/lproj/package.json +9 -0
- data/packages/optparse/README.md +161 -0
- data/packages/optparse/TODO +1 -0
- data/packages/optparse/examples/browser-test.html +75 -0
- data/packages/optparse/examples/nodejs-test.js +90 -0
- data/packages/optparse/lib/optparse.js +309 -0
- data/packages/optparse/package.json +13 -0
- data/packages/optparse/seed.yml +5 -0
- data/packages/text/lib/main.js +8 -0
- data/packages/text/package.json +9 -0
- data/packages/web-file/README.md +7 -0
- data/packages/web-file/lib/errors.js +32 -0
- data/packages/web-file/lib/file-reader.js +10 -0
- data/packages/web-file/lib/file-system.js +234 -0
- data/packages/web-file/lib/file-writer.js +10 -0
- data/packages/web-file/lib/file.js +9 -0
- data/packages/web-file/lib/main.js +34 -0
- data/packages/web-file/lib/platform.js +25 -0
- data/packages/web-file/lib/ruby/file.rb +252 -0
- data/packages/web-file/lib/ruby/file_reader.rb +69 -0
- data/packages/web-file/lib/ruby/file_system.rb +134 -0
- data/packages/web-file/lib/ruby/file_writer.rb +78 -0
- data/packages/web-file/package.json +12 -0
- data/packages/web-typed-array/README.md +7 -0
- data/packages/web-typed-array/lib/array-buffer-view.js +9 -0
- data/packages/web-typed-array/lib/array-buffer.js +7 -0
- data/packages/web-typed-array/lib/main.js +33 -0
- data/packages/web-typed-array/lib/platform.js +20 -0
- data/packages/web-typed-array/lib/ruby/array_buffer.rb +31 -0
- data/packages/web-typed-array/lib/ruby/array_buffer_view.rb +130 -0
- data/packages/web-typed-array/lib/ruby/typed_array.rb +133 -0
- data/packages/web-typed-array/lib/typed-array.js +26 -0
- data/packages/web-typed-array/package.json +9 -0
- data/spade-packager.gemspec +39 -0
- data/spec/cli/build_spec.rb +57 -0
- data/spec/cli/install_spec.rb +119 -0
- data/spec/cli/installed_spec.rb +55 -0
- data/spec/cli/list_spec.rb +74 -0
- data/spec/cli/login_spec.rb +75 -0
- data/spec/cli/new_spec.rb +5 -0
- data/spec/cli/owner_spec.rb +114 -0
- data/spec/cli/push_spec.rb +73 -0
- data/spec/cli/uninstall_spec.rb +58 -0
- data/spec/cli/unpack_spec.rb +72 -0
- data/spec/cli/unyank_spec.rb +73 -0
- data/spec/cli/yank_spec.rb +73 -0
- data/spec/credentials_spec.rb +23 -0
- data/spec/fixtures/badrake-0.8.7.spd +0 -0
- data/spec/fixtures/builder-3.0.0.spd +0 -0
- data/spec/fixtures/bundler-1.1.pre.spd +0 -0
- 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 +3 -0
- data/spec/fixtures/core-test/lib/main.js +1 -0
- data/spec/fixtures/core-test/resources/runner.css +0 -0
- data/spec/fixtures/core-test/tests/test.js +1 -0
- data/spec/fixtures/highline-1.6.1.spd +0 -0
- 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 +30 -0
- data/spec/fixtures/rake-0.8.6.spd +0 -0
- data/spec/fixtures/rake-0.8.7.spd +0 -0
- data/spec/gauntlet_spec.rb +27 -0
- data/spec/package_spec.rb +267 -0
- data/spec/spec_helper.rb +32 -0
- data/spec/support/cli.rb +103 -0
- data/spec/support/fake.rb +48 -0
- data/spec/support/fake_gem_server.rb +67 -0
- data/spec/support/fake_gemcutter.rb +50 -0
- data/spec/support/matchers.rb +32 -0
- data/spec/support/path.rb +61 -0
- data/templates/project/LICENSE +19 -0
- data/templates/project/README.md +21 -0
- data/templates/project/lib/main.js +9 -0
- data/templates/project/project.json +31 -0
- data/templates/project/tests/main-test.js +8 -0
- metadata +484 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
# Sorts an array in ascending natural order using merge sort.
|
2
|
+
merge_sort = (list) ->
|
3
|
+
|
4
|
+
return list if list.length is 1
|
5
|
+
|
6
|
+
result = []
|
7
|
+
pivot = Math.floor list.length / 2
|
8
|
+
left = merge_sort list.slice 0, pivot
|
9
|
+
right = merge_sort list.slice pivot
|
10
|
+
|
11
|
+
while left.length and right.length
|
12
|
+
result.push(if left[0] < right[0] then left.shift() else right.shift())
|
13
|
+
|
14
|
+
result.concat(left).concat(right)
|
15
|
+
|
16
|
+
|
17
|
+
# Test the function.
|
18
|
+
console.log merge_sort([3, 2, 1]).join(' ') is '1 2 3'
|
19
|
+
console.log merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# An in-place selection sort.
|
2
|
+
selection_sort = (list) ->
|
3
|
+
len = list.length
|
4
|
+
|
5
|
+
# For each item in the list.
|
6
|
+
for i in [0...len]
|
7
|
+
|
8
|
+
# Set the minimum to this position.
|
9
|
+
min = i
|
10
|
+
|
11
|
+
# Check the rest of the array to see if anything is smaller.
|
12
|
+
min = j if list[j] < list[min] for j in [i + 1...len]
|
13
|
+
|
14
|
+
# Swap if a smaller item has been found.
|
15
|
+
[list[i], list[min]] = [list[min], list[i]] if i isnt min
|
16
|
+
|
17
|
+
# The list is now sorted.
|
18
|
+
list
|
19
|
+
|
20
|
+
|
21
|
+
# Test the function.
|
22
|
+
console.log selection_sort([3, 2, 1]).join(' ') is '1 2 3'
|
23
|
+
console.log selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9'
|
@@ -0,0 +1,181 @@
|
|
1
|
+
# Examples from the Poignant Guide.
|
2
|
+
|
3
|
+
# ['toast', 'cheese', 'wine'].each { |food| print food.capitalize }
|
4
|
+
|
5
|
+
['toast', 'wine', 'cheese'].each (food) -> print food.capitalize()
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
# class LotteryTicket
|
10
|
+
# def picks; @picks; end
|
11
|
+
# def picks=(var); @picks = var; end
|
12
|
+
# def purchased; @purchased; end
|
13
|
+
# def purchased=(var); @purchased = var; end
|
14
|
+
# end
|
15
|
+
|
16
|
+
LotteryTicket =
|
17
|
+
get_picks: -> @picks
|
18
|
+
set_picks: (@picks) ->
|
19
|
+
get_purchased: -> @purchase
|
20
|
+
set_purchased: (@purchased) ->
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
# class << LotteryDraw
|
25
|
+
# def play
|
26
|
+
# result = LotteryTicket.new_random
|
27
|
+
# winners = {}
|
28
|
+
# @@tickets.each do |buyer, ticket_list|
|
29
|
+
# ticket_list.each do |ticket|
|
30
|
+
# score = ticket.score( result )
|
31
|
+
# next if score.zero?
|
32
|
+
# winners[buyer] ||= []
|
33
|
+
# winners[buyer] << [ ticket, score ]
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
# @@tickets.clear
|
37
|
+
# winners
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
|
41
|
+
LotteryDraw =
|
42
|
+
play: ->
|
43
|
+
result = LotteryTicket.new_random()
|
44
|
+
winners = {}
|
45
|
+
this.tickets.each (buyer, ticket_list) ->
|
46
|
+
ticket_list.each (ticket) ->
|
47
|
+
score = ticket.score result
|
48
|
+
return if score is 0
|
49
|
+
winners[buyer] or= []
|
50
|
+
winners[buyer].push [ticket, score]
|
51
|
+
this.tickets = {}
|
52
|
+
winners
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
# module WishScanner
|
57
|
+
# def scan_for_a_wish
|
58
|
+
# wish = self.read.detect do |thought|
|
59
|
+
# thought.index( 'wish: ' ) == 0
|
60
|
+
# end
|
61
|
+
# wish.gsub( 'wish: ', '' )
|
62
|
+
# end
|
63
|
+
# end
|
64
|
+
|
65
|
+
WishScanner =
|
66
|
+
scan_for_a_wish: ->
|
67
|
+
wish = this.read().detect (thought) -> thought.index('wish: ') is 0
|
68
|
+
wish.replace 'wish: ', ''
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
# class Creature
|
73
|
+
#
|
74
|
+
# # This method applies a hit taken during a fight.
|
75
|
+
# def hit( damage )
|
76
|
+
# p_up = rand( charisma )
|
77
|
+
# if p_up % 9 == 7
|
78
|
+
# @life += p_up / 4
|
79
|
+
# console.log "[#{ self.class } magick powers up #{ p_up }!]"
|
80
|
+
# end
|
81
|
+
# @life -= damage
|
82
|
+
# console.log "[#{ self.class } has died.]" if @life <= 0
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# # This method takes one turn in a fight.
|
86
|
+
# def fight( enemy, weapon )
|
87
|
+
# if life <= 0
|
88
|
+
# console.log "[#{ self.class } is too dead to fight!]"
|
89
|
+
# return
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
# # Attack the opponent
|
93
|
+
# your_hit = rand( strength + weapon )
|
94
|
+
# console.log "[You hit with #{ your_hit } points of damage!]"
|
95
|
+
# enemy.hit( your_hit )
|
96
|
+
#
|
97
|
+
# # Retaliation
|
98
|
+
# p enemy
|
99
|
+
# if enemy.life > 0
|
100
|
+
# enemy_hit = rand( enemy.strength + enemy.weapon )
|
101
|
+
# console.log "[Your enemy hit with #{ enemy_hit } points of damage!]"
|
102
|
+
# self.hit( enemy_hit )
|
103
|
+
# end
|
104
|
+
# end
|
105
|
+
#
|
106
|
+
# end
|
107
|
+
|
108
|
+
Creature =
|
109
|
+
|
110
|
+
# This method applies a hit taken during a fight.
|
111
|
+
hit: (damage) ->
|
112
|
+
p_up = Math.rand this.charisma
|
113
|
+
if p_up % 9 is 7
|
114
|
+
this.life += p_up / 4
|
115
|
+
console.log "[" + this.name + " magick powers up " + p_up + "!]"
|
116
|
+
this.life -= damage
|
117
|
+
if this.life <= 0 then console.log "[" + this.name + " has died.]"
|
118
|
+
|
119
|
+
# This method takes one turn in a fight.
|
120
|
+
fight: (enemy, weapon) ->
|
121
|
+
if this.life <= 0 then return console.log "[" + this.name + "is too dead to fight!]"
|
122
|
+
|
123
|
+
# Attack the opponent.
|
124
|
+
your_hit = Math.rand this.strength + weapon
|
125
|
+
console.log "[You hit with " + your_hit + "points of damage!]"
|
126
|
+
enemy.hit your_hit
|
127
|
+
|
128
|
+
# Retaliation.
|
129
|
+
console.log enemy
|
130
|
+
if enemy.life > 0
|
131
|
+
enemy_hit = Math.rand enemy.strength + enemy.weapon
|
132
|
+
console.log "[Your enemy hit with " + enemy_hit + "points of damage!]"
|
133
|
+
this.hit enemy_hit
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
# # Get evil idea and swap in code words
|
138
|
+
# print "Enter your new idea: "
|
139
|
+
# idea = gets
|
140
|
+
# code_words.each do |real, code|
|
141
|
+
# idea.gsub!( real, code )
|
142
|
+
# end
|
143
|
+
#
|
144
|
+
# # Save the jibberish to a new file
|
145
|
+
# print "File encoded. Please enter a name for this idea: "
|
146
|
+
# idea_name = gets.strip
|
147
|
+
# File::open( "idea-" + idea_name + ".txt", "w" ) do |f|
|
148
|
+
# f << idea
|
149
|
+
# end
|
150
|
+
|
151
|
+
# Get evil idea and swap in code words
|
152
|
+
print "Enter your new idea: "
|
153
|
+
idea = gets()
|
154
|
+
code_words.each (real, code) -> idea.replace(real, code)
|
155
|
+
|
156
|
+
# Save the jibberish to a new file
|
157
|
+
print "File encoded. Please enter a name for this idea: "
|
158
|
+
idea_name = gets().strip()
|
159
|
+
File.open "idea-" + idea_name + '.txt', 'w', (file) -> file.write idea
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
# def wipe_mutterings_from( sentence )
|
164
|
+
# unless sentence.respond_to? :include?
|
165
|
+
# raise ArgumentError,
|
166
|
+
# "cannot wipe mutterings from a #{ sentence.class }"
|
167
|
+
# end
|
168
|
+
# while sentence.include? '('
|
169
|
+
# open = sentence.index( '(' )
|
170
|
+
# close = sentence.index( ')', open )
|
171
|
+
# sentence[open..close] = '' if close
|
172
|
+
# end
|
173
|
+
# end
|
174
|
+
|
175
|
+
wipe_mutterings_from = (sentence) ->
|
176
|
+
throw new Error "cannot wipe mutterings" unless sentence.indexOf
|
177
|
+
while sentence.indexOf('(') >= 0
|
178
|
+
open = sentence.indexOf('(') - 1
|
179
|
+
close = sentence.indexOf(')') + 1
|
180
|
+
sentence = sentence.slice(0, open) + sentence.slice(close, sentence.length)
|
181
|
+
sentence
|
@@ -0,0 +1,206 @@
|
|
1
|
+
# Examples from _why's Potion, the Readme and "Potion: A Short Pamphlet".
|
2
|
+
|
3
|
+
# 5 times: "Odelay!" print.
|
4
|
+
|
5
|
+
print "Odelay!" for i in [1..5]
|
6
|
+
|
7
|
+
|
8
|
+
# add = (x, y): x + y.
|
9
|
+
# add(2, 4) string print
|
10
|
+
|
11
|
+
add = (x, y) -> x + y
|
12
|
+
print add 2, 4
|
13
|
+
|
14
|
+
|
15
|
+
# loop: 'quaff' print.
|
16
|
+
|
17
|
+
loop print 'quaff'
|
18
|
+
|
19
|
+
|
20
|
+
# ('cheese', 'bread', 'mayo') at (1) print
|
21
|
+
|
22
|
+
print ['cheese', 'bread', 'mayo'][1]
|
23
|
+
|
24
|
+
|
25
|
+
# (language='Potion', pointless=true) at (key='language') print
|
26
|
+
|
27
|
+
print {language: 'Potion', pointless: true}['language']
|
28
|
+
|
29
|
+
|
30
|
+
# minus = (x, y): x - y.
|
31
|
+
# minus (y=10, x=6)
|
32
|
+
|
33
|
+
minus = (x, y) -> x - y
|
34
|
+
minus 6, 10
|
35
|
+
|
36
|
+
|
37
|
+
# foods = ('cheese', 'bread', 'mayo')
|
38
|
+
# foods (2)
|
39
|
+
|
40
|
+
foods = ['cheese', 'bread', 'mayo']
|
41
|
+
foods[2]
|
42
|
+
|
43
|
+
|
44
|
+
# (dog='canine', cat='feline', fox='vulpine') each (key, val):
|
45
|
+
# (key, ' is a ', val) join print.
|
46
|
+
|
47
|
+
for key, val of {dog: 'canine', cat: 'feline', fox: 'vulpine'}
|
48
|
+
print key + ' is a ' + val
|
49
|
+
|
50
|
+
|
51
|
+
# Person = class: /name, /age, /sex.
|
52
|
+
# Person print = ():
|
53
|
+
# ('My name is ', /name, '.') join print.
|
54
|
+
|
55
|
+
class Person
|
56
|
+
print: ->
|
57
|
+
print 'My name is ' + @name + '.'
|
58
|
+
|
59
|
+
|
60
|
+
# p = Person ()
|
61
|
+
# p /name string print
|
62
|
+
|
63
|
+
p = new Person
|
64
|
+
print p.name
|
65
|
+
|
66
|
+
|
67
|
+
# Policeman = Person class (rank): /rank = rank.
|
68
|
+
# Policeman print = ():
|
69
|
+
# ('My name is ', /name, ' and I'm a ', /rank, '.') join print.
|
70
|
+
#
|
71
|
+
# Policeman ('Constable') print
|
72
|
+
|
73
|
+
class Policeman extends Person
|
74
|
+
(@rank) ->
|
75
|
+
|
76
|
+
print: ->
|
77
|
+
print 'My name is ' + @name + " and I'm a " + @rank + '.'
|
78
|
+
|
79
|
+
print new Policeman 'Constable'
|
80
|
+
|
81
|
+
|
82
|
+
# app = [window (width=200, height=400)
|
83
|
+
# [para 'Welcome.', button 'OK']]
|
84
|
+
# app first name
|
85
|
+
|
86
|
+
app =
|
87
|
+
window:
|
88
|
+
width: 200
|
89
|
+
height: 200
|
90
|
+
para: 'Welcome.'
|
91
|
+
button: 'OK'
|
92
|
+
|
93
|
+
app.window
|
94
|
+
|
95
|
+
|
96
|
+
# x = 1
|
97
|
+
# y = 2
|
98
|
+
#
|
99
|
+
# x = 1, y = 2
|
100
|
+
|
101
|
+
x = 1
|
102
|
+
y = 2
|
103
|
+
|
104
|
+
x = 1; y = 2
|
105
|
+
|
106
|
+
|
107
|
+
# table = (language='Potion'
|
108
|
+
# pointless=true)
|
109
|
+
|
110
|
+
table =
|
111
|
+
language: 'Potion'
|
112
|
+
pointless: yes
|
113
|
+
|
114
|
+
|
115
|
+
# # this foul business...
|
116
|
+
# String length = (): 10.
|
117
|
+
|
118
|
+
# this foul business...
|
119
|
+
String::length = -> 10
|
120
|
+
|
121
|
+
|
122
|
+
# block = :
|
123
|
+
# 'potion' print.
|
124
|
+
|
125
|
+
block = ->
|
126
|
+
print 'potion'
|
127
|
+
|
128
|
+
|
129
|
+
# if (age > 100): 'ancient'.
|
130
|
+
|
131
|
+
if age > 100 then 'ancient'
|
132
|
+
|
133
|
+
|
134
|
+
# author =
|
135
|
+
# if (title == 'Jonathan Strange & Mr. Norrell'):
|
136
|
+
# 'Susanna Clarke'.
|
137
|
+
# elsif (title == 'The Star Diaries'):
|
138
|
+
# 'Stanislaw Lem'.
|
139
|
+
# elsif (title == 'The Slynx'):
|
140
|
+
# 'Tatyana Tolstaya'.
|
141
|
+
# else:
|
142
|
+
# '... probably Philip K. Dick'.
|
143
|
+
|
144
|
+
switch author
|
145
|
+
when 'Jonathan Strange & Mr. Norrell'
|
146
|
+
'Susanna Clarke'
|
147
|
+
when 'The Star Diaries'
|
148
|
+
'Stanislaw Lem'
|
149
|
+
when 'The Slynx'
|
150
|
+
'Tatyana Tolstaya'
|
151
|
+
else
|
152
|
+
'... probably Philip K. Dick'
|
153
|
+
|
154
|
+
|
155
|
+
# count = 8
|
156
|
+
# while (count > 0):
|
157
|
+
# 'quaff' print
|
158
|
+
# count--.
|
159
|
+
|
160
|
+
count = 8
|
161
|
+
while count > 0
|
162
|
+
print 'quaff'
|
163
|
+
count--
|
164
|
+
|
165
|
+
|
166
|
+
# 1 to 5 (a):
|
167
|
+
# a string print.
|
168
|
+
|
169
|
+
print a for a in [1..5]
|
170
|
+
|
171
|
+
|
172
|
+
# if (3 ?gender):
|
173
|
+
# "Huh? Numbers are sexed? That's amazing." print.
|
174
|
+
|
175
|
+
if 3.gender?
|
176
|
+
print "Huh? Numbers are sexed? That's amazing."
|
177
|
+
|
178
|
+
|
179
|
+
# HomePage get = (url):
|
180
|
+
# session = url query ? at ('session').
|
181
|
+
|
182
|
+
HomePage::get = (url) ->
|
183
|
+
session = url.query.session if url.query?
|
184
|
+
|
185
|
+
|
186
|
+
# BTree = class: /left, /right.
|
187
|
+
# b = BTree ()
|
188
|
+
# b /left = BTree ()
|
189
|
+
# b /right = BTree ()
|
190
|
+
|
191
|
+
BTree = ->
|
192
|
+
b = new BTree
|
193
|
+
b.left = new BTree
|
194
|
+
b.right = new BTree
|
195
|
+
|
196
|
+
|
197
|
+
# BTree = class: /left, /right.
|
198
|
+
# b = BTree ()
|
199
|
+
#
|
200
|
+
# if (b ? /left):
|
201
|
+
# 'left path found!' print.
|
202
|
+
|
203
|
+
BTree = ->
|
204
|
+
b = new BTree
|
205
|
+
|
206
|
+
print 'left path found!' if b.left?
|
@@ -0,0 +1,682 @@
|
|
1
|
+
# **Underscore.coffee
|
2
|
+
# (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.**
|
3
|
+
# Underscore is freely distributable under the terms of the
|
4
|
+
# [MIT license](http://en.wikipedia.org/wiki/MIT_License).
|
5
|
+
# Portions of Underscore are inspired by or borrowed from
|
6
|
+
# [Prototype.js](http://prototypejs.org/api), Oliver Steele's
|
7
|
+
# [Functional](http://osteele.com), and John Resig's
|
8
|
+
# [Micro-Templating](http://ejohn.org).
|
9
|
+
# For all details and documentation:
|
10
|
+
# http://documentcloud.github.com/underscore/
|
11
|
+
|
12
|
+
|
13
|
+
# Baseline setup
|
14
|
+
# --------------
|
15
|
+
|
16
|
+
# Establish the root object, `window` in the browser, or `global` on the server.
|
17
|
+
root = this
|
18
|
+
|
19
|
+
|
20
|
+
# Save the previous value of the `_` variable.
|
21
|
+
previousUnderscore = root._
|
22
|
+
|
23
|
+
|
24
|
+
# Establish the object that gets thrown to break out of a loop iteration.
|
25
|
+
# `StopIteration` is SOP on Mozilla.
|
26
|
+
breaker = if typeof(StopIteration) is 'undefined' then '__break__' else StopIteration
|
27
|
+
|
28
|
+
|
29
|
+
# Helper function to escape **RegExp** contents, because JS doesn't have one.
|
30
|
+
escapeRegExp = (string) -> string.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')
|
31
|
+
|
32
|
+
|
33
|
+
# Save bytes in the minified (but not gzipped) version:
|
34
|
+
ArrayProto = Array.prototype
|
35
|
+
ObjProto = Object.prototype
|
36
|
+
|
37
|
+
|
38
|
+
# Create quick reference variables for speed access to core prototypes.
|
39
|
+
slice = ArrayProto.slice
|
40
|
+
unshift = ArrayProto.unshift
|
41
|
+
toString = ObjProto.toString
|
42
|
+
hasOwnProperty = ObjProto.hasOwnProperty
|
43
|
+
propertyIsEnumerable = ObjProto.propertyIsEnumerable
|
44
|
+
|
45
|
+
|
46
|
+
# All **ECMA5** native implementations we hope to use are declared here.
|
47
|
+
nativeForEach = ArrayProto.forEach
|
48
|
+
nativeMap = ArrayProto.map
|
49
|
+
nativeReduce = ArrayProto.reduce
|
50
|
+
nativeReduceRight = ArrayProto.reduceRight
|
51
|
+
nativeFilter = ArrayProto.filter
|
52
|
+
nativeEvery = ArrayProto.every
|
53
|
+
nativeSome = ArrayProto.some
|
54
|
+
nativeIndexOf = ArrayProto.indexOf
|
55
|
+
nativeLastIndexOf = ArrayProto.lastIndexOf
|
56
|
+
nativeIsArray = Array.isArray
|
57
|
+
nativeKeys = Object.keys
|
58
|
+
|
59
|
+
|
60
|
+
# Create a safe reference to the Underscore object for use below.
|
61
|
+
_ = (obj) -> new wrapper(obj)
|
62
|
+
|
63
|
+
|
64
|
+
# Export the Underscore object for **CommonJS**.
|
65
|
+
if typeof(exports) != 'undefined' then exports._ = _
|
66
|
+
|
67
|
+
|
68
|
+
# Export Underscore to global scope.
|
69
|
+
root._ = _
|
70
|
+
|
71
|
+
|
72
|
+
# Current version.
|
73
|
+
_.VERSION = '1.1.0'
|
74
|
+
|
75
|
+
|
76
|
+
# Collection Functions
|
77
|
+
# --------------------
|
78
|
+
|
79
|
+
# The cornerstone, an **each** implementation.
|
80
|
+
# Handles objects implementing **forEach**, arrays, and raw objects.
|
81
|
+
_.each = (obj, iterator, context) ->
|
82
|
+
try
|
83
|
+
if nativeForEach and obj.forEach is nativeForEach
|
84
|
+
obj.forEach iterator, context
|
85
|
+
else if _.isNumber obj.length
|
86
|
+
iterator.call context, obj[i], i, obj for i in [0...obj.length]
|
87
|
+
else
|
88
|
+
iterator.call context, val, key, obj for own key, val of obj
|
89
|
+
catch e
|
90
|
+
throw e if e isnt breaker
|
91
|
+
obj
|
92
|
+
|
93
|
+
|
94
|
+
# Return the results of applying the iterator to each element. Use JavaScript
|
95
|
+
# 1.6's version of **map**, if possible.
|
96
|
+
_.map = (obj, iterator, context) ->
|
97
|
+
return obj.map(iterator, context) if nativeMap and obj.map is nativeMap
|
98
|
+
results = []
|
99
|
+
_.each obj, (value, index, list) ->
|
100
|
+
results.push iterator.call context, value, index, list
|
101
|
+
results
|
102
|
+
|
103
|
+
|
104
|
+
# **Reduce** builds up a single result from a list of values. Also known as
|
105
|
+
# **inject**, or **foldl**. Uses JavaScript 1.8's version of **reduce**, if possible.
|
106
|
+
_.reduce = (obj, iterator, memo, context) ->
|
107
|
+
if nativeReduce and obj.reduce is nativeReduce
|
108
|
+
iterator = _.bind iterator, context if context
|
109
|
+
return obj.reduce iterator, memo
|
110
|
+
_.each obj, (value, index, list) ->
|
111
|
+
memo = iterator.call context, memo, value, index, list
|
112
|
+
memo
|
113
|
+
|
114
|
+
|
115
|
+
# The right-associative version of **reduce**, also known as **foldr**. Uses
|
116
|
+
# JavaScript 1.8's version of **reduceRight**, if available.
|
117
|
+
_.reduceRight = (obj, iterator, memo, context) ->
|
118
|
+
if nativeReduceRight and obj.reduceRight is nativeReduceRight
|
119
|
+
iterator = _.bind iterator, context if context
|
120
|
+
return obj.reduceRight iterator, memo
|
121
|
+
reversed = _.clone(_.toArray(obj)).reverse()
|
122
|
+
_.reduce reversed, iterator, memo, context
|
123
|
+
|
124
|
+
|
125
|
+
# Return the first value which passes a truth test.
|
126
|
+
_.detect = (obj, iterator, context) ->
|
127
|
+
result = null
|
128
|
+
_.each obj, (value, index, list) ->
|
129
|
+
if iterator.call context, value, index, list
|
130
|
+
result = value
|
131
|
+
_.breakLoop()
|
132
|
+
result
|
133
|
+
|
134
|
+
|
135
|
+
# Return all the elements that pass a truth test. Use JavaScript 1.6's
|
136
|
+
# **filter**, if it exists.
|
137
|
+
_.filter = (obj, iterator, context) ->
|
138
|
+
return obj.filter iterator, context if nativeFilter and obj.filter is nativeFilter
|
139
|
+
results = []
|
140
|
+
_.each obj, (value, index, list) ->
|
141
|
+
results.push value if iterator.call context, value, index, list
|
142
|
+
results
|
143
|
+
|
144
|
+
|
145
|
+
# Return all the elements for which a truth test fails.
|
146
|
+
_.reject = (obj, iterator, context) ->
|
147
|
+
results = []
|
148
|
+
_.each obj, (value, index, list) ->
|
149
|
+
results.push value if not iterator.call context, value, index, list
|
150
|
+
results
|
151
|
+
|
152
|
+
|
153
|
+
# Determine whether all of the elements match a truth test. Delegate to
|
154
|
+
# JavaScript 1.6's **every**, if it is present.
|
155
|
+
_.every = (obj, iterator, context) ->
|
156
|
+
iterator ||= _.identity
|
157
|
+
return obj.every iterator, context if nativeEvery and obj.every is nativeEvery
|
158
|
+
result = true
|
159
|
+
_.each obj, (value, index, list) ->
|
160
|
+
_.breakLoop() unless (result = result and iterator.call(context, value, index, list))
|
161
|
+
result
|
162
|
+
|
163
|
+
|
164
|
+
# Determine if at least one element in the object matches a truth test. Use
|
165
|
+
# JavaScript 1.6's **some**, if it exists.
|
166
|
+
_.some = (obj, iterator, context) ->
|
167
|
+
iterator ||= _.identity
|
168
|
+
return obj.some iterator, context if nativeSome and obj.some is nativeSome
|
169
|
+
result = false
|
170
|
+
_.each obj, (value, index, list) ->
|
171
|
+
_.breakLoop() if (result = iterator.call(context, value, index, list))
|
172
|
+
result
|
173
|
+
|
174
|
+
|
175
|
+
# Determine if a given value is included in the array or object,
|
176
|
+
# based on `===`.
|
177
|
+
_.include = (obj, target) ->
|
178
|
+
return _.indexOf(obj, target) isnt -1 if nativeIndexOf and obj.indexOf is nativeIndexOf
|
179
|
+
return true for own key, val of obj when val is target
|
180
|
+
false
|
181
|
+
|
182
|
+
|
183
|
+
# Invoke a method with arguments on every item in a collection.
|
184
|
+
_.invoke = (obj, method) ->
|
185
|
+
args = _.rest arguments, 2
|
186
|
+
(if method then val[method] else val).apply(val, args) for val in obj
|
187
|
+
|
188
|
+
|
189
|
+
# Convenience version of a common use case of **map**: fetching a property.
|
190
|
+
_.pluck = (obj, key) ->
|
191
|
+
_.map(obj, (val) -> val[key])
|
192
|
+
|
193
|
+
|
194
|
+
# Return the maximum item or (item-based computation).
|
195
|
+
_.max = (obj, iterator, context) ->
|
196
|
+
return Math.max.apply(Math, obj) if not iterator and _.isArray(obj)
|
197
|
+
result = computed: -Infinity
|
198
|
+
_.each obj, (value, index, list) ->
|
199
|
+
computed = if iterator then iterator.call(context, value, index, list) else value
|
200
|
+
computed >= result.computed and (result = {value: value, computed: computed})
|
201
|
+
result.value
|
202
|
+
|
203
|
+
|
204
|
+
# Return the minimum element (or element-based computation).
|
205
|
+
_.min = (obj, iterator, context) ->
|
206
|
+
return Math.min.apply(Math, obj) if not iterator and _.isArray(obj)
|
207
|
+
result = computed: Infinity
|
208
|
+
_.each obj, (value, index, list) ->
|
209
|
+
computed = if iterator then iterator.call(context, value, index, list) else value
|
210
|
+
computed < result.computed and (result = {value: value, computed: computed})
|
211
|
+
result.value
|
212
|
+
|
213
|
+
|
214
|
+
# Sort the object's values by a criterion produced by an iterator.
|
215
|
+
_.sortBy = (obj, iterator, context) ->
|
216
|
+
_.pluck(((_.map obj, (value, index, list) ->
|
217
|
+
{value: value, criteria: iterator.call(context, value, index, list)}
|
218
|
+
).sort((left, right) ->
|
219
|
+
a = left.criteria; b = right.criteria
|
220
|
+
if a < b then -1 else if a > b then 1 else 0
|
221
|
+
)), 'value')
|
222
|
+
|
223
|
+
|
224
|
+
# Use a comparator function to figure out at what index an object should
|
225
|
+
# be inserted so as to maintain order. Uses binary search.
|
226
|
+
_.sortedIndex = (array, obj, iterator) ->
|
227
|
+
iterator ||= _.identity
|
228
|
+
low = 0
|
229
|
+
high = array.length
|
230
|
+
while low < high
|
231
|
+
mid = (low + high) >> 1
|
232
|
+
if iterator(array[mid]) < iterator(obj) then low = mid + 1 else high = mid
|
233
|
+
low
|
234
|
+
|
235
|
+
|
236
|
+
# Convert anything iterable into a real, live array.
|
237
|
+
_.toArray = (iterable) ->
|
238
|
+
return [] if (!iterable)
|
239
|
+
return iterable.toArray() if (iterable.toArray)
|
240
|
+
return iterable if (_.isArray(iterable))
|
241
|
+
return slice.call(iterable) if (_.isArguments(iterable))
|
242
|
+
_.values(iterable)
|
243
|
+
|
244
|
+
|
245
|
+
# Return the number of elements in an object.
|
246
|
+
_.size = (obj) -> _.toArray(obj).length
|
247
|
+
|
248
|
+
|
249
|
+
# Array Functions
|
250
|
+
# ---------------
|
251
|
+
|
252
|
+
# Get the first element of an array. Passing `n` will return the first N
|
253
|
+
# values in the array. Aliased as **head**. The `guard` check allows it to work
|
254
|
+
# with **map**.
|
255
|
+
_.first = (array, n, guard) ->
|
256
|
+
if n and not guard then slice.call(array, 0, n) else array[0]
|
257
|
+
|
258
|
+
|
259
|
+
# Returns everything but the first entry of the array. Aliased as **tail**.
|
260
|
+
# Especially useful on the arguments object. Passing an `index` will return
|
261
|
+
# the rest of the values in the array from that index onward. The `guard`
|
262
|
+
# check allows it to work with **map**.
|
263
|
+
_.rest = (array, index, guard) ->
|
264
|
+
slice.call(array, if _.isUndefined(index) or guard then 1 else index)
|
265
|
+
|
266
|
+
|
267
|
+
# Get the last element of an array.
|
268
|
+
_.last = (array) -> array[array.length - 1]
|
269
|
+
|
270
|
+
|
271
|
+
# Trim out all falsy values from an array.
|
272
|
+
_.compact = (array) -> item for item in array when item
|
273
|
+
|
274
|
+
|
275
|
+
# Return a completely flattened version of an array.
|
276
|
+
_.flatten = (array) ->
|
277
|
+
_.reduce array, (memo, value) ->
|
278
|
+
return memo.concat(_.flatten(value)) if _.isArray value
|
279
|
+
memo.push value
|
280
|
+
memo
|
281
|
+
, []
|
282
|
+
|
283
|
+
|
284
|
+
# Return a version of the array that does not contain the specified value(s).
|
285
|
+
_.without = (array) ->
|
286
|
+
values = _.rest arguments
|
287
|
+
val for val in _.toArray(array) when not _.include values, val
|
288
|
+
|
289
|
+
|
290
|
+
# Produce a duplicate-free version of the array. If the array has already
|
291
|
+
# been sorted, you have the option of using a faster algorithm.
|
292
|
+
_.uniq = (array, isSorted) ->
|
293
|
+
memo = []
|
294
|
+
for el, i in _.toArray array
|
295
|
+
memo.push el if i is 0 || (if isSorted is true then _.last(memo) isnt el else not _.include(memo, el))
|
296
|
+
memo
|
297
|
+
|
298
|
+
|
299
|
+
# Produce an array that contains every item shared between all the
|
300
|
+
# passed-in arrays.
|
301
|
+
_.intersect = (array) ->
|
302
|
+
rest = _.rest arguments
|
303
|
+
_.select _.uniq(array), (item) ->
|
304
|
+
_.all rest, (other) ->
|
305
|
+
_.indexOf(other, item) >= 0
|
306
|
+
|
307
|
+
|
308
|
+
# Zip together multiple lists into a single array -- elements that share
|
309
|
+
# an index go together.
|
310
|
+
_.zip = ->
|
311
|
+
length = _.max _.pluck arguments, 'length'
|
312
|
+
results = new Array length
|
313
|
+
for i in [0...length]
|
314
|
+
results[i] = _.pluck arguments, String i
|
315
|
+
results
|
316
|
+
|
317
|
+
|
318
|
+
# If the browser doesn't supply us with **indexOf** (I'm looking at you, MSIE),
|
319
|
+
# we need this function. Return the position of the first occurrence of an
|
320
|
+
# item in an array, or -1 if the item is not included in the array.
|
321
|
+
_.indexOf = (array, item) ->
|
322
|
+
return array.indexOf item if nativeIndexOf and array.indexOf is nativeIndexOf
|
323
|
+
i = 0; l = array.length
|
324
|
+
while l - i
|
325
|
+
if array[i] is item then return i else i++
|
326
|
+
-1
|
327
|
+
|
328
|
+
|
329
|
+
# Provide JavaScript 1.6's **lastIndexOf**, delegating to the native function,
|
330
|
+
# if possible.
|
331
|
+
_.lastIndexOf = (array, item) ->
|
332
|
+
return array.lastIndexOf(item) if nativeLastIndexOf and array.lastIndexOf is nativeLastIndexOf
|
333
|
+
i = array.length
|
334
|
+
while i
|
335
|
+
if array[i] is item then return i else i--
|
336
|
+
-1
|
337
|
+
|
338
|
+
|
339
|
+
# Generate an integer Array containing an arithmetic progression. A port of
|
340
|
+
# [the native Python **range** function](http://docs.python.org/library/functions.html#range).
|
341
|
+
_.range = (start, stop, step) ->
|
342
|
+
a = arguments
|
343
|
+
solo = a.length <= 1
|
344
|
+
i = start = if solo then 0 else a[0]
|
345
|
+
stop = if solo then a[0] else a[1]
|
346
|
+
step = a[2] or 1
|
347
|
+
len = Math.ceil((stop - start) / step)
|
348
|
+
return [] if len <= 0
|
349
|
+
range = new Array len
|
350
|
+
idx = 0
|
351
|
+
loop
|
352
|
+
return range if (if step > 0 then i - stop else stop - i) >= 0
|
353
|
+
range[idx] = i
|
354
|
+
idx++
|
355
|
+
i+= step
|
356
|
+
|
357
|
+
|
358
|
+
# Function Functions
|
359
|
+
# ------------------
|
360
|
+
|
361
|
+
# Create a function bound to a given object (assigning `this`, and arguments,
|
362
|
+
# optionally). Binding with arguments is also known as **curry**.
|
363
|
+
_.bind = (func, obj) ->
|
364
|
+
args = _.rest arguments, 2
|
365
|
+
-> func.apply obj or root, args.concat arguments
|
366
|
+
|
367
|
+
|
368
|
+
# Bind all of an object's methods to that object. Useful for ensuring that
|
369
|
+
# all callbacks defined on an object belong to it.
|
370
|
+
_.bindAll = (obj) ->
|
371
|
+
funcs = if arguments.length > 1 then _.rest(arguments) else _.functions(obj)
|
372
|
+
_.each funcs, (f) -> obj[f] = _.bind obj[f], obj
|
373
|
+
obj
|
374
|
+
|
375
|
+
|
376
|
+
# Delays a function for the given number of milliseconds, and then calls
|
377
|
+
# it with the arguments supplied.
|
378
|
+
_.delay = (func, wait) ->
|
379
|
+
args = _.rest arguments, 2
|
380
|
+
setTimeout((-> func.apply(func, args)), wait)
|
381
|
+
|
382
|
+
|
383
|
+
# Memoize an expensive function by storing its results.
|
384
|
+
_.memoize = (func, hasher) ->
|
385
|
+
memo = {}
|
386
|
+
hasher or= _.identity
|
387
|
+
->
|
388
|
+
key = hasher.apply this, arguments
|
389
|
+
return memo[key] if key of memo
|
390
|
+
memo[key] = func.apply this, arguments
|
391
|
+
|
392
|
+
|
393
|
+
# Defers a function, scheduling it to run after the current call stack has
|
394
|
+
# cleared.
|
395
|
+
_.defer = (func) ->
|
396
|
+
_.delay.apply _, [func, 1].concat _.rest arguments
|
397
|
+
|
398
|
+
|
399
|
+
# Returns the first function passed as an argument to the second,
|
400
|
+
# allowing you to adjust arguments, run code before and after, and
|
401
|
+
# conditionally execute the original function.
|
402
|
+
_.wrap = (func, wrapper) ->
|
403
|
+
-> wrapper.apply wrapper, [func].concat arguments
|
404
|
+
|
405
|
+
|
406
|
+
# Returns a function that is the composition of a list of functions, each
|
407
|
+
# consuming the return value of the function that follows.
|
408
|
+
_.compose = ->
|
409
|
+
funcs = arguments
|
410
|
+
->
|
411
|
+
args = arguments
|
412
|
+
for i in [funcs.length - 1..0] by -1
|
413
|
+
args = [funcs[i].apply(this, args)]
|
414
|
+
args[0]
|
415
|
+
|
416
|
+
|
417
|
+
# Object Functions
|
418
|
+
# ----------------
|
419
|
+
|
420
|
+
# Retrieve the names of an object's properties.
|
421
|
+
_.keys = nativeKeys or (obj) ->
|
422
|
+
return _.range 0, obj.length if _.isArray(obj)
|
423
|
+
key for key, val of obj
|
424
|
+
|
425
|
+
|
426
|
+
# Retrieve the values of an object's properties.
|
427
|
+
_.values = (obj) ->
|
428
|
+
_.map obj, _.identity
|
429
|
+
|
430
|
+
|
431
|
+
# Return a sorted list of the function names available in Underscore.
|
432
|
+
_.functions = (obj) ->
|
433
|
+
_.filter(_.keys(obj), (key) -> _.isFunction(obj[key])).sort()
|
434
|
+
|
435
|
+
|
436
|
+
# Extend a given object with all of the properties in a source object.
|
437
|
+
_.extend = (obj) ->
|
438
|
+
for source in _.rest(arguments)
|
439
|
+
obj[key] = val for key, val of source
|
440
|
+
obj
|
441
|
+
|
442
|
+
|
443
|
+
# Create a (shallow-cloned) duplicate of an object.
|
444
|
+
_.clone = (obj) ->
|
445
|
+
return obj.slice 0 if _.isArray obj
|
446
|
+
_.extend {}, obj
|
447
|
+
|
448
|
+
|
449
|
+
# Invokes interceptor with the obj, and then returns obj.
|
450
|
+
# The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
|
451
|
+
_.tap = (obj, interceptor) ->
|
452
|
+
interceptor obj
|
453
|
+
obj
|
454
|
+
|
455
|
+
|
456
|
+
# Perform a deep comparison to check if two objects are equal.
|
457
|
+
_.isEqual = (a, b) ->
|
458
|
+
# Check object identity.
|
459
|
+
return true if a is b
|
460
|
+
# Different types?
|
461
|
+
atype = typeof(a); btype = typeof(b)
|
462
|
+
return false if atype isnt btype
|
463
|
+
# Basic equality test (watch out for coercions).
|
464
|
+
return true if `a == b`
|
465
|
+
# One is falsy and the other truthy.
|
466
|
+
return false if (!a and b) or (a and !b)
|
467
|
+
# One of them implements an `isEqual()`?
|
468
|
+
return a.isEqual(b) if a.isEqual
|
469
|
+
# Check dates' integer values.
|
470
|
+
return a.getTime() is b.getTime() if _.isDate(a) and _.isDate(b)
|
471
|
+
# Both are NaN?
|
472
|
+
return false if _.isNaN(a) and _.isNaN(b)
|
473
|
+
# Compare regular expressions.
|
474
|
+
if _.isRegExp(a) and _.isRegExp(b)
|
475
|
+
return a.source is b.source and
|
476
|
+
a.global is b.global and
|
477
|
+
a.ignoreCase is b.ignoreCase and
|
478
|
+
a.multiline is b.multiline
|
479
|
+
# If a is not an object by this point, we can't handle it.
|
480
|
+
return false if atype isnt 'object'
|
481
|
+
# Check for different array lengths before comparing contents.
|
482
|
+
return false if a.length and (a.length isnt b.length)
|
483
|
+
# Nothing else worked, deep compare the contents.
|
484
|
+
aKeys = _.keys(a); bKeys = _.keys(b)
|
485
|
+
# Different object sizes?
|
486
|
+
return false if aKeys.length isnt bKeys.length
|
487
|
+
# Recursive comparison of contents.
|
488
|
+
return false for key, val of a when !(key of b) or !_.isEqual(val, b[key])
|
489
|
+
true
|
490
|
+
|
491
|
+
|
492
|
+
# Is a given array or object empty?
|
493
|
+
_.isEmpty = (obj) ->
|
494
|
+
return obj.length is 0 if _.isArray(obj) or _.isString(obj)
|
495
|
+
return false for own key of obj
|
496
|
+
true
|
497
|
+
|
498
|
+
|
499
|
+
# Is a given value a DOM element?
|
500
|
+
_.isElement = (obj) -> obj and obj.nodeType is 1
|
501
|
+
|
502
|
+
|
503
|
+
# Is a given value an array?
|
504
|
+
_.isArray = nativeIsArray or (obj) -> !!(obj and obj.concat and obj.unshift and not obj.callee)
|
505
|
+
|
506
|
+
|
507
|
+
# Is a given variable an arguments object?
|
508
|
+
_.isArguments = (obj) -> obj and obj.callee
|
509
|
+
|
510
|
+
|
511
|
+
# Is the given value a function?
|
512
|
+
_.isFunction = (obj) -> !!(obj and obj.constructor and obj.call and obj.apply)
|
513
|
+
|
514
|
+
|
515
|
+
# Is the given value a string?
|
516
|
+
_.isString = (obj) -> !!(obj is '' or (obj and obj.charCodeAt and obj.substr))
|
517
|
+
|
518
|
+
|
519
|
+
# Is a given value a number?
|
520
|
+
_.isNumber = (obj) -> (obj is +obj) or toString.call(obj) is '[object Number]'
|
521
|
+
|
522
|
+
|
523
|
+
# Is a given value a boolean?
|
524
|
+
_.isBoolean = (obj) -> obj is true or obj is false
|
525
|
+
|
526
|
+
|
527
|
+
# Is a given value a Date?
|
528
|
+
_.isDate = (obj) -> !!(obj and obj.getTimezoneOffset and obj.setUTCFullYear)
|
529
|
+
|
530
|
+
|
531
|
+
# Is the given value a regular expression?
|
532
|
+
_.isRegExp = (obj) -> !!(obj and obj.exec and (obj.ignoreCase or obj.ignoreCase is false))
|
533
|
+
|
534
|
+
|
535
|
+
# Is the given value NaN -- this one is interesting. `NaN != NaN`, and
|
536
|
+
# `isNaN(undefined) == true`, so we make sure it's a number first.
|
537
|
+
_.isNaN = (obj) -> _.isNumber(obj) and window.isNaN(obj)
|
538
|
+
|
539
|
+
|
540
|
+
# Is a given value equal to null?
|
541
|
+
_.isNull = (obj) -> obj is null
|
542
|
+
|
543
|
+
|
544
|
+
# Is a given variable undefined?
|
545
|
+
_.isUndefined = (obj) -> typeof obj is 'undefined'
|
546
|
+
|
547
|
+
|
548
|
+
# Utility Functions
|
549
|
+
# -----------------
|
550
|
+
|
551
|
+
# Run Underscore.js in noConflict mode, returning the `_` variable to its
|
552
|
+
# previous owner. Returns a reference to the Underscore object.
|
553
|
+
_.noConflict = ->
|
554
|
+
root._ = previousUnderscore
|
555
|
+
this
|
556
|
+
|
557
|
+
|
558
|
+
# Keep the identity function around for default iterators.
|
559
|
+
_.identity = (value) -> value
|
560
|
+
|
561
|
+
|
562
|
+
# Run a function `n` times.
|
563
|
+
_.times = (n, iterator, context) ->
|
564
|
+
iterator.call context, i for i in [0...n]
|
565
|
+
|
566
|
+
|
567
|
+
# Break out of the middle of an iteration.
|
568
|
+
_.breakLoop = -> throw breaker
|
569
|
+
|
570
|
+
|
571
|
+
# Add your own custom functions to the Underscore object, ensuring that
|
572
|
+
# they're correctly added to the OOP wrapper as well.
|
573
|
+
_.mixin = (obj) ->
|
574
|
+
for name in _.functions(obj)
|
575
|
+
addToWrapper name, _[name] = obj[name]
|
576
|
+
|
577
|
+
|
578
|
+
# Generate a unique integer id (unique within the entire client session).
|
579
|
+
# Useful for temporary DOM ids.
|
580
|
+
idCounter = 0
|
581
|
+
_.uniqueId = (prefix) ->
|
582
|
+
(prefix or '') + idCounter++
|
583
|
+
|
584
|
+
|
585
|
+
# By default, Underscore uses **ERB**-style template delimiters, change the
|
586
|
+
# following template settings to use alternative delimiters.
|
587
|
+
_.templateSettings = {
|
588
|
+
start: '<%'
|
589
|
+
end: '%>'
|
590
|
+
interpolate: /<%=(.+?)%>/g
|
591
|
+
}
|
592
|
+
|
593
|
+
|
594
|
+
# JavaScript templating a-la **ERB**, pilfered from John Resig's
|
595
|
+
# *Secrets of the JavaScript Ninja*, page 83.
|
596
|
+
# Single-quote fix from Rick Strahl.
|
597
|
+
# With alterations for arbitrary delimiters, and to preserve whitespace.
|
598
|
+
_.template = (str, data) ->
|
599
|
+
c = _.templateSettings
|
600
|
+
endMatch = new RegExp("'(?=[^"+c.end.substr(0, 1)+"]*"+escapeRegExp(c.end)+")","g")
|
601
|
+
fn = new Function 'obj',
|
602
|
+
'var p=[],print=function(){p.push.apply(p,arguments);};' +
|
603
|
+
'with(obj||{}){p.push(\'' +
|
604
|
+
str.replace(/\r/g, '\\r')
|
605
|
+
.replace(/\n/g, '\\n')
|
606
|
+
.replace(/\t/g, '\\t')
|
607
|
+
.replace(endMatch,"✄")
|
608
|
+
.split("'").join("\\'")
|
609
|
+
.split("✄").join("'")
|
610
|
+
.replace(c.interpolate, "',$1,'")
|
611
|
+
.split(c.start).join("');")
|
612
|
+
.split(c.end).join("p.push('") +
|
613
|
+
"');}return p.join('');"
|
614
|
+
if data then fn(data) else fn
|
615
|
+
|
616
|
+
|
617
|
+
# Aliases
|
618
|
+
# -------
|
619
|
+
|
620
|
+
_.forEach = _.each
|
621
|
+
_.foldl = _.inject = _.reduce
|
622
|
+
_.foldr = _.reduceRight
|
623
|
+
_.select = _.filter
|
624
|
+
_.all = _.every
|
625
|
+
_.any = _.some
|
626
|
+
_.contains = _.include
|
627
|
+
_.head = _.first
|
628
|
+
_.tail = _.rest
|
629
|
+
_.methods = _.functions
|
630
|
+
|
631
|
+
|
632
|
+
# Setup the OOP Wrapper
|
633
|
+
# ---------------------
|
634
|
+
|
635
|
+
# If Underscore is called as a function, it returns a wrapped object that
|
636
|
+
# can be used OO-style. This wrapper holds altered versions of all the
|
637
|
+
# underscore functions. Wrapped objects may be chained.
|
638
|
+
wrapper = (obj) ->
|
639
|
+
this._wrapped = obj
|
640
|
+
this
|
641
|
+
|
642
|
+
|
643
|
+
# Helper function to continue chaining intermediate results.
|
644
|
+
result = (obj, chain) ->
|
645
|
+
if chain then _(obj).chain() else obj
|
646
|
+
|
647
|
+
|
648
|
+
# A method to easily add functions to the OOP wrapper.
|
649
|
+
addToWrapper = (name, func) ->
|
650
|
+
wrapper.prototype[name] = ->
|
651
|
+
args = _.toArray arguments
|
652
|
+
unshift.call args, this._wrapped
|
653
|
+
result func.apply(_, args), this._chain
|
654
|
+
|
655
|
+
|
656
|
+
# Add all ofthe Underscore functions to the wrapper object.
|
657
|
+
_.mixin _
|
658
|
+
|
659
|
+
|
660
|
+
# Add all mutator Array functions to the wrapper.
|
661
|
+
_.each ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], (name) ->
|
662
|
+
method = Array.prototype[name]
|
663
|
+
wrapper.prototype[name] = ->
|
664
|
+
method.apply(this._wrapped, arguments)
|
665
|
+
result(this._wrapped, this._chain)
|
666
|
+
|
667
|
+
|
668
|
+
# Add all accessor Array functions to the wrapper.
|
669
|
+
_.each ['concat', 'join', 'slice'], (name) ->
|
670
|
+
method = Array.prototype[name]
|
671
|
+
wrapper.prototype[name] = ->
|
672
|
+
result(method.apply(this._wrapped, arguments), this._chain)
|
673
|
+
|
674
|
+
|
675
|
+
# Start chaining a wrapped Underscore object.
|
676
|
+
wrapper::chain = ->
|
677
|
+
this._chain = true
|
678
|
+
this
|
679
|
+
|
680
|
+
|
681
|
+
# Extracts the result from a wrapped and chained object.
|
682
|
+
wrapper::value = -> this._wrapped
|