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

@k88/pipe-compose

v2.5.0

Published

ES6 pipe and compose in JavScript

Downloads

70,858

Readme

What is it?

This library exports 6 functions: pipe, compose, cPipe, cCompose, pPipe, and pCompose.

Pipe and Compose

To paraphrase the Medium post, image you want to pipe the output of n-functions together. You'll end up with something like:

reverse(get3Chars(uppercase('the-argument')));
// EHT

In this example, you first uppercase to get THE-ARGUMENT, then you get3Chars and have THE and finally you reverse it to get EHT.

With pipe you can accomplish the same thing using:

pipe('the-argument', uppercase, get3Chars, reverse);

It helps to read this similar to bash's pipe |:

echo 'the-argument' | uppercase | get3Chars | reverse;

compose is the reverse of pipe. For example these two would return the same result.

pipe('the-argument', uppercase, get3Chars, reverse);
compose('the-argument', reverse, get3Chars, uppercase);

cPipe and cCompose

These are composable versions of pipe and compose, i.e.:

const textModifier = cPipe(uppercase, get3Chars, reverse);

textModifier('the-argument');
// EHT

textModifier('another-argument');
// ONA

pPipe and pCompose

There are also composable promise version of the pipe and compose.

const calc = pPipe(add1, multiply2, divide3);
await calc(2);
// 2

In this example, we are creating a calculation function that takes in an number, and performs basic math operation on it. In this case, we are passing 2 and first adding 1 to it (giving us 3), then multiplying by 2 (giving us 6) and finally dividing by 3 (giving us back 2).

As before, pCompose would work in the reverse:

const calc = pCompose(add1, multiply2, divide3);
await calc(6);
// 5

How to use it

Installing NPM package

Install from npm:

npm install @k88/pipe-compose

and use it:

import { pipe, compose } from '@k88/pipe-compose';

Get the source code

All of these auxiliary functions are one-liner, so you can also just copy/paste them into your application:

const pipe = (x, ...fns) => fns.reduce((v, f) => f(v), x);
const compose = (x, ...fns) => fns.reduceRight((v, f) => f(v), x);

const cPipe = (...fns) => (x) => => fns.reduce((v, f) => f(v), x);
const cCompose = (...fns) => (x) => fns.reduceRight((v, f) => f(v), x);

const pPipe = (...fns) => async (x) => fns.reduce(async (v, f) => f(await v), x);
const pCompose = (...fns) => async (x) => fns.reduceRight(async (v, f) => f(await v), x);