toffeescript
toffeescript copied to clipboard
CoffeeScript with async syntax and some additional features
ToffeeScript
ToffeeScript is a CoffeeScript dialect with Asynchronous Grammar
Features
- Asynchronous everywhere
- Condition: If, Switch
- Loop: For In, For Of, While with guard
when
- Mathematics
- Logical Operation
- Auto Callback
- Regexp Operator
=~
and matches\~
,\&
,\0
~\9
- High efficent code generated.
- Sourcemap Supported.
- Follow up to CoffeeScript 1.6.2 so far
- Safety named-function supported.
- Added in ToffeeScript 1.6.2-3
Installation
npm install toffee-script
Named Function
ToffeeScript support named function which is different from CoffeeScript. if the function defined in the first level of code block and the function name haven't been used, then compile it as named function. see Code Examples section It won't have compatible issue with CoffeeScript except one case
# m never declared above, m must be local variable and assign to undefined
m()
m = ->
m
in CoffeeScript will throw exception undefined is not function
. Use m as constant undefined variable is rare case.
in ToffeeScript function m is hoisted, and will run function m() as Javascript does.
Code Examples
Left: ToffeeScript
Right: Generated JavaScript
Basic
x, y = a! b console.log x, y |
var x, y, _this = this; |
with powerful CoffeeScript assignment [...]
[@x, y...] = a! b console.log @x, y |
var y, _this = this, __slice = [].slice; |
Condition
if i x = a! else y = b! console.log x, y |
var x, y, _this = this; |
Async in condition
if e = a! return cb(e) foo() |
var e, _this = this; |
Async in condition with multi return
Async call always return first argument
if e, data = fs.readFile! 'foo' return cb(e) console.log data |
var data, e, _this = this; |
Loop
Support For In, For Of, While with guard when
xs = for i in [1..3] when i > 2 a! # return arguments[0] in default |
var i, xs, _$res$_1, _i, _this = this; |
Mathematics
x = a! + b! * c! |
var x, _this = this; |
Object
A = a: a b: b! c: c |
var A, _$$_1, _this = this; |
Logical
Support ||
, &&
, ?
, &&=
, ||=
, ?=
x = a! || b! console.log x |
var x, _this = this; |
Auto Callback
a = (autocb) -> return 3 |
function a(autocb) { return autocb(3); }; |
Return Multiple Values
a = (autocb) -> return null, 3 |
function a(autocb) { return autocb(null, 3); }; |
Autocb with default args
a = (paramA, autocb(e, data)) -> e = foo! if something data = 1 else data = 2 |
function a(paramA, autocb) { var data, e, _this = this; foo(function() { e = arguments[0]; if (something) { data = 1; autocb(e, data); } else { data = 2; autocb(e, data); } }); }; |
Regexp
if a =~ b || b =~ c \~ \& \0 \9 |
var __matches; |
Named Function Supported
a = -> b = -> null |
function a() {}; |
Those cases will be kept in non-named function
f = null if a b = c -> d e = -> f = -> null |
var b, e, f; |