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

@graficos/pipe-js

v0.0.3

Published

functional pipes in js

Downloads

31

Readme

Pipe JS

Unit Tests

Installation

npm i @graficos/pipe-js

Intro

Pipes are a pattern of function composition. They enable you to do several transformations to a value in 1 step.

You pass a number of functions and a parameter that will be consumed by these functions, so you can make some transformations to this value.

How are they useful?

For example, let's imagine this scenario, where you're applying the result of a function to another function:

var result = f(g(x))

I bet you have seen it multiple times. And something even more scary:

var result = f(g(h(i(j(x))))) // 🤔

How are you supposed to read or even debug that? This is where pipes are useful:

// The first example would be the same as:
var result = pipe(g, f)(x)

// and the second example:
var result = pipe(j, i, h, g, f)(x)

You'll notice that with pipe, we read the transformations left-to-right (which was the same as "from the inside to the outside" in the initial examples), which improves the readability.

Don't be scared about the (...)(x) syntax, that's how you call "a function that returns a function", which is what pipe is. It's also called a "Higher Order Function", because you pass functions as parameters.

How to debug pipes

If you need to see what's happening on each step, you can create what we usually call a tap function ("tap", "pipe", ...see the correlation? ;)). Imagine we have the following example:

import { pipe } from '@graficos/pipe-js'

// `formatText` is the combination of other "transformations" applied sequentially.
const formatText = pipe(removeHTMLTags, formatAuthorInfo, addFooterText)

const myFormattedText = formatText(sourceText)

We could debug each step with the tap function:

import { pipe } from '@graficos/pipe-js'

const tap = (currentValue) => {
  console.log(currentValue)
  return currentValue // remember, all your functions must return the value to be passed to the next function
}

// `formatText` is the combination of other "transformations" applied sequentially.
const formatText = pipe(
  removeHTMLTags,
  tap,
  formatAuthorInfo,
  tap,
  addFooterText
)

const myFormattedText = formatText(sourceText)
console.log({ myFormattedText })

What if my functions are asynchronous?

That's why pipe-js comes also with an asyncPipe method (tree-shakeable if you don't use it).

Example:

import { asyncPipe } from '@graficos/pipe-js'

const someAsyncFunction = (value) =>
  new Promise((resolve) => resolve(value + 2))
const someOtherAsyncOperation = (value) =>
  new Promise((resolve) => resolve(value * 3))

const result = await asyncPipe(someAsyncFunction, someOtherAsyncOperation)(1) // -> result === 9

Just remember that just like pipe always returned a function, asyncPipe will always return a Promise. You, you need to uwrap the value that it'll return with async/await or using .then().

Examples

You can see some tests to see more examples in /src (the *.spec.js files).

Another example would be "composable derived data". Sometimes they are called selectors or "computed properties":

const addHello = (value) => `Hello, ${value}`
const toUpperCase = (value) => String.prototype.toUpperCase.call(value)

const person = {
  name: 'Paul',
  getGreet() {
    return pipe(addHello, toUpperCase)(this.name)
  },
}

console.log(person.getGreet()) // -> HELLO, PAUL
person.name = 'Hello'
console.log(person.getGreet()) // -> HELLO, HELLO

In this example, any time name changes, the derived state will be updated (as it is a function invoked everytime with the current this.name).

About the library

  • It's done in JS and using JSDoc, so you'll have proper autocompletion in your editor.

  • You can use it both in the browser and in node, as it is exported as UMD, ESM and CJS.

Read more about "pipes":