pipe
Composes functions left-to-right. The first function receives the initial arguments; each subsequent function receives the previous function’s return value. Aligned with the TC39 pipe operator proposal.
Import
Section titled “Import”import { pipe } from "1o1-utils";import { pipe } from "1o1-utils/pipe";Signature
Section titled “Signature”function pipe<A extends unknown[], B, C, ..., R>( f1: (...args: A) => B, f2: (b: B) => C, // ...): (...args: A) => ROverloads are typed up to 10 functions. Beyond that, the return type falls back to unknown.
Parameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
fns | Array<(...args) => any> | No | Functions to compose, executed in the order provided (variadic) |
Returns
Section titled “Returns”A function that applies every fn in sequence. Calling pipe() with no arguments returns an identity function that passes its first argument through unchanged.
Examples
Section titled “Examples”const slug = pipe( (x: string) => x.trim(), (x: string) => x.toLowerCase(), (x: string) => x.replace(/\s+/g, "-"),);
slug(" Hello World "); // "hello-world"// First function can take multiple argsconst sum = pipe( (a: number, b: number) => a + b, (n: number) => n * 2,);
sum(2, 3); // 10// Type inference flows across the chainconst length = pipe( (x: string) => x.trim(), (x: string) => x.split(""), (chars: string[]) => chars.length,);
const n: number = length(" abc "); // 3Edge Cases
Section titled “Edge Cases”- Throws if any argument is not a function.
pipe()with zero arguments returns an identity function.pipe(fn)with a single function returns that function unchanged — all arguments are forwarded.- Execution is synchronous. If a function returns a
Promise, the next function receives thePromise(not the awaited value). - Exceptions thrown by composed functions propagate out of the composed function.
- Only the first function receives the caller’s
thiscontext; subsequent stages are called withoutthis. Differs fromlodash/flowandes-toolkit/flow, which forwardthisto every stage.
Also known as
Section titled “Also known as”compose, function composition, flow, left-to-right, pipeline, chain, sequence, combine functions
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use pipe to compose a string-transformation pipeline