npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

functs

v1.1.9

Published

Merge multiple functions into one.

Downloads

5

Readme

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'