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

@cdandrea/piping-ts

v1.0.8

Published

100% type-safe pipe() function written in TS

Downloads

8

Readme

100% type-safe pipe() and compose() functions written in TS

According to Wikipedia, "Function composition is an act or mechanism to combine simple functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole".

Piping is basically the same idea, but in reverse order, where the functions are provided in a more natural order (i.e. the same order as they are actually invoked).

This package provides a simple way to transform functions into their piped or composed versions using Higher-Order Functions (HOFs) called pipe() and compose(), respectively, while keeping the original functions signatures, making sure that, for each function in the sequence:

  • pipe(): the type of the output (result) of the previous function will always match the type of the input of the next function. The type of the parameter of the piped function will always match the type of its first function's parameter and the final result will have the type of its last function.
  • compose(): the type of the input of the previous function will always match the type of the output (result) of the next function. The type of the parameter of the composed function will always match the type of its last function's parameter and the final result will have the type of its first function.

Features

  • 100% type-safe
  • Supports functions that have up to 30 parameters
  • Fully compatible with JavaScript
  • Very small (both functions have under 250 bytes gzipped after bundling)
  • No external dependencies

Installation

Install from npm using your favorite package manager:

npm install @cdandrea/piping-ts
yarn add @cdandrea/piping-ts
pnpm install @cdandrea/piping-ts

Usage

Pipe:

Simply supply the functions to be piped as parameters of the pipe() HOF.

Basic example:

import { pipe } from '@cdandrea/piping-ts'

const isEven = (n: number): boolean => n % 2 === 0
const asString = (b: boolean): string => b.toString()
const asArray = (s: string): string[] => [s]

const f = pipe(isEven, asString, asArray)

// f's signature will be: `(a1: number) => string[]`
//
// Notice how `f` expects a `number` as input, which is the same
// type expected by its first function parameter, `isEven`, and
// returns a `string[]` as the result, which is the result of
// its last function parameter, `asArray`.

console.log(f(10)) // Prints ["true"]

More elaborate example, using pipe() and curry():

import { curry } from '@cdandrea/currying-ts'
import { pipe } from '@cdandrea/piping-ts'

const prop = curry((prop: string, obj: Record<string, number>) => obj[prop])
const add = curry((a: number, b: number) => b + a)
const modulo = curry((a: number, b: number) => b % a)
const amtAdd1Mod7 = pipe(prop('amount'), add(1), modulo(7))

console.log(amtAdd1Mod7({ amount: 17 })) // Prints 4
console.log(amtAdd1Mod7({ amount: 987 })) // Prints 1
console.log(amtAdd1Mod7({ amount: 68 })) // Prints 6

Notice that the piped function returned by pipe() gets fully typed.

Compose:

Simply supply the functions to be composed as parameters of the compose() HOF:

import { curry } from '@cdandrea/currying-ts'
import { compose } from '@cdandrea/piping-ts'

const prop = curry((prop: string, obj: Record<string, number>) => obj[prop])
const add = curry((a: number, b: number) => b + a)
const modulo = curry((a: number, b: number) => b % a)
const amtAdd1Mod7 = compose(modulo(7), add(1), prop('amount'))

console.log(amtAdd1Mod7({ amount: 17 })) // Prints 4
console.log(amtAdd1Mod7({ amount: 987 })) // Prints 1
console.log(amtAdd1Mod7({ amount: 68 })) // Prints 6