Giter Club home page Giter Club logo

functs's Introduction

functs

Merge multiple functions into one.

bitHound Code bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies License: MIT

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 = 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!'

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 = require('functs');
const a = functs();

Example - Add

//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';
});

Example - Remove

//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);

Example - Execute

//since the functs-object is a function the execution is simple: just run it.
b();

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 = 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();

.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 = 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'

functs's People

Contributors

jonesderking avatar robojones avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

steebchen

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.