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

piperize

v1.0.0

Published

Functional utility to create a pipeline of pure functions

Downloads

283

Readme

Piperize

Simple light utility to create functional pipelines in Typescript or Javascript.

Install

NPM: npm install --save-dev piperize

Yarn: yarn add piperize

Import piperize as and when you need with ES Modules:

import piperize from 'piperize';

Usage

Create a pipeline by simply calling piperize with a list of PURE functions (see a nice explanation of pure functions here)


const pipeline = piperize(
	func1,
	func2,
	func3
);

This will return a function to be called at your leisure:

const finalValue = pipeline(10);

Here is a simple full example:


import piperize from 'piperize';

const double = num => num * 2;
const minusTwo = num => num - 2;
const pipeline = piperize(
	double,
	minusTwo
);
const value = pipeline(10);

console.log(`Value = ${value}`);
//output: Value = 18

Async workflow?

Piperize has you covered with pipeAsync()

You can reliably pipe async or non-async functions with the pipeAsync utility function:


import { pipeAsync } from 'piperize';
//async function here
const double = async num => num * 2;
//non async function
const minus2 = num => num - 2;

const value = await pipeAsync(
	double,
	minus2
)(10);

console.log(value); //18

Utility middleware

Piperize comes with some useful utilties to help you in your functional programming work flow:

  • ifFalsy - great for piping in error handling or pipeline rescuing

import piperize, { ifFalsy } from 'piperize';

function willReturnFalsy(value) {
	return '';
}

function handleFalsy(value) {
	console.log(`piped value was falsy, oops. Here it is ${value}`);
	
	return 'this did though';
}

const value = piperize(
	willReturnFalsy,
	ifFalsy(handleFalsy)
)('wont get through');

console.log(value); //this did though
  • ifTruthy - to save you null checking
import piperize, { ifTruthy } from 'piperize';

function willReturnTruthy(value) {
	return value + ' is soooo truthy';
}

function handleTruthy(value) {
	return value + ' and you know it';
}

const value = piperize(
	willReturnTruthy,
	ifTruthy(handleTruthy)
)('this value');

console.log(value); //this value is soooo truthy and you know it
  • log - for logging values at any stage in the pipeline
import piperize, { log } from 'piperize';


const double = num => num * 2;
const minus2 = num => num - 2;
const buildMessage = num => `Num is ${num}`;

piperize(
    double,
    log,
    minus2,
    buildMessage,
    log
)(10);

//logs: 
//piperize(): Current pipe value: 20
//piperize(): Current pipe value: Num is 18
  • catchError/catchErrorAsync & ifError - piperize does not catch errors by default. This is intended. This is the fix.

import piperize, { catchError, ifError, log } from 'piperize';

const willError = num => { throw new Error('error'); };
const whenError = e => {
	//handle the error
	console.log(e);
	return 10;
};
const afterError = num => `Num is ${num}`;

piperize(
	catchError(willError),
	ifError(whenError),
	afterError,
	log
)(20);

// logs Num is 10
  • combine - combines the results of a series of pure functions into one object. Handy for consolidating lots of smaller functions or pipelines into a single object
import { combine } from 'piperize';

const doubled = num => ({
    doubled: num * 2
});
const tripled = num => ({
    tripled: num * 3
})

const args = combine(
	doubled,
	tripled
)(2);

console.log(args);
// { doubled: 4, tripled: 6 }
  • ifElse - simple curry utility to resolve a value from a truthy or falsy value
import { ifElse } from 'piperize';

//first param is the true value and second is the false one
const eitherOfThese = ifElse('foo', 'bar');

console.log(eitherOfThese(true)); //foo
console.log(eitherOfThese(false)) //bar
  • firstTruthy - passes a value through a series of functions and returns the first truthy value encountered
import { firstTruthy } from 'piperize';

const first = () => false;
const second = () => '';
const third = () => null;
const final = (num, multiplier) => num * multiplier * 2;
const value = firstTruthy(first, second, third, final)(2, 2);

expect(value).toEqual(8); // true