tubo
Tubo works like the pipe operator more common in functional programming languages, like Elixir.
This lib supports sync and async arguments.
If all arguments are sync functions or literal, the pipeline will run as a normal function returning the last value computed. In case one of the arguments is a function that returns a promise or a promise instance (then-able object), a promise will be returned at the end.
Installation
npm install tubo --save
Usage
Sync
{ return x * 2} { return x * x} var output = console // => 16
Async
try const result = await console catch error console
Async with Promise
Mixed
It is also possible to mix sync and async arguments.
Whenever a function that generates promises or a promise intance is found,
the lib switchs to async mode and will return a Promise
.
Examples
Validation (sync and async)
// Before { email = } // After { return } try catch error console
Straight-forward Cases
// Beforeif cache && localStorage return m // Afterif cache && localStorage return
Usage with const
// Beforelet items = items = Array ? items : items // Afterconst items =
Functional Updates
// Beforereturn Event // Afterreturn
Reference and credits
- Ramda.js pipe
- lodash.js flow
- pipe.py
- Some exampes taken from https://github.com/mindeavor/es-pipeline-operator/wiki/Example-Use-Cases
caiogondim.com · GitHub @caiogondim · Twitter @caio_gondim