npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fluent-piper

v3.1.0

Published

Type-safe left-to-right functional pipeline composition

Downloads

51

Readme

fluent-piper

Have you see code like dispatch(intUserEvent(await findActiveUser(flatten(ids.map((it) => it.split(",")))))) and felt: wow, that's ugly.

Such code is hard to read is because to understand it you need to first unwrap it inside out. The FP world has had a solution for this for quite some time: functional pipelines.

This library brings a similar approach to typescript: You can now refactor the above code to be:

await pipe(ids)
    .thru(ids => ids.map(it => it.split(",")))
    .thru(flatten)
    .thru(findActiveUser)
    .await()
    .thru(initUserEvent)
    .thru(dispatch)
    .value

This is longer, but also easier to read because we can follow the logic top -> down, left -> right in natural reading order.

Also, unlike many similar javascript libraries, this is fully type-safe and does not impose any limitations on the number of steps your chain can have.

Installation

pnpm (recommended):

pnpm install fluent-piper

npm:

npm install fluent-piper

Usage - Eager evaluation

We pass an initial value to pipe and chain steps using .thru and finally call .value to get the result.

import {pipe} from "fluent-piper";

const result = pipe(10).thru((i: number) => i + 1).thru((i: number) => `Result: ${i}`).value;
//                   |           ^            |          ^
//                   |___________|            |__________|----- Types of Subsequent output -> input pairs must match
//                         |
//                         Initial input must match input of first step
//
// result: string = "Result: 11"
//         ^        ^
//         |        |__ Result of left-to-right composition
//         |
//         |__ Output type inferred from output type of last step
//

In above usage, steps are eagerly evaluated - every step passed to .thru gets immediately executed.

With async steps

We can use .await() to ensure that next step receives resolved value instead of a promise

import {pipe} from "fluent-piper";

const result = pipe(10)
    .thru(async (i: number) => i + 1)
    .await()
    .thru((i: number) => `Result: ${i}`)
        // ^--- Not a promise
    .value;
// result: Promise<string>

Advanced usage - Lazy Pipelines

There is a lazy API that offers few more features at the cost of higher overhead. The lazy API builds up a chain of thunks that get executed when the final .value is called. Until .value is invoked, nothing gets executed.

Catching exceptions

If some of the steps can throw, we can use .catch to handle them within the pipeline and chain subsequent steps

import {pipe} from "fluent-piper"

const departmentName = await pipe.lazy({ id: 1})
    .thru(fetchUser)
    .await()
    .thru(fetchDepartment)
    .await()
    .catch(e => {
        // Catch errors thrown from previous steps (sync/async)
        console.error(e);
        return null;
    })
    .thru(dept => dept?.name)
    .value;

Bailing early

A more railway oriented approach to handle early exits in a type-safe manner is available through .bailIf:

import {pipe} from "fluent-piper"

const departmentInfo = await pipe.lazy({ id: 1})
    .thru(fetchUser)
    .await()
    .bailIf(
        user => !user.departmentId, // Decide if to bail
        user => ({ type: "unassigned" as const }) // Value to bail with
    )
    .thru(fetchDepartment) // We will reach here only if we didn't bail above
    .await()
    .bailIf(
        isPublic, // (dept) => boolean
        dept => ({ type: "classified" as const })
    )
    .thru(dept => ({ type: "public" as const, name: dept.name })) // Only if dept is public
    .value; // Promise<{ type: "unassigned" } | { type: "classified" } | { type: "public", name: string }>

License

MIT