freddies

The Zero-Dependency Left-To-Right Function Composer!


Keywords
functional, function, left-to-right, fp, reduce, compose, redux, elm, state, reducer, monad, freddy
License
MIT
Install
npm install freddies@1.0.2

Documentation

freddies

The Zero-Dependency Left-To-Right Function Composer!

Build Status npm version Coverage Status

What's A "Freddy"?

That's a term I just made up for a process defined as array of functions!

...it also comes from Function-Reducible Array

Show Me!

Don't Do This:

// NO! Pyramid of DOOM! Reversed Process Order!
const foo = doLastThing(
                doThirdThing(
                    doSecondThing(
                        doFirstThing(a, b, c)
                    )
                )
           );

Do This Instead!

// YES! Freddies are defined as ordered steps!
const myFreddy = [
    doFirstThing, 
    doSecondThing, 
    doThirdThing, 
    doLastThing
];

// Freddy to function!
const doSomeThings = f(myFreddy);

// And done!
const foo = doSomeThings(a, b, c);

Or Even Skip the Explicit Array Declaration!

const doSomeThings = f(
    doFirstThing, 
    doSecondThing, 
    doThirdThing, 
    doLastThing
);

const foo = doSomeThings(a, b, c);

Got An Async Process?

Freddies understand promises!

const findUserByName = (name) => { 
    return db.users.findOne({name}) 
};

const activateUser   = (user) => {
    user.activated = true;
    return user;
};

// Freddy to function!
const activateByName = f(findUserByName, activateUser, db.users.update);

// Invoke and catch errors like usual
activateByName('Fred').catch(errorHandler);

Got a Complex Multi-Stage Process?

Freddies can contain other freddies!

const access = [readSession, checkPermissions ]
const sanitizeForm = [ sanitizeXss, sanitizeSqlInject ]
const validateForm = [ checkNonce, validateComment]
const clean  = [sanitizeForm, validateForm];
// clearly-defined flow 
on('add-comment', f(
    access,
    clean,
    db.comments.insert,
    respondOk
)).catch(respondWithError);

Shut up and take my money!

Installation (npm):

npm install --save freddies

For the import-ers:

import f from 'freddies';

For the require-ers:

var f = require('freddies').default;