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

callpipe

v1.0.1

Published

Linear function chaining for JavaScript

Downloads

7

Readme

callpipe

callpipe lets you chain functions linearly in JavaScript without syntax transformers. It is a combination of the proposed call and pipe operators.

Methods

To invoke a method of any arity, pass the function as the first argument to call (the internal name of the closure returned by pipe) and the rest of the parameters. To extract the result, call it without any parameters.

import { pipe } from 'callpipe';

const { concat, toUpperCase } = String.prototype;

function exclaim() {
	return this + '!';
}

function prepend(string) {
	return string + this;
}

console.assert(
	pipe
		('hello')
		(concat, ' ', 'world')
		(toUpperCase)
		(exclaim)
		(prepend, '🌎 ')
		()
	=== '🌎 HELLO WORLD!'
);

/*
prepend.call(
	exclaim.call(
		'hello'
			.concat(' ', 'world')
			.toUpperCase()),
	'🌎 ')
*/

Data-First Functions

There are two ways of invoking regular functions. You can either pass in an index as the first parameter or a symbol. For index substitution, type inferencing is supported by up to 10 parameters. For placeholder substitution, it is best to use a unique symbol such as the one exported by callpipe: $. For data-first functions, using index substitution and passing 0 is preferred since it's shorter.

import { $, pipe } from 'callpipe';

function* count() {
	for (let i = 0; ; ++i) {
		yield i;
	}
}

function* take(iterator, length) {
	iterator = iterator[Symbol.iterator]();
	while (length-- > 0) {
		const result = iterator.next();
		if (result.done) {
			return;
		}
		yield result.value;
	}
}

function* map(iterator, callback) {
	for (const value of iterator) {
		yield callback(value);
	}
}

function reduce(iterator, callback, accumulator) {
	for (const value of iterator) {
		accumulator = callback(accumulator, value);
	}
	return accumulator;
}

const double = x => x * 2;
const add = (x, y) => x + y;

console.assert(
	pipe
		(count())
		(0, take, 10)
		(0, map, double)
		(0, reduce, add, 0)
		()
	=== 90
);

console.assert(
	pipe
		(count())
		($, take, $, 10)
		($, map, $, double)
		($, reduce, $, add, 0)
		()
	=== 90
);

/*
reduce(
	map(
		take(
			count(),
			10),
		double),
	add,
	0);
*/

Data-Last Functions

Data-last functions are invoked the same way as data-first functions, except you'll have to pass in -1 instead of 0. Placeholder substitution works the same.

import { $, pipe } from 'callpipe';

function add(x, y, z) {
	return x + y + z;
}

console.assert(
	pipe
		(3)
		(-1, add, 1, 2)
		()
	=== 6
);

console.assert(
	pipe
		(3)
		($, add, 1, 2, $)
		()
	=== 6
);

/*
add(1, 2, 3);
*/

Data-N Functions

You're not restricted to 0 and -1 as indexes.

import { $, pipe } from 'callpipe';

function add(x, y, z) {
	return x + y + z;
}

console.assert(
	pipe
		(2)
		(1, add, 1, 3)
		()
	=== 6
);

console.assert(
	pipe
		(2)
		($, add, 1, $, 3)
		()
	=== 6
);

/*
add(1, 2, 3);
*/

Internals

As mentioned earlier, pipe returns a function call that closes over the passed in value referred to as the accumulator. Please keep in mind that the accumulator is overwritten whenever call is invoked with a function (referred to as the reducer). Unless no parameters are passed to call, it will always return itself.

import { pipe } from 'callpipe';

// reducer
function increment() {
	return this + 1;
}

// accumulator
const initial = 0;

// closure
const call = pipe(initial);

const a = call(increment);
console.assert(a() === 1);

const b = call(increment);
console.assert(b() === 2);

console.assert(a() === 2);
console.assert(b() === 2);
console.assert(a === b);