Skip to main content
Home

Built and signed on GitHub Actions

Utils for piping and composing functions.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
76%
Published
10 months ago (0.1.0)

Pipe

Utils for piping and composing functions.

import { flow, pipe } from "@gordonb/pipe";

const double = (n: number) => n * 2;
const addOne = (n: number) => n + 1;
const toString = (n: number) => n.toString();

const result = pipe(5, double, addOne, toString);
// "11"

const combined = flow(double, addOne, toString);
combined(5);
// "11"

This module offers four functions:

  • pipe(value, ...fns) pipe value through one-argument functions, returning result.
  • pipeAsync(value, ...fns) pipe value through async functions, returning promise for result. The return value of each step is awaited. The next function in the chain will receive the awaited value. pipeAsync allows using sync or async functions in the pipeline.
  • flow(...fns) composes many one-argument functions into a single one-argument function that will apply them left-to-right. This is the opposite direction of traditional function composition, but it reads more nicely with the way JavaScript is typically read/written.
  • flowAsync(...fns) composes many one-argument async functions into a single one-argument async function that will apply them left-to-right. flowAsync supports using both async and ordinary one-argument functions.

All functions support typesafe overloads for up to 20 functions.

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@gordonb/pipe

Import symbol

import * as pipe from "@gordonb/pipe";
or

Import directly with a jsr specifier

import * as pipe from "jsr:@gordonb/pipe";

Add Package

pnpm i jsr:@gordonb/pipe
or (using pnpm 10.8 or older)
pnpm dlx jsr add @gordonb/pipe

Import symbol

import * as pipe from "@gordonb/pipe";

Add Package

yarn add jsr:@gordonb/pipe
or (using Yarn 4.8 or older)
yarn dlx jsr add @gordonb/pipe

Import symbol

import * as pipe from "@gordonb/pipe";

Add Package

vlt install jsr:@gordonb/pipe

Import symbol

import * as pipe from "@gordonb/pipe";

Add Package

npx jsr add @gordonb/pipe

Import symbol

import * as pipe from "@gordonb/pipe";

Add Package

bunx jsr add @gordonb/pipe

Import symbol

import * as pipe from "@gordonb/pipe";