• Stars
    star
    177
  • Rank 215,985 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created 10 months ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A lazy evaluated, chainable, and reusable pipe for data transformation and processing.

sloth-pipe

Sloth Pipe

github latest release npm version npm weekly downloads dependencies license open issues minzipped size follow on xitter

Sloth Pipe is a tiny library for TypeScript and JavaScript that lets you create lazy, chainable, and reusable pipes for data transformation and processing. Borrowing from functional programming paradigms, it offers a convenient and powerful way to compose functions and manage data flow in an application, with an emphasis on lazy evaluation and efficient execution.

Why Sloth Pipe?

Developers want pipes. They've been one of the most requested features in JavaScript for years, and there's even a Stage 2 proposal for adding them to the language. Sloth Pipe isn't a direct replacement for the proposed pipeline operator, but it does offer a similar experience and many of the same benefits.

Features

  • Lazy Evaluation: Computations are only performed when necessary, optimizing performance and resource utilization.
  • Chainable API: Enables the creation of fluent and readable code by chaining multiple operations.
  • Error Handling: Built-in support for error handling within the pipe.
  • Async/Await Compatibility: Seamlessly integrate asynchronous functions into your pipes.
  • Tap Operations: Allows side-effects without altering the pipe's main data flow.
  • Reusable pipes: Easily reuse pipes, even after execution.
  • Extensible: Easily extendable with custom functions and operations.
  • Type-Safe: Written in TypeScript, with full support for type inference and type safety.
  • Lightweight: Small and lightweight, with no external dependencies.
  • Well-Tested: Thoroughly tested with 100% code coverage.

Installation

To install Sloth Pipe, use the following command:

bun i sloth-pipe

or

npm install sloth-pipe

Usage

Here's a simple example of how to use Sloth Pipe:

import { Pipe } from "sloth-pipe";

const result = Pipe(5)
    .to((x) => x * 2)
    .to((x) => x + 3)
    .exec();

console.log(result); // Outputs: 13

Async Operations

Sloth Pipe seamlessly integrates with asynchronous operations:

const add = async (x: Promise<number>, y: number) => {
    const xVal = await x;
    return xVal + y;
};
const asyncResult = await Pipe(5)
    .to(async (x) => x * 2)
    .to(add, 3) // pass additional arguments to any function
    .exec();

console.log(asyncResult); // Outputs: 13

Error Handling

Handle errors gracefully within the pipe:

const safeResult = Pipe(5)
    .to((x) => {
        if (x > 0) throw new Error("Example error");
        return x;
    })
    .catch((err) => 0)
    .exec();

console.log(safeResult); // Outputs: 0

API Reference

The API consists of a few key methods: to, tap, exec, and catch. For a detailed reference, please refer to the API documentation.

Contributing

Any and all contributions are welcome! Open an issue or submit a pull request to contribute.

This project uses Bun for development. To get started, clone the repository and run bun install to install dependencies. Then, run bun test to run the test suite.

To build the project, run bun build. The output will be in the dist directory.

License

This project is licensed under the MIT License.

More Repositories

1

bs5-lightbox

A pure JS lightbox gallery plugin for Bootstrap 5 based on the Modal and Carousel components
JavaScript
86
star
2

burnout-meter

Update Twitter profile with a meter showing how close you are to code burnout.
Rust
17
star
3

woody

A dead simple logger that works globally and across threads.
Rust
10
star
4

num2english

A Rust trait to convert numbers of any type and size to their English representation.
Rust
6
star
5

typescript-result-option

Implementation of Rust's Result and Option in TypeScript.
TypeScript
6
star
6

crab-snitch

Get an alert on macOS if an application accesses your microphone or webcam.
Objective-C
5
star
7

codecademy-brackets-theme

CSS
5
star
8

rust-sessions

Files, workspaces, etc... from Rust Sessions
Rust
4
star
9

codepen-brackets-theme

A theme for Brackets based on the http://codepen.io default theme.
CSS
3
star
10

shopify2skeleton

Shopify 2.0 Skeleton Theme
Liquid
3
star
11

codepen-brackets-panda-theme

A theme for Brackets referencing the http://codepen.io Panda theme.
CSS
2
star
12

quark-cms

Quark CMS - Content management on an atomic level.
PHP
2
star
13

twitch-chatgpt

A simple API for calling ChatGPT from Twitch chat.
JavaScript
2
star
14

collectionmaxxing

TypeScript collections library
TypeScript
2
star
15

generative-music

Rust
2
star
16

crabtml

CrabTML is a lightweight and flexible template engine written in Rust. It provides a simple yet powerful way to render dynamic content in your applications.
Rust
2
star
17

travvyterm

C
2
star
18

dnd-character-wizard

A CLI step-by-step character sheet generator for Dungeons & Dragons.
Rust
2
star
19

rust-time-tracker-cli

A simple time tracking program for the CLI written in Rust.
Rust
1
star
20

copyclip

A super simple CLI tool for piping text to the clipboard, written in Rust.
Rust
1
star
21

spotify-api-php

Simple PHP API wrapper for the Spotify Web API.
PHP
1
star
22

taw-bash-profile

My personal .bash_profile.
Shell
1
star
23

trvswgnr.github.io

CSS
1
star
24

shopify-ajax-api

AJAX API Wrapper for Shopify
TypeScript
1
star
25

song-lyric-password-generator

JavaScript
1
star
26

whos-that-pokemon

TypeScript
1
star
27

merge-css

combine css and remove duplicates
TypeScript
1
star
28

lukey-travvy-twitch-gif-thing

Rust
1
star
29

language-benchmarks

Shell
1
star
30

vimrc

my vanilla vim config
Vim Script
1
star
31

cdn

Personal CDN with jsdelivr.net
CSS
1
star
32

trvswgnr

1
star
33

camel-ts

OCaml's Result and Option modules in TypeScript, for funzies.
TypeScript
1
star
34

bigups

TypeScript
1
star
35

world-builder-webapp

HTML
1
star
36

dynamic_object

Dynamic, Type-Erased Key-Value Maps in Rust
Rust
1
star