Created
May 26, 2025 14:20
-
-
Save RedStone576/b5e4b0aa3a3c153ae40f2b0881c9bd4c to your computer and use it in GitHub Desktop.
Pipeline operator in typescript
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
| class Pipe<T> | |
| { | |
| #value: T | |
| constructor(value: T) | |
| { | |
| this.#value = value | |
| } | |
| get value(): T | |
| { | |
| return this.#value | |
| } | |
| $<U>(func: (arg: T) => U): Pipe<U> | |
| { | |
| const result = func(this.#value) | |
| return new Pipe(result) | |
| } | |
| } |
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
| const { value: h } = new Pipe("balls") | |
| .$(x => x.toUpperCase()) // string -> string | |
| .$(x => x.length) // string -> number | |
| .$(x => (console.log(x), x > 3)) // number -> boolean | |
| // console log and return a value, arrow function implicitly returns the last expression | |
| console.log(h) // boolean | |
| /**/ | |
| new Pipe("test") | |
| .value = "tes1" | |
| // this was prevented dw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment