https://github.com/jsr-core/pipe
Performs consecutive operations on a value in TypeScript
https://github.com/jsr-core/pipe
javascript jsr pipe pipeline typescript
Last synced: 17 days ago
JSON representation
Performs consecutive operations on a value in TypeScript
- Host: GitHub
- URL: https://github.com/jsr-core/pipe
- Owner: jsr-core
- License: mit
- Created: 2024-08-10T20:06:19.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-10-27T14:20:39.000Z (6 months ago)
- Last Synced: 2025-03-23T18:04:42.155Z (about 1 month ago)
- Topics: javascript, jsr, pipe, pipeline, typescript
- Language: TypeScript
- Homepage: https://jsr.io/@core/pipe
- Size: 35.2 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# pipe
[](https://jsr.io/@core/pipe)
[](https://github.com/jsr-core/pipe/actions?query=workflow%3ATest)
[](https://codecov.io/github/jsr-core/pipe)Performs consecutive operations on a value in TypeScript. An alternative library
of the proposal of [Pipe Operator (`|>`) for JavaScript]. It supports type
inference and type checking of the operator functions.> [!NOTE]
>
> When the number of operator functions applied to `pipe` get more than twenty,
> the result of type inference of each operator function become `unknown` and
> users need to annotate the type explicitly.[Pipe Operator (`|>`) for JavaScript]: https://github.com/tc39/proposal-pipeline-operator
## Usage
Pipe a value through a series of operator functions.
```ts
import { pipe } from "@core/pipe/pipe";const result = pipe(
1,
(v) => v + 1, // inferred as (v: number) => number
(v) => v * 2, // inferred as (v: number) => number
(v) => v.toString(), // inferred as (v: number) => string
);
console.log(result); // "4"
```Or use `async` module to pipe a value through a series of asynchronous operator
functions.```ts
import { pipe } from "@core/pipe/async/pipe";const result = await pipe(
1,
(v) => Promise.resolve(v + 1), // inferred as (v: number) => number | Promise
(v) => Promise.resolve(v * 2), // inferred as (v: number) => number | Promise
(v) => Promise.resolve(v.toString()), // inferred as (v: number) => string | Promise
);
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
(v) => Promise.resolve(v.toString()), // inferred as (v: number) => string | Promise
);
console.log(await operator(1)); // "4"
```## Difference
The `pipe` function in the root module is equivalent to function calls without
`await` like below.```ts
import { pipe } from "@core/pipe/pipe";const a = (v: unknown) => v;
const b = (v: unknown) => v;
const c = (v: unknown) => v;// Equivalent
console.log(pipe(1, a, b, c)); // 1
console.log(c(b(a(1)))); // 1
```The `pipe` function in the `async` module is equivalent to function calls with
`await` like below.```ts
import { pipe } from "@core/pipe/async/pipe";const a = (v: unknown) => Promise.resolve(v);
const b = (v: unknown) => Promise.resolve(v);
const c = (v: unknown) => Promise.resolve(v);// Equivalent
console.log(await pipe(1, a, b, c)); // 1
console.log(await c(await b(await a(1)))); // 1
```## License
The code follows MIT license written in [LICENSE](./LICENSE). Contributors need
to agree that any modifications sent in this repository follow the license.