Skip to content

Commit

Permalink
Merge pull request #1 from jsr-core/rename
Browse files Browse the repository at this point in the history
refactor!: Rename the library to `@core/pipe`
  • Loading branch information
lambdalisue authored Aug 12, 2024
2 parents 538064e + 6b8570a commit 579f7fb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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"
```
Expand Down
4 changes: 2 additions & 2 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@core/pipeline",
"name": "@core/pipe",
"version": "0.0.0",
"exports": {
".": "./mod.ts"
Expand All @@ -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",
Expand Down
22 changes: 14 additions & 8 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 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<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/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"
* ```
Expand Down

0 comments on commit 579f7fb

Please sign in to comment.