Compile pipeline operator (
|>
) to ES5
Proposal: mindeavor/es-pipeline-operator
In
var user = { name: 'SuperPaintman' };
function capitalize(str) {
return str.toUpperCase();
}
function sayHello(name) {
return 'Hello, ' + name + '!';
}
var res = user.name
|> capitalize
|> sayHello;
// => "Hello, SUPERPAINTMAN!"
Out
var user = { name: 'SuperPaintman' };
function capitalize(str) {
return str.toUpperCase();
}
function sayHello(name) {
return 'Hello, ' + name + '!';
}
var res = sayHello(capitalize(user.name));
// => "Hello, SUPERPAINTMAN!"
In
var user = { score: 40.49138 };
var res = user.score
|> (_ => _ * 2)
|> (_ => _.toFixed(2));
// => 80.98
Out
var user = { score: 40.49138 };
var res = (_ => _ * 2)((_ => _.toFixed(2))(user.score));
// => 80.98
var path = require('path');
var pathToUrl = (rootDir, filePath) => [rootDir, filePath]
|> ((args) => path.relative(...args))
|> path.dirname
|> ((res) => res.split(path.sep).join(path.posix.sep))
|> ((res) => '/' + (res === '.' ? '' : (res + '/')));
pathToUrl('./controllers', './controllers/api/users/index.js');
// => "/api/users/"
pathToUrl('./controllers', './controllers/index.js');
// => "/"
Out
var path = require('path');
var pathToUrl = (rootDir, filePath) => (res => '/' + (res === '.' ? '' : res + '/'))((res => res.split(path.sep).join(path.posix.sep))(path.dirname((args => path.relative(...args))([rootDir, filePath]))));
pathToUrl('./controllers', './controllers/api/users/index.js');
// => "/api/users/"
pathToUrl('./controllers', './controllers/index.js');
// => "/"
Why do we need parentheses around multi-argument functions?
Because they separate the =>
from the |>
.
Ie. the following code:
var res = user.score
|> _ => _ * 2
|> double;
Is equivalent to:
var res = user.score
|> _ => _ * double(2);
// or
var secondArg = 2
|> double;
var res = user.score
|> _ => _ * secondArg;
Why
|>
?
Firstly, it is a invalid token in terms of javascript (ES3-ES2017).
Secondly, in vanilla javascript there are only 3 token starting with |
: |
, ||
and |=
;
npm install --save-dev babel-plugin-transform-pipeline
# or
yarn add --dev babel-plugin-transform-pipeline
.babelrc
{
"plugins": ["transform-pipeline"]
}
babel --plugins transform-pipeline script.js
require("babel-core").transform("code", {
plugins: ["transform-pipeline"]
});
npm run build
npm run test
- Fork it (https://github.com/SuperPaintman/babel-plugin-transform-pipeline/fork)
- Create your feature branch (
git checkout -b feature/<feature_name>
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin feature/<feature_name>
) - Create a new Pull Request
- SuperPaintman SuperPaintman - creator, maintainer