Skip to content

Commit

Permalink
feat: add compose function for composing multiple operators
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Oct 27, 2024
1 parent bcf44d9 commit 2936dc6
Show file tree
Hide file tree
Showing 8 changed files with 1,268 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ const result = pipe(
console.log(result); // "4"
```

If you want to create a new function that composes multiple operators, use
`compose` like below.

```ts
import { compose } from "@core/pipe/compose";

const operator = compose(
(v: number) => v + 1, // The first operator must be typed explicitly
(v) => v * 2, // inferred as (v: number) => number
(v) => v.toString(), // inferred as (v: number) => string
);
console.log(operator(1)); // "4"
```

Or use `async` module to compose multiple asynchronous operators.

```ts
import { compose } from "@core/pipe/async/compose";

const operator = compose(
(v: number) => Promise.resolve(v + 1), // The first operator must be typed explicitly
(v) => Promise.resolve(v * 2), // inferred as (v: number) => number | Promise<number>
(v) => Promise.resolve(v.toString()), // inferred as (v: number) => string | Promise<string>
);
console.log(await operator(1)); // "4"
```

## Difference

The `pipe` function in the root module is equivalent to function calls without
Expand Down
Loading

0 comments on commit 2936dc6

Please sign in to comment.