A type safety function to create a pipeline
npm i @lidqqq/pipe
// Async
import { pipe } from '@lidqqq/pipe';
import type { AsyncReturnType } from '@lidqqq/pipe';
async function plus2(n: number): Promise<number> {
return n + 2;
}
async function n2s(n: AsyncReturnType<typeof plus2>): Promise<string> {
return n.toString();
}
const main = pipe<typeof plus2, typeof n2s>(plus2, n2s);
(async () => {
console.log((await main(2)) === '4'); // true
})();
// Synchronously
import { pipeSync } from '@lidqqq/pipe';
function plus2(a: number) {
return a + 2;
}
function n2s(a: number): string {
return a.toString();
}
const main = pipeSync<typeof plus2, typeof n2s>(plus2, n2s);
console.log(main(42) === '44'); // true
// ESM
import { pipe } from '@lidqqq/pipe';
// CJS
const { pipe } = require('@lidqqq/pipe');