https://github.com/deviltea/tiny-pipe
A pipe function builder with tiny size and type-safe.
https://github.com/deviltea/tiny-pipe
functional-programming pipe type-safe typescript
Last synced: 12 months ago
JSON representation
A pipe function builder with tiny size and type-safe.
- Host: GitHub
- URL: https://github.com/deviltea/tiny-pipe
- Owner: DevilTea
- License: mit
- Created: 2023-12-04T02:22:15.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-08T02:52:59.000Z (over 2 years ago)
- Last Synced: 2024-03-29T16:03:10.258Z (almost 2 years ago)
- Topics: functional-programming, pipe, type-safe, typescript
- Language: TypeScript
- Homepage:
- Size: 22.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# @deviltea/tiny-pipe
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![bundle][bundle-src]][bundle-href]
[![License][license-src]][license-href]
A pipe function builder with tiny size and type-safe.
## Overview
**Tiny Pipe** is a lightweight, type-safe pipe function builder. It allows you to create a sequence of operations, either synchronously or asynchronously, with safe error handling capabilities.
- **Lightweight**: Built with performance and minimal footprint in mind.
- **Type-safe**: Ensures correct types throughout the pipe's execution.
- **Error handling**: Built-in mechanisms for safely managing errors during pipeline execution.
## Features
- **Sync and Async Pipelines**: Support both synchronous and asynchronous function chains.
- **Safe Execution**: Use `pipeSafely` to automatically handle errors within the pipeline.
- **Tiny Size**: Optimized for minimal overhead while providing robust functionality.
## Installation
Install the package using `pnpm`:
```bash
pnpm add @deviltea/tiny-pipe
```
Or with `npm`:
```bash
npm install @deviltea/tiny-pipe
```
## Usage
### Synchronous Pipeline
```ts
import { createPipe } from '@deviltea/tiny-pipe'
const result = createPipe()
.pipe((value: number) => value + 1)
.pipe((value: number) => value * 2)
.execute(5)
console.log(result) // Output: 12
```
### Asynchronous Pipeline
```ts
import { createPipe } from '@deviltea/tiny-pipe'
const result = await createPipe()
.pipe(async (value: number) => value + 1)
.pipe(async (value: number) => value * 2)
.execute(5)
console.log(result) // Output: 12
```
### Error Handling
```ts
import { createPipe } from '@deviltea/tiny-pipe'
createPipe()
.pipeSafely(() => {
throw new Error('Oops!')
})
.pipe((result) => {
if (result.status === 'error') {
console.error(result.reason)
}
else {
console.log(result.value)
}
})
.execute()
```
## Development
### Scripts
- `pnpm build`: Build the library.
- `pnpm dev`: Start development mode.
- `pnpm lint`: Lint the code.
- `pnpm test`: Run the test suite with Vitest.
## Contributing
Feel free to open an issue or submit a pull request if you find any bugs or have suggestions for improvements.
## License
[MIT](./LICENSE) License © 2023-PRESENT [DevilTea](https://github.com/DevilTea)
[npm-version-src]: https://img.shields.io/npm/v/@deviltea/tiny-pipe?style=flat&colorA=080f12&colorB=1fa669
[npm-version-href]: https://npmjs.com/package/@deviltea/tiny-pipe
[npm-downloads-src]: https://img.shields.io/npm/dm/@deviltea/tiny-pipe?style=flat&colorA=080f12&colorB=1fa669
[npm-downloads-href]: https://npmjs.com/package/@deviltea/tiny-pipe
[bundle-src]: https://img.shields.io/bundlephobia/minzip/@deviltea/tiny-pipe?style=flat&colorA=080f12&colorB=1fa669&label=minzip
[bundle-href]: https://bundlephobia.com/result?p=@deviltea/tiny-pipe
[license-src]: https://img.shields.io/github/license/DevilTea/tiny-pipe.svg?style=flat&colorA=080f12&colorB=1fa669
[license-href]: https://github.com/DevilTea/tiny-pipe/blob/main/LICENSE