Skip to content

quinnnned/to-fun

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

to-fun

Advanced Left-To-Right Function Composer

Build Status npm version Coverage Status

Better Function Composition

Don't Do This:

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

Do This Instead!

// YES! Clear, Ordered Steps!
const thingsToDo = [
    doFirstThing, 
    doSecondThing, 
    doThirdThing, 
    doLastThing
];

// To Function!
const doSomeThings = toFun(thingsToDo);

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

Or Even Skip the Explicit Array Declaration!

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

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

Got An Async Process?

to-fun understands promises!

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

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

// To Function!
const activateByName = toFun(findUserByName, activateUser, db.users.update);

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

Got a Complex Multi-Stage Process?

to-fun supports nested composition!

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

Shut up and take my money!

Installation (npm):

npm install --save to-fun

For the import-ers:

import f from 'to-fun';

For the require-ers:

var f = require('to-fun').default;

About

Advanced Left-to-Right Function Composer

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published