Giter Site home page Giter Site logo

casualuser / toffee-script Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jiangmiao/toffeescript

0.0 2.0 0.0 15.81 MB

CoffeeScript with async syntax and some additional features

Home Page: http://coffeescript.org/

License: MIT License

CoffeeScript 65.85% Ruby 0.41% JavaScript 0.10% HTML 33.65%

toffee-script's Introduction

ToffeeScript

ToffeeScript is a CoffeeScript dialect with Asynchronous Grammar

Features

  1. Asynchronous everywhere
    • Condition: If, Switch
    • Loop: For In, For Of, While with guard when
    • Mathematics
    • Logical Operation
  2. Auto Callback
  3. Regexp Operator =~ and matches \~, \&, \0~\9
  4. High efficent code generated.
  5. Sourcemap Supported.
    • Follow up to CoffeeScript 1.6.2 so far
  6. 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;

a(b, function() { x = arguments[0], y = arguments[1]; return console.log(x, y); });

with powerful CoffeeScript assignment [...]

[@x, y...] = a! b
console.log @x, y
var y,
  _this = this,
  __slice = [].slice;

a(b, function() { _this.x = arguments[0], y = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return console.log(_this.x, y); });

Condition

if i
  x = a!
else
  y = b!
console.log x, y
var x, y,
  _this = this;

if (i) { a(function() { x = arguments[0]; _$$_0(); }); } else { b(function() { y = arguments[0]; _$$_0(); }); }

function _$$_0() { return console.log(x, y); };

Async in condition

if e = a!
  return cb(e)
foo()
var e,
  _this = this;

a(function() { _$cb$_2(e = arguments[0]); }); function _$cb$2($$0) { if ($$_0) { return cb(e); } else { _$$_1(); } function _$$_1() { return foo(); }; };

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;

fs.readFile('foo', function() { _$cb$_2((e = arguments[0], data = arguments[1], e)); }); function _$cb$2($$0) { if ($$_0) { return cb(e); } else { _$$_1(); } function _$$_1() { return console.log(data); }; };

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;

_$res$_1 = []; i = _i = 1; function _step() { i = ++_i; _body(); }; function _body() { if (i <= 3) { if (i > 2) { a(function($$_2) { step($res$1.push($$_2)); }); } else { _step(); } } else { _done(); } }; function _done() { _$cb$0($res$_1); }; _body(); function _$cb$_0() { return xs = arguments[0]; };

Mathematics

x = a! + b! * c!
var x,
  _this = this;

a(function(_$$1) { b(function($$3) { c(function($$_4) { _$cb$2($$_3 * _$$_4); }); }); function _$cb$2($$_5) { _$cb$0($$_1 + _$$_5); }; }); function _$cb$_0() { return x = arguments[0]; };

Object

A =
  a: a
  b: b!
  c: c
var A, _$$_1,
  _this = this;

_$$1 = a; b(function($$_2) { _$cb$_0({ a: _$$_1, b: _$$_2, c: c }); }); function _$cb$_0() { return A = arguments[0]; };

Logical

Support ||, &&, ?, &&=, ||=, ?=

x = a! || b!
console.log x
var x,
  _this = this;

a(function(_$$1) { if ($$_1) { _$cb$3($$1); } else { b(function($$_2) { _$cb$3($$_2); }); } }); function _$cb$3($$_4) { _$cb$0($$_4); }; function _$cb$_0() { x = arguments[0]; return console.log(x); };

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;

if ((__matches = a.match(b)) || (__matches = b.match(c))) { __matches; __matches[0]; __matches[0]; __matches[9]; }

Named Function Supported

a = ->
b = ->
null
function a() {};

function b() {};

null;

Those cases will be kept in non-named function

f = null
if a
  b = c ->
d e = ->
f = ->
null
var b, e, f;

f = null;

if (a) { b = c(function() {}); }

d(e = function() {});

f = function() {};

null;

toffee-script's People

Contributors

jashkenas avatar michaelficarra avatar jiangmiao avatar satyr avatar trevorburnham avatar geraldalewis avatar stanangeloff avatar epidemian avatar cehoffman avatar matehat avatar thejh avatar zmthy avatar hden avatar vendethiel avatar breckinloggins avatar danielgtaylor avatar sgentle avatar jeremybanks avatar sstephenson avatar asalant avatar codelahoma avatar troels avatar tim-smart avatar spinda avatar maxtaco avatar sparecycles avatar jawj avatar revence27 avatar willmoffat avatar olsonjeffery avatar

Watchers

James Cloos avatar Aleksei Tcelishchev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.