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

pipe-functions

v1.3.0

Published

Pipe functions in a Unix-like style. It supports Promises (async) anywhere in the pipeline and every step will be executed sequentially. The return (resolve in case of Promises) of each function will be passed in as an argument to the next one

Downloads

272,910

Readme

pipe-functions

Build Status

Pipe functions in a Unix-like style. It supports Promises (async) anywhere in the pipeline and every step will be executed sequentially. The return (resolve in case of Promises) of each function will be passed in as an argument to the next one

Key features:

  • Supports Promises, or any lib following the Promises/A+ spec about being thenable (.then() method)
  • Promises will be executed sequentially
  • First argument can be of any type (String, Number, Date, etc.) or even a Function or a Promise
  • Node.js and Browser ready (to be used on a Browser, without a build step, check lib/pipe-non-es6.js)
  • Lightweight, 501 bytes, before gzip!

Install

NPM / Node

npm install pipe-functions

Usage

Sync

const pipe = require('pipe-functions');

// First argument can be of any type
const result = pipe('input', fn1, fn2, fnN);
// And also a function
const result2 = pipe(fn0, fn1, fn2, fnN);

Async (Promises)

If the pipeline contains a Promise anywhere in the pipeline, we must treat pipe like a Promise itself, so we must to use .then() to get the final result.

const pipe = require('pipe-functions');

// First argument can be of any type, as shown in the previous example
pipe('input', fn1, fnPromise1, fn2, fnPromise2).then(console.log);

A suggestion regarding Promises. Probably you've seen, or had to write, a stack of promises like that:

someAsyncFunction('param')
  .then(doSomethingWithTheResult)
  .then(doSomethingElseWithTheResultOfTheLast)
  .then(oneMore)
  .then(almostThere)
  .then(done)
  .catch(console.log)

It could be written as:

const pipe = require('pipe-functions');

pipe(
    someAsyncFunction('param'),
    doSomethingWithTheResult,
    doSomethingElseWithTheResultOfTheLast,
    oneMore,
    almostThere,
    done
).catch(console.log)

Examples

OBS: Some examples needs a platform with support for Destructuring (Nodejs v6+, Chrome).

Sync

const pipe = require('pipe-functions');

/** Functions **/
const capitalize = v => v[0].toUpperCase() + v.slice(1);
const quote = v => `"${v}"`;

/** Pipe **/
// result will be: "Time"
const result = pipe('time', capitalize, quote);

Async (Promises)

const pipe = require('pipe-functions');

/** Functions **/
// Sync
const capitalize = v => v[0].toUpperCase() + v.slice(1);
// Async
const fetchAndSetBandName = v => new Promise((resolve, reject) => setTimeout(() => resolve(`Pink Floyd - ${v}`), 1000));

/** Pipe **/
// the result will be: Pink Floyd - Time
pipe('time', capitalize, fetchAndSetBandName).then(console.log)

Example with destructuring,

To easily pass in more than one value (within an Object or Array) through the pipeline.

const pipe = require('pipe-functions');

/** Functions **/
// Async
const fetchBandName = ({ song }) => new Promise((resolve, reject) =>
setTimeout(() => resolve({ song, band: 'Pink Floyd' }), 1000));
// Sync
const concatBandAndSong = ({ song, band }) => `${band} - ${song}`;

/** Pipe **/
// the result will be: Pink Floyd - Time
pipe('time', fetchBandName, concatBandAndSong).then(console.log)