diff --git a/coffee-script.gemspec b/coffee-script.gemspec index 902be5bd43..8a4aecc4ef 100644 --- a/coffee-script.gemspec +++ b/coffee-script.gemspec @@ -1,7 +1,7 @@ Gem::Specification.new do |s| s.name = 'coffee-script' - s.version = '0.2.3' # Keep version in sync with coffee-script.rb - s.date = '2010-1-10' + s.version = '0.2.4' # Keep version in sync with coffee-script.rb + s.date = '2010-1-12' s.homepage = "http://jashkenas.github.com/coffee-script/" s.summary = "The CoffeeScript Compiler" diff --git a/documentation/coffee/heredocs.coffee b/documentation/coffee/heredocs.coffee new file mode 100644 index 0000000000..10769ea61a --- /dev/null +++ b/documentation/coffee/heredocs.coffee @@ -0,0 +1,5 @@ +html: ''' + + cup of coffeescript + + ''' \ No newline at end of file diff --git a/documentation/coffee/multiple_return_values.coffee b/documentation/coffee/multiple_return_values.coffee new file mode 100644 index 0000000000..d175cc8043 --- /dev/null +++ b/documentation/coffee/multiple_return_values.coffee @@ -0,0 +1,5 @@ +weather_report: location => + # Make an Ajax request to fetch the weather... + [location, 72, "Mostly Sunny"] + +[city, temp, forecast]: weather_report("Berkeley, CA") \ No newline at end of file diff --git a/documentation/coffee/object_extraction.coffee b/documentation/coffee/object_extraction.coffee new file mode 100644 index 0000000000..109f81e266 --- /dev/null +++ b/documentation/coffee/object_extraction.coffee @@ -0,0 +1,13 @@ +futurists: { + sculptor: "Umberto Boccioni" + painter: "Vladimir Burliuk" + poet: { + name: "F.T. Marinetti" + address: [ + "Via Roma 42R" + "Bellagio, Italy 22021" + ] + } +} + +{poet: {name: poet, address: [street, city]}}: futurists \ No newline at end of file diff --git a/documentation/coffee/parallel_assignment.coffee b/documentation/coffee/parallel_assignment.coffee new file mode 100644 index 0000000000..ea005b3b25 --- /dev/null +++ b/documentation/coffee/parallel_assignment.coffee @@ -0,0 +1,4 @@ +bait: 1000 +and_switch: 0 + +[bait, and_switch]: [and_switch, bait] \ No newline at end of file diff --git a/documentation/index.html.erb b/documentation/index.html.erb index f79ff39fb6..f2ae351358 100644 --- a/documentation/index.html.erb +++ b/documentation/index.html.erb @@ -51,7 +51,7 @@
Latest Version: - 0.2.3 + 0.2.4
Comprehensions can also be used to iterate over the keys and values in - an object. Use of to signal comprehension over the properties of + an object. Use of to signal comprehension over the properties of an object instead of the values in an array.
<%= code_for('object_comprehensions', 'ages.join(", ")') %> @@ -481,12 +482,12 @@ coffee --print app/scripts/*.coffee > concatenation.js be completely usable if it weren't for a couple of small exceptions: it's awkward to call super (the prototype object's implementation of the current function), and it's awkward to correctly - set the prototype chain. + set the prototype chain.CoffeeScript provides extends to help with prototype setup, :: for quick access to an - object's prototype, and converts super() into a call against + object's prototype, and converts super() into a call against the immediate ancestor's method of the same name.
<%= code_for('super', true) %> @@ -499,11 +500,39 @@ coffee --print app/scripts/*.coffee > concatenation.js so you don't have to close the parentheses on the other side. <%= code_for('blocks') %> ++ If you prefer not to use blocks, you'll need to add a pair of parentheses + to help distinguish the arguments from the definition of the function: + _.map(array, (num => num * 2)) +
+ ++ Pattern Matching (Destructuring Assignment) + To make extracting values from complex arrays and objects more convenient, + CoffeeScript implements ECMAScript Harmony's proposed + destructuring assignment + syntax. When you assign an array or object literal to a value, CoffeeScript + breaks up and matches both sides against each other, assigning the values + on the right to the variables on the left. In the simplest case, it can be + used for parallel assignment: +
+ <%= code_for('parallel_assignment', 'bait') %> ++ But it's also helpful for dealing with functions that return multiple + values. +
+ <%= code_for('multiple_return_values', 'forecast') %> ++ Pattern matching can be used with any depth of array and object nesting, + to help pull out deeply nested properties. +
+ <%= code_for('object_extraction', 'poet + " — " + street') %>Embedded JavaScript - If you ever need to interpolate literal JavaScript snippets, you can - use backticks to pass JavaScript straight through. + Hopefully, you'll never need to use it, but if you ever need to intersperse + snippets of JavaScript within your CoffeeScript, you can + use backticks to pass it straight through.
<%= code_for('embedded', 'hi()') %> @@ -527,10 +556,17 @@ coffee --print app/scripts/*.coffee > concatenation.js <%= code_for('try') %>- Multiline Strings + Multiline Strings and Heredocs Multiline strings are allowed in CoffeeScript.
<%= code_for('strings', 'moby_dick') %> ++ Heredocs can be used to hold formatted or indentation-sensitive text + (or, if you just don't feel like escaping quotes and apostrophes). The + indentation level that begins the heredoc is maintained throughout, so + you can keep it all aligned with the body of your code. +
+ <%= code_for('heredocs') %>+ 0.2.4 + Added ECMAScript Harmony style destructuring assignment, for dealing with + extracting values from nested arrays and objects. Added indentation-sensitive + heredocs for nicely formatted strings or chunks of code. +
+0.2.3 Axed the unsatisfactory ino keyword, replacing it with of for - object comprehensions. They now look like: for key, value of object. + object comprehensions. They now look like: for prop, value of object.
diff --git a/documentation/js/array_comprehensions.js b/documentation/js/array_comprehensions.js index f7ce5e09ca..fe7d5db668 100644 --- a/documentation/js/array_comprehensions.js +++ b/documentation/js/array_comprehensions.js @@ -2,12 +2,12 @@ var __a, __b, __c, __d, __e, __f, __g, food, lunch, roid, roid2; // Eat lunch. lunch = (function() { - __c = []; __a = ['toast', 'cheese', 'wine']; - for (__b=0; __b<__a.length; __b++) { - food = __a[__b]; - __c.push(eat(food)); + __a = []; __b = ['toast', 'cheese', 'wine']; + for (__c=0; __c<__b.length; __c++) { + food = __b[__c]; + __a.push(eat(food)); } - return __c; + return __a; })(); // Naive collision detection. __d = asteroids; diff --git a/documentation/js/expressions_comprehension.js b/documentation/js/expressions_comprehension.js index 262bab7ee5..2d159d9f4f 100644 --- a/documentation/js/expressions_comprehension.js +++ b/documentation/js/expressions_comprehension.js @@ -2,12 +2,12 @@ var __a, __b, globals, name; // The first ten global properties. globals = ((function() { - __b = []; __a = window; - for (name in __a) { - if (__a.hasOwnProperty(name)) { - __b.push(name); + __a = []; __b = window; + for (name in __b) { + if (__b.hasOwnProperty(name)) { + __a.push(name); } } - return __b; + return __a; })()).slice(0, 10); })(); \ No newline at end of file diff --git a/documentation/js/heredocs.js b/documentation/js/heredocs.js new file mode 100644 index 0000000000..a523ff72f5 --- /dev/null +++ b/documentation/js/heredocs.js @@ -0,0 +1,4 @@ +(function(){ + var html; + html = "\n cup of coffeescript\n"; +})(); \ No newline at end of file diff --git a/documentation/js/multiple_return_values.js b/documentation/js/multiple_return_values.js new file mode 100644 index 0000000000..5ea8d80fbd --- /dev/null +++ b/documentation/js/multiple_return_values.js @@ -0,0 +1,11 @@ +(function(){ + var __a, city, forecast, temp, weather_report; + weather_report = function weather_report(location) { + // Make an Ajax request to fetch the weather... + return [location, 72, "Mostly Sunny"]; + }; + __a = weather_report("Berkeley, CA"); + city = __a[0]; + temp = __a[1]; + forecast = __a[2]; +})(); \ No newline at end of file diff --git a/documentation/js/object_comprehensions.js b/documentation/js/object_comprehensions.js index c3865f6ed4..81d94a417c 100644 --- a/documentation/js/object_comprehensions.js +++ b/documentation/js/object_comprehensions.js @@ -6,13 +6,13 @@ tim: 11 }; ages = (function() { - __b = []; __a = years_old; - for (child in __a) { - age = __a[child]; - if (__a.hasOwnProperty(child)) { - __b.push(child + " is " + age); + __a = []; __b = years_old; + for (child in __b) { + age = __b[child]; + if (__b.hasOwnProperty(child)) { + __a.push(child + " is " + age); } } - return __b; + return __a; })(); })(); \ No newline at end of file diff --git a/documentation/js/object_extraction.js b/documentation/js/object_extraction.js new file mode 100644 index 0000000000..ca41aa189d --- /dev/null +++ b/documentation/js/object_extraction.js @@ -0,0 +1,17 @@ +(function(){ + var __a, __b, __c, city, futurists, poet, street; + futurists = { + sculptor: "Umberto Boccioni", + painter: "Vladimir Burliuk", + poet: { + name: "F.T. Marinetti", + address: ["Via Roma 42R", "Bellagio, Italy 22021"] + } + }; + __a = futurists; + __b = __a.poet; + poet = __b.name; + __c = __b.address; + street = __c[0]; + city = __c[1]; +})(); \ No newline at end of file diff --git a/documentation/js/overview.js b/documentation/js/overview.js index 6ecf2944d8..1e61b37199 100644 --- a/documentation/js/overview.js +++ b/documentation/js/overview.js @@ -33,11 +33,11 @@ } // Array comprehensions: cubed_list = (function() { - __c = []; __a = list; - for (__b=0; __b<__a.length; __b++) { - num = __a[__b]; - __c.push(math.cube(num)); + __a = []; __b = list; + for (__c=0; __c<__b.length; __c++) { + num = __b[__c]; + __a.push(math.cube(num)); } - return __c; + return __a; })(); })(); \ No newline at end of file diff --git a/documentation/js/parallel_assignment.js b/documentation/js/parallel_assignment.js new file mode 100644 index 0000000000..5286f1539e --- /dev/null +++ b/documentation/js/parallel_assignment.js @@ -0,0 +1,8 @@ +(function(){ + var __a, and_switch, bait; + bait = 1000; + and_switch = 0; + __a = [and_switch, bait]; + bait = __a[0]; + and_switch = __a[1]; +})(); \ No newline at end of file diff --git a/documentation/js/range_comprehensions.js b/documentation/js/range_comprehensions.js index 8631bb6cc2..9272dcff38 100644 --- a/documentation/js/range_comprehensions.js +++ b/documentation/js/range_comprehensions.js @@ -1,21 +1,21 @@ (function(){ var __a, __b, __c, __d, __e, countdown, egg_delivery, num; countdown = (function() { - __b = []; __d = 10; __e = 1; + __a = []; __d = 10; __e = 1; for (__c=0, num=__d; (__d <= __e ? num <= __e : num >= __e); (__d <= __e ? num += 1 : num -= 1), __c++) { - __b.push(num); + __a.push(num); } - return __b; + return __a; })(); egg_delivery = function egg_delivery() { var __f, __g, __h, __i, __j, dozen_eggs, i; - __g = []; __i = 0; __j = eggs.length; + __f = []; __i = 0; __j = eggs.length; for (__h=0, i=__i; (__i <= __j ? i < __j : i > __j); (__i <= __j ? i += 12 : i -= 12), __h++) { - __g.push((function() { + __f.push((function() { dozen_eggs = eggs.slice(i, i + 12); return deliver(new egg_carton(dozen)); })()); } - return __g; + return __f; }; })(); \ No newline at end of file diff --git a/index.html b/index.html index 63bfc3907f..dd74befcc5 100644 --- a/index.html +++ b/index.html @@ -37,7 +37,7 @@
Latest Version: - 0.2.3 + 0.2.4
var __a, __b, __c, __d, __e, __f, __g, food, lunch, roid, roid2; // Eat lunch. lunch = (function() { - __c = []; __a = ['toast', 'cheese', 'wine']; - for (__b=0; __b<__a.length; __b++) { - food = __a[__b]; - __c.push(eat(food)); + __a = []; __b = ['toast', 'cheese', 'wine']; + for (__c=0; __c<__b.length; __c++) { + food = __b[__c]; + __a.push(eat(food)); } - return __c; + return __a; })(); // Naive collision detection. __d = asteroids; @@ -743,46 +744,46 @@Language Reference
deliver(new egg_carton(dozen))
var __a, __b, __c, __d, __e, countdown, egg_delivery, num; countdown = (function() { - __b = []; __d = 10; __e = 1; + __a = []; __d = 10; __e = 1; for (__c=0, num=__d; (__d <= __e ? num <= __e : num >= __e); (__d <= __e ? num += 1 : num -= 1), __c++) { - __b.push(num); + __a.push(num); } - return __b; + return __a; })(); egg_delivery = function egg_delivery() { var __f, __g, __h, __i, __j, dozen_eggs, i; - __g = []; __i = 0; __j = eggs.length; + __f = []; __i = 0; __j = eggs.length; for (__h=0, i=__i; (__i <= __j ? i < __j : i > __j); (__i <= __j ? i += 12 : i -= 12), __h++) { - __g.push((function() { + __f.push((function() { dozen_eggs = eggs.slice(i, i + 12); return deliver(new egg_carton(dozen)); })()); } - return __g; + return __f; };
Comprehensions can also be used to iterate over the keys and values in - an object. Use of to signal comprehension over the properties of + an object. Use of to signal comprehension over the properties of an object instead of the values in an array.
years_old: {max: 10, ida: 9, tim: 11} @@ -796,14 +797,14 @@Language Reference
tim: 11 }; ages = (function() { - __b = []; __a = years_old; - for (child in __a) { - age = __a[child]; - if (__a.hasOwnProperty(child)) { - __b.push(child + " is " + age); + __a = []; __b = years_old; + for (child in __b) { + age = __b[child]; + if (__b.hasOwnProperty(child)) { + __a.push(child + " is " + age); } } - return __b; + return __a; })();
var __a, __b, globals, name; // The first ten global properties. globals = ((function() { - __b = []; __a = window; - for (name in __a) { - if (__a.hasOwnProperty(name)) { - __b.push(name); + __a = []; __b = window; + for (name in __b) { + if (__b.hasOwnProperty(name)) { + __a.push(name); } } - return __b; + return __a; })()).slice(0, 10);
@@ -990,12 +991,12 @@
CoffeeScript provides extends to help with prototype setup, :: for quick access to an - object's prototype, and converts super() into a call against + object's prototype, and converts super() into a call against the immediate ancestor's method of the same name.
Animal: => @@ -1119,11 +1120,121 @@Language Reference
}); });
+ If you prefer not to use blocks, you'll need to add a pair of parentheses + to help distinguish the arguments from the definition of the function: + _.map(array, (num => num * 2)) +
+ ++ Pattern Matching (Destructuring Assignment) + To make extracting values from complex arrays and objects more convenient, + CoffeeScript implements ECMAScript Harmony's proposed + destructuring assignment + syntax. When you assign an array or object literal to a value, CoffeeScript + breaks up and matches both sides against each other, assigning the values + on the right to the variables on the left. In the simplest case, it can be + used for parallel assignment: +
+bait: 1000 +and_switch: 0 + +[bait, and_switch]: [and_switch, bait] +
var __a, and_switch, bait; +bait = 1000; +and_switch = 0; +__a = [and_switch, bait]; +bait = __a[0]; +and_switch = __a[1]; +
+ But it's also helpful for dealing with functions that return multiple + values. +
+weather_report: location => + # Make an Ajax request to fetch the weather... + [location, 72, "Mostly Sunny"] + +[city, temp, forecast]: weather_report("Berkeley, CA") +
var __a, city, forecast, temp, weather_report; +weather_report = function weather_report(location) { + // Make an Ajax request to fetch the weather... + return [location, 72, "Mostly Sunny"]; +}; +__a = weather_report("Berkeley, CA"); +city = __a[0]; +temp = __a[1]; +forecast = __a[2]; +
+ Pattern matching can be used with any depth of array and object nesting, + to help pull out deeply nested properties. +
+futurists: { + sculptor: "Umberto Boccioni" + painter: "Vladimir Burliuk" + poet: { + name: "F.T. Marinetti" + address: [ + "Via Roma 42R" + "Bellagio, Italy 22021" + ] + } +} + +{poet: {name: poet, address: [street, city]}}: futurists +
var __a, __b, __c, city, futurists, poet, street; +futurists = { + sculptor: "Umberto Boccioni", + painter: "Vladimir Burliuk", + poet: { + name: "F.T. Marinetti", + address: ["Via Roma 42R", "Bellagio, Italy 22021"] + } +}; +__a = futurists; +__b = __a.poet; +poet = __b.name; +__c = __b.address; +street = __c[0]; +city = __c[1]; +
Embedded JavaScript - If you ever need to interpolate literal JavaScript snippets, you can - use backticks to pass JavaScript straight through. + Hopefully, you'll never need to use it, but if you ever need to intersperse + snippets of JavaScript within your CoffeeScript, you can + use backticks to pass it straight through.
hi: `function() { return [document.title, "Hello JavaScript"].join(": "); @@ -1198,7 +1309,7 @@Language Reference
- Multiline Strings + Multiline Strings and Heredocs Multiline strings are allowed in CoffeeScript.
moby_dick: "Call me Ishmael. Some years ago -- @@ -1224,6 +1335,20 @@Language Reference
about a little and see the watery part of the \ world..."; ;alert(moby_dick);'>run: moby_dick
+ Heredocs can be used to hold formatted or indentation-sensitive text + (or, if you just don't feel like escaping quotes and apostrophes). The + indentation level that begins the heredoc is maintained throughout, so + you can keep it all aligned with the body of your code. +
+html: ''' + <strong> + cup of coffeescript + </strong> + ''' +
var html; +html = "<strong>\n cup of coffeescript\n</strong>"; +
+ 0.2.4 + Added ECMAScript Harmony style destructuring assignment, for dealing with + extracting values from nested arrays and objects. Added indentation-sensitive + heredocs for nicely formatted strings or chunks of code. +
+0.2.3 Axed the unsatisfactory ino keyword, replacing it with of for - object comprehensions. They now look like: for key, value of object. + object comprehensions. They now look like: for prop, value of object.
diff --git a/lib/coffee-script.rb b/lib/coffee-script.rb
index e694423103..3fc2d6ad32 100644
--- a/lib/coffee-script.rb
+++ b/lib/coffee-script.rb
@@ -10,7 +10,7 @@
# Namespace for all CoffeeScript internal classes.
module CoffeeScript
- VERSION = '0.2.3' # Keep in sync with the gemspec.
+ VERSION = '0.2.4' # Keep in sync with the gemspec.
# Compile a script (String or IO) to JavaScript.
def self.compile(script, options={})
diff --git a/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage b/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage
index 5a60ab242e..311872442d 100644
--- a/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage
+++ b/lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage
@@ -93,6 +93,30 @@