Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

About

Kleisli composition in JS

Resources

License

Releases

No releases published

Packages

No packages published