Skip to content

PatrickJS/switch-fn

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

switch-fn Build Status

Write a functional switch statement.

Install

$ npm install --save switch-fn

Usage

var Switch = require('switch-fn')

var fn = Switch({
  a: actionA,
  b: actionB,
  default: defaultAction
})

var result = fn('a') // => calls actionA with 'a' and gives back actionA's return value

API

Switch(cases) -> function

cases

Required Type: object

An object, with keys being the 'cases' to match against and values being the function to call in each case.

If no matching is found, and a 'default' case is given, it will be used.

More Examples

Getting crazy with value-pipe:

var pipe = require('value-pipe')
var Switch = require('switch-fn')

var onStatus = pipe(getStatus, Switch({
  active: actOnActive,
  inactive: actOnInactive,
  pending: actOnPending
}))

var result = onStatus(user)

function getStatus (user) {
  return user.status
}

You can pass in an array if you're only expecting numbers:

var Switch = require('switch-fn')

var fn = Switch([onZero, onOne])

fn(0)

License

MIT © Andrew Joslin

About

Write a functional switch statement.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%