@ts-delight/pipe
Type-safe left-to-right functional pipeline composition
Features
- No ad-hoc limit on steps being composed
- Supports async
- Supports both eager and lazy invocations
- No dependencies
Installation
npm install --save @ts-delight/pipe
Usage
Eager evaluation
import {pipe} from "@ts-delight/pipe";
const result = pipe(10).thru((i: number) => i + 1).thru((i: number) => `Result: ${i}`).value;
// | ^
// |___________|
// |
// Type of input must match input of first step
//
// result: string = "Result: 11"
// ^ ^
// | |__ Result of left-to-right composition
// |
// |__ Output type inferred from output type of last step
//
Eager evaluation with async steps
import {pipe} from "@ts-delight/pipe";
const result = pipe(10)
.thruAsync(async (i: number) => i + 1)
.thru((i: number) => `Result: ${i}`)
.value;
// result: Promise<string>
Lazy evaluation with sync steps
import {pipeFn} from "@ts-delight/pipe";
const {fn} = pipeFn()
.thru((i: number) => i + 1)
.thru((i: nmber) => `Result: ${i}`);
// fn: (i: number) => string;
fn(10); // => "Result: 11"
Lazy evaluation with async steps
import {pipeFnAsync} from "@ts-delight/pipe";
const {fn} = pipeFnAsync()
.thru(async (i: number) => i + 1)
.thru(async (i: nmber) => `Result: ${i}`);
// fn: (i: number) => Promise<string>;
fn(10); // => Promise which resolves to "Result: 11"