Overload the bitwise operators (<<
) and (>>
) to provide F# like pipe forward/backward behavior.
This is an alternative to babel-plugin-pipe-operator and babel-plugin-pipe-operator-curry and favours the currying approach instead of using the first callable argument.
import { mean, round } from 'lodash';
const array = [1, 2, 3, 4, 5];
// regular javascript
round(mean(array));
// typical composition helpers
pipe(mean, round)(array);
compose(round, mean)(array);
// composition with pipe operators
array
>> mean
>> round
round
<< mean
<< array
import { map, filter } from 'ramda';
const array = [1, 2, 3, 4, 5];
// regular javascript
filter(n => n % 3 == 0)(map(n => n * 2)(array))
// typical composition with javascript
pipe(map(n => n * 2), filter(n => n % 3 == 0))(array);
compose(filter(n => n % 3 == 0), map(n => n * 2))(array);
// composition with pipe operators
let result = array
>> map(n => n * 2)
>> filter(n => n % 3 == 0);
let result = filter(n => n % 3 == 0)
<< map(n => n * 2)
<< array;
If you want to use the original bitwise operators, you can disable this plugin in current scope (and child scopes) using the "no pipe"
directive.
const fn = () => {
const arr = [1, 2, 3] >> map(n => n + 1);
return () => {
"no pipe";
arr.map(n => n >> 1);
};
};
const fn = () => {
const arr = map(n => n + 1) << [1, 2, 3];
return () => {
"no pipe";
arr.map(n => 1 << n);
};
};
$ npm install --save-dev babel-plugin-pipe-composition
{
"plugins": ["pipe-composition"]
}
$ babel --plugins pipe-composition script.js
require("babel-core").transform("code", {
plugins: ["pipe-composition"]
});
MIT