freddies
v1.0.2
Published
The Zero-Dependency Left-To-Right Function Composer!
Downloads
28
Maintainers
Readme
freddies
The Zero-Dependency Left-To-Right Function Composer!
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;