@jswork/pipe
v1.0.4
Published
Pipe is a lightweight JavaScript library for function composition and execution.
Downloads
290
Readme
pipe
Pipe is a lightweight JavaScript library for function composition and execution.
installation
npm install @jswork/pipe
methods
- [x] pipe === pipe.sync
- [x] pipe.sync (default)
- [x] pipe.async
usage
import pipe from '@jswork/pipe';
// fns
function addOne(n: number): number {
return n + 1;
}
function double(n: number): number {
return n * 2;
}
function divide(n: number): number {
if (n === 0) {
throw new Error('Divide by zero error.');
}
return 10 / n;
}
// use pipe
const calculate = pipe(
addOne,
double,
divide,
addOne
);
const result1 = calculate(3);
const result2 = calculate(-1);
// has result, without log
console.log('Result1:', result1); // 10 / (3 + 1)*2 + 1
// has result, but with warning log
console.log('Result2:', result2); // 10 / (-1+1)*2 + 1
license
Code released under the MIT license.