Merge multiple functions into one.
functs
is a lightweight npm module for merging multiple functions into one.
You can use this e.g. for callbacks, event-handlers, etc.
const functs = require('functs');
//create some functions
function f1(first){
console.log('Welcome', first);
}
function f2(first, second){
console.log('world', second);
}
//merge them into one function
const a = functs(f1, f2);
//run it
a('in the', 'of internet!'); //=> 'Welcome in the world of internet!'
npm install functs
Since functs is using ES6 you may not use it in the Browser because of compatibility.
I have added a version with better compatibility to the module.
Just copy the file functs/functs-browser.js
from the node_modules
to your public directory.
const functs = require('functs');
const a = functs();
//you can add functions to a functs object by passing them to the creator
const b = functs(() => {
return 'function 1';
}, () => {
return 'function 2';
});
//the other way to add functions is the .add method
b.add(() => {
return 'function 3';
});
//to remove a function you need to pass the function itself to the .remove method
const key = b.add(() => {
return 'function 4';
});
b.remove(key);
//since the functs-object is a function the execution is simple: just run it.
b();
functions
to be included.
A new functs-object
that includes all functions
given as arguments.
The functs-object
is a function
(with some extra methods) so you can use methods like .apply()
or .call()
. The this
-argument of the functs-object
will be applied on all included functions
.
With .add()
you can add functions
to a Functs-object
.
One or more functions
or
an Array
of functions
.
An Array
of the given arguments.
const functs = require('functs');
//create a new functs-object
var a = functs();
//add functions
a.add(() => {
console.log('hi');
}, () => {
console.log('foo');
});
//run the added functions
a();
With .remove
you can remove functions
from a functs-object
.
One or more functions
or
an Array
of functions
(Tip: You can use the Array
that .add()
returns).
The included functions
will be removed.
ùndefined
All functions
in a functs-object
can recieve this method as last argument.
It gets appended to the arguments given when executing the functs-object
.
none
undefined
const functs = require('functs');
//create a new functs-object
var a = functs(errorHandler, doSomething);
function errorHandler(error, result, abort) {
if(error) {
console.log('error:', error);
//do not continue if an error occurs
abort();
}
}
function doSomething(error, result) {
//this only gets executed when the errorHandler does not call abort
console.log('Yayy! Got some result:', result);
}
//run the functions in the Functs-object
a(null, 'nice result');
//this will log: 'Yayy! Got some result: nice result'
a('extremely critical error');
//this will log: 'error: extremely critical error'