Created
October 31, 2025 09:40
-
-
Save mlaopane/3078c13e166c5ace5cc28d0b433b3c74 to your computer and use it in GitHub Desktop.
Pipe functions with type inference on each pipe call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Transformer<I, O> = (input: I) => O | |
| /** | |
| * @example | |
| * ```ts | |
| * Pipeline.init({id: 1, name: "John"}) | |
| * .pipe((user) => `${user.name}-${user.id}`) | |
| * .pipe((slug) => slug.toLocaleLowerCase()) | |
| * .value() | |
| * // Result === "john-1" | |
| * ``` | |
| */ | |
| export class Pipeline<I> { | |
| private readonly currentValue: I | |
| private constructor(initialValue: I) { | |
| this.currentValue = initialValue | |
| } | |
| static init<T>(initialValue: T): Pipeline<T> { | |
| return new Pipeline(initialValue) | |
| } | |
| pipe<O>(transformer: Transformer<I, O>): Pipeline<O> { | |
| const next = transformer(this.currentValue) | |
| return new Pipeline<O>(next) | |
| } | |
| value(): I { | |
| return this.currentValue | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment