pipe-functions
Pipe functions in a Unix-like style. It supports Promises
(async) anywhere in the pipeline and every step will be executed sequentially. The return (resolve in case of Promises
) of each function will be passed in as an argument to the next one
Key features:
- Supports
Promises
, or any lib following the Promises/A+ spec about being thenable (.then() method) Promises
will be executed sequentially- First argument can be of any type (
String
,Number
,Date
, etc.) or even aFunction
or aPromise
- Node.js and Browser ready (to be used on a Browser, without a build step, check
lib/pipe-non-es6.js
) - Lightweight, 501 bytes, before gzip!
Install
NPM / Node
npm install pipe-functions
Usage
Sync
const pipe = ; // First argument can be of any typeconst result = ;// And also a functionconst result2 = ;
Promises
)
Async (If the pipeline contains a Promise
anywhere in the pipeline, we must treat pipe
like a Promise
itself, so we must to use .then() to get the final result.
const pipe = ; // First argument can be of any type, as shown in the previous example;
A suggestion regarding Promises
. Probably you've seen, or had to write, a stack of promises like that:
It could be written as:
const pipe = ;
Examples
OBS: Some examples needs a platform with support for Destructuring (Nodejs v6+, Chrome).
Sync
const pipe = ; /** Functions **/const capitalize = v0 + v;const quote = `""`; /** Pipe **/// result will be: "Time"const result = ;
Promises
)
Async (const pipe = ; /** Functions **/// Syncconst capitalize = v0 + v;// Asyncconst fetchAndSetBandName = ; /** Pipe **/// the result will be: Pink Floyd - Time
Example with destructuring,
To easily pass in more than one value (within an Object or Array) through the pipeline.
const pipe = ; /** Functions **/// Asyncconst fetchBandName = ;// Syncconst concatBandAndSong = ` - `; /** Pipe **/// the result will be: Pink Floyd - Time