Skip to content

Commit

Permalink
docs: update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Aug 12, 2024
1 parent f857b0f commit 6b8570a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@
[![test](https://github.com/jsr-core/pipe/workflows/Test/badge.svg)](https://github.com/jsr-core/pipe/actions?query=workflow%3ATest)
[![codecov](https://codecov.io/github/jsr-core/pipe/graph/badge.svg?token=pfbLRGU5AM)](https://codecov.io/github/jsr-core/pipe)

## Usage
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
[Pipe Operator (`|>`) for JavaScript]: https://github.com/tc39/proposal-pipeline-operator

## Usage

Pipe a value through a series of operatorfunctions.
Pipe a value through a series of operator functions.

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

const result = pipe(
1,
(v) => v + 1,
(v) => v * 2,
(v) => v.toString(),
(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"
```
Expand Down
20 changes: 13 additions & 7 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@ export type LastOperatorReturn<T extends Operator<unknown, unknown>[]> =
: never;

/**
* Pipe a value through a series of operator functions.
* Pipes a value through a series of operator functions.
* Supports type inference for both the operator functions and the return value of the final operator.
*
* @param value - The value to pipe through the operators.
* @param operators - The operators to apply to the value.
* @returns The value after it has been piped through all the operators.
* > [!NOTE]
* >
* > If the number of operators exceeds 20, the operator functions' types will default to
* > `Operator<unknown, unknown>`, requiring explicit type annotations.
*
* @param value - The initial value to be processed through the operators.
* @param operators - A sequence of functions to apply to the value.
* @returns The final value after being processed through all the operators.
*
* @example
* ```ts
* import { pipe } from "@core/pipe";
*
* const result = pipe(
* 1,
* (v) => v + 1,
* (v) => v * 2,
* (v) => v.toString(),
* (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"
* ```
Expand Down

0 comments on commit 6b8570a

Please sign in to comment.