Skip to content
Kleisli composition in JS
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test
.gitignore
LICENSE
README.md
compose.js
index.js
package-lock.json
package.json
pipe.js

README.md

fish

The (>=>) and (<=<) operators in JavaScript. Kinda.

Install

npm install fish-operator

Usage

The first parameter of these functions is the string or Symbol key of the bind method for the objects returned by the functions to compose. A bind method will have the signature bind :: (A -> m<B>) -> m<B> for a given m<A>. For Promises, this will be the then method so you will pass in 'then' as the first parameter. The following examples show this idea using Promise-returning functions:

const { compose, pipe } = require('fish-operator');

const f = x => Promise.resolve(x + 1);
const g = x => Promise.resolve(x * 2);

const h = compose('then')(f, g);
await h(10);
// <- 21

const j = pipe('then')(f, g);
await h(10);
// <- 22

In the real world, you would probably use these functions like this:

const { pipe } = require('fish-operator');
const pipeP = pipe('then');
const prop = key => obj => obj[key];

// Fictitious Promise-returning API functions...
const getUserById = _id => Promise.resolve({ _id, name: 'Susan', jobId: 2 });
const getJobById = _id => Promise.resolve({ _id, name: 'Rattlesnake Groomer' });

const getJobByUserId = pipeP(getUserById, prop('jobId'), getJobById, prop('name'));
await getJobByUserId(10);
// <- 'Rattlesnake Groomer'

Each function accepts any number of functions that return either a value or an object with a method that matches the bindKey parameter given.

License

WTFPL

You can’t perform that action at this time.