functs
Merge multiple functions into one.
Introduction
functs
is a lightweight npm module for merging multiple functions into one.
You can use this e.g. for callbacks, event-handlers, etc.
Example - Demo
const functs = ; //create some functions{ console;}{ console;} //merge them into one functionconst a = ; //run it; //=> 'Welcome in the world of internet!'
Getting Started
Installation
npm install functs
Browser Version
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.
Example - Create
const functs = ;const a = ;
Example - Add
//you can add functions to a functs object by passing them to the creatorconst b = ; //the other way to add functions is the .add methodb;
Example - Remove
//to remove a function you need to pass the function itself to the .remove methodconst key = b;b;
Example - Execute
//since the functs-object is a function the execution is simple: just run it.;
functs()
Arguments
functions
to be included.
Returns
A new functs-object
that includes all functions
given as arguments.
Methods
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
.
.add()
With .add()
you can add functions
to a Functs-object
.
Arguments
One or more functions
or
an Array
of functions
.
Returns
An Array
of the given arguments.
Usage
const functs = ; //create a new functs-objectvar a = ; //add functionsa; //run the added functions;
.remove()
With .remove
you can remove functions
from a functs-object
.
Arguments
One or more functions
or
an Array
of functions
(Tip: You can use the Array
that .add()
returns).
The included functions
will be removed.
Returns
ùndefined
abort()
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
.
Arguments
none
Returns
undefined
Example - Error Handling
const functs = ; //create a new functs-objectvar a = ; { iferror console; //do not continue if an error occurs ; } { //this only gets executed when the errorHandler does not call abort console;} //run the functions in the Functs-object ;//this will log: 'Yayy! Got some result: nice result' ;//this will log: 'error: extremely critical error'