Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yamiteru/pipu
🔥 hellishly fast, 🤏 tiny and 🤖 modular async/sync pipe toolkit for typescript.
https://github.com/yamiteru/pipu
fp functional-programming javascript pipe pipe-operator typescript
Last synced: 5 days ago
JSON representation
🔥 hellishly fast, 🤏 tiny and 🤖 modular async/sync pipe toolkit for typescript.
- Host: GitHub
- URL: https://github.com/yamiteru/pipu
- Owner: yamiteru
- License: mit
- Created: 2022-11-25T22:33:11.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-07-22T00:24:54.000Z (over 1 year ago)
- Last Synced: 2024-04-24T04:11:47.737Z (10 months ago)
- Topics: fp, functional-programming, javascript, pipe, pipe-operator, typescript
- Language: TypeScript
- Homepage:
- Size: 1.09 MB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Pipu
🔥 hellishly fast, 🤏 tiny and 🤖 modular async/sync pipe toolkit for typescript.
---
## Installation
```bash
yarn add pipu # pnpm add pipu
```---
## Concept
Pipu is designed to be as low-level and unopinionated as possible so you can build your own abstractions on top of it.
It supports both sync and async modes. That means you can use sync to solve problems where sync fits the job best and async where async fits the job best.
---
## Examples
1. [Map](./examples/1-map.ts)
2. [Filter](./examples/2-filter.ts)
3. [Or](./examples/3-or.ts)
4. [Wrap](./examples/4-wrap.ts)
5. [Parse](./examples/5-parse.ts)
6. [Resolve](./examples/6-resolve.ts)---
## Pipeables
Pipeables are functions that you can put into a pipe (pipe itself is `Pipeable`).
There's just a few core pipeables so it's easy to build your own abstractions on top them.
*(but I'm open to ideas and PRs regarding new pipeables)*
### filter
Pipeable which runs either `truePipeable` or `falsePipeable` based on return value of `predicate`.
By default `truePipeable` returns `Ok<$Input>` and `falsePipeable` returns `Err>`.
```ts
filter((v: number) => !(v % 2));filter(
(v: number) => !(v % 2),
() => ok("even"),
);filter(
(v: number) => !(v % 2),
() => ok("even"),
() => ok("odd"),
);
```### map
It maps value from `$Input` to `$Output` and returns it as `Ok<$Output>`.
```ts
// Pipeable>
const doubleString = map((v: number) => `${v * 2}`);
```### wrap
Pipeable which wraps a sub-pipeable and overrides its `Error`.
It's recommended to use `error` function to create a custom `Error`.
```ts
// Pipeable>>
wrap(
map((v: number) => v * 2),
error("CUSTOM"),
);
```### and
Pipeable which takes N number of pipeables and returns either `Ok` returned by the last pipeable or `Err` returned by any of the pipeables.
```ts
// Pipeable | Error<"CUSTOM", number>>>
const customPipe = pipe(
filter((v: number) => !(v % 2)),
wrap(
map((v) => `${v}`),
error("CUSTOM"),
),
);
```### or
Pipeable which takes N number of pipeables and returns either `Ok` returned by any of the pipeables or `Err` if none of the pipeables returned `Ok`.
```ts
// Pipeable>>
const customPipe = or(
filter((v: number) => !(v % 2)),
filter((v: number) => !(v % 3)),
);
```### resolve
Async pipeable which tries to resolve a promise and return `ResultOk`.
```ts
// PipeableAsync>>
const getUsersByAge = and(
filter((age: number) => age > 18),
resolve((age) => api.getUsersByAge(age)),
);
```---
## Utils
### error
Creates error tuple based on value and potential sub-error.
It should mainly be used in `wrap` function as a second parameter.
All `Pipeable`s should return error created with this function.
```ts
error("TEST", (value, error) => ({ valueType: typeof value, error }));
```### parse
Allows to run `Pipeable` with `unknown` input while infering everything else from the `Pipeable` as usual.
```ts
const isStringOrNumber = pipe(...);
// ResultErr<["OR", [], { }]>
const result1 = parse(isStringOrNumber, []);
// ResultOk<1>
const result2 = parse(isStringOrNumber, 1);
```---
Have a beautiful day 🍀.