Skip to content
generated from DevilTea/starter-ts

A pipe function builder with tiny size and type-safe.

License

Notifications You must be signed in to change notification settings

DevilTea/tiny-pipe

Repository files navigation

@deviltea/tiny-pipe

npm version npm downloads bundle License

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:

pnpm add @deviltea/tiny-pipe

Or with npm:

npm install @deviltea/tiny-pipe

Usage

Synchronous Pipeline

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

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

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 © 2023-PRESENT DevilTea