Compiles JavaScript written using arrow functions to use ES5-compatible function syntax. For example, this:
1 2 3;
compiles to this:
1 2 3;
For more information about the proposed syntax, see the TC39 wiki page on arrow functions.
Install
$ npm install es6-arrow-function
Usage
$ node> var compile = compile;Function
Without arguments:
> code;'$(function() { return main(); });'
With a single argument:
> code;'[1, 2, 3].map(function(n) { return n * 2; });'
With multiple arguments:
> code;'[1, 2, 3].map(function(n, i) { return n * i; });'
It binds the current context:
> code;'stream.on("data", (function(d) { return this.data += d; }).bind(this));'
Or work directly with the AST:
$ cat astjson "type": "Program" "body": "type": "ExpressionStatement" "expression": "type": "CallExpression" "callee": "type": "Identifier" "name": "$" "arguments": "type": "ArrowFunctionExpression" "id": null "params": "defaults": "body": "type": "CallExpression" "callee": "type": "Identifier" "name": "main" "arguments": "rest": null "generator": false "expression": true $ node> var transform = transform;Function> console; "type": "Program" "body": "type": "ExpressionStatement" "expression": "type": "CallExpression" "callee": "type": "Identifier" "name": "$" "arguments": "type": "FunctionExpression" "id": null "params": "defaults": "body": "type": "BlockStatement" "body": "type": "ReturnStatement" "argument": "type": "CallExpression" "callee": "type": "Identifier" "name": "main" "arguments": "rest": null "generator": false "expression": false
Command line
If installing via npm
a command line tool will be available called es6-arrow-function
.
$ echo "()=>123" | es6-arrow-function
(function () {
return 123;
});
$ es6-arrow-function $file
(function () {
return 123;
});
Browserify
Browserify support is built in.
$ npm install es6-arrow-function # install local dependency
$ browserify -t es6-arrow-function $file
// BOILERPLATE
(function () {
return 123;
});
Contributing
Setup
First, install the development dependencies:
$ npm install
Then, try running the tests:
$ npm test
Pull Requests
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Any contributors to the master es6-arrow-function repository must sign the Individual Contributor License Agreement (CLA). It's a short form that covers our bases and makes sure you're eligible to contribute.
When you have a change you'd like to see in the master repository, send a pull request. Before we merge your request, we'll make sure you're in the list of people who have signed a CLA.