Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

justinvdm/multifn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

multifn

note For now, this project is deprecated. I no longer use or work on it, and doubt others are using it. If by any chance you were using this, and would like to see more of it in some form, let me know: justnvdm(at)gmail.com

create functions with multiple overrides.

var multifn = require('multifn');

var fn = multifn()
  .if(function(a, b) { return a && b; })
  .do(function(a, b) { return a + b; })

  .if(function(a, b) { return a && !b; })
  .do(function(a) { return this(a, 1); })

  .else(function() { return 1; });

fn();  // 1
fn(3);  // 4
fn(2, 3);  // 5

install

node:

$ npm install multifn

browser:

$ bower install multifn
<script src="/bower_components/multifn/multifn.js"></script>

api

multifn()

Creates a new multifn. When the multifn is invoked, its if functions will be invoked in the order they were specified, short circuiting when a matching if function is found and returning the result of the corresponding do function.

var fn = multifn()
  .if(function(a, b) { return a && !b; })
  .do(function(a) { return a; })

  .if(function(a, b) { return a && b; })
  .do(function(a, b) { return a + b; });

fn(3);  // 3
fn(2, 3);  // 5

multifn().if(ifFn).do(doFn)

Defines a new override for the multifn. When ifFn returns true, doFn will be invoked, its result used as the multifn's invokation result. Both ifFn and doFn will be invoked with the arguments that the multifn was invoked with, and will both have the multifn as their this context.

var fn = multifn()
  .if(function(a, b) { return a && b; })
  .do(function(a) { return a + b; })

fn(2, 3);  // 5

multifn().else(elseFn)

Defines a new fallback function to use when none of the overrides match. Defaults to a 'noop': function() {}.

var fn = multifn()
  .if(function(a, b) { return a && b; })
  .do(function(a) { return a + b; })
  .else(function() { return 1; });

fn();  // 1

About

create functions with multiple overrides

Resources

License

Stars

Watchers

Forks

Packages

No packages published