package
Write a functional switch statement.
npm install switch-fnGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Write a functional switch statement.
$ npm install --save switch-fnvar 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 valueSwitch(cases) -> functionRequired
Type: object
An object, with keys being the 'cases' to match against and values being the function to call in each case.
If no case matching the input is found and a 'default' case is given, it will be used.
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
}var Switch = require('switch-fn')
var fn = Switch([onZero, onOne])
fn(0)var fib = Switch({
0: (n) => n,
1: (n) => n,
default: (n) => fib(n - 1) + fib(n - 2)
});
fib(10) // => 55MIT © Andrew Joslin