diff --git a/README.md b/README.md index 00a4720..3918afb 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,33 @@ -# pipeline +# pipe -[![jsr](https://jsr.io/badges/@core/pipeline)](https://jsr.io/@core/pipeline) -[![test](https://github.com/jsr-core/pipeline/workflows/Test/badge.svg)](https://github.com/jsr-core/pipeline/actions?query=workflow%3ATest) -[![codecov](https://codecov.io/github/jsr-core/pipeline/graph/badge.svg?token=pfbLRGU5AM)](https://codecov.io/github/jsr-core/pipeline) +[![jsr](https://jsr.io/badges/@core/pipe)](https://jsr.io/@core/pipe) +[![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/pipeline"; +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" ``` diff --git a/deno.jsonc b/deno.jsonc index 483b280..fe30d7c 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,5 +1,5 @@ { - "name": "@core/pipeline", + "name": "@core/pipe", "version": "0.0.0", "exports": { ".": "./mod.ts" @@ -20,7 +20,7 @@ ] }, "imports": { - "@core/pipeline": "./mod.ts", + "@core/pipe": "./mod.ts", "@std/assert": "jsr:@std/assert@^1.0.2", "@std/jsonc": "jsr:@std/jsonc@^1.0.0", "@std/path": "jsr:@std/path@^1.0.2", diff --git a/mod.ts b/mod.ts index d4cab9d..2d116f5 100644 --- a/mod.ts +++ b/mod.ts @@ -11,21 +11,27 @@ export type LastOperatorReturn[]> = : never; /** - * Pipe a value through a series of operatorfunctions. + * 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`, 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/pipeline"; + * 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" * ```