switch-js
Install
npm i switch-js
Example
const switcher = ;const valueToTest = 'foo'; ; // result:// foo!// this is cool!
Check for multiple cases:
; // result: foo or bar!
Check if value is matched by a function:
; // result: a string!
Use to
to set value:
const valueInUppercase = value'NO MATCH FOUND!'; // result: FOO! // .value('NO MATCH FOUND!')// has the same effect as// .default().to('NO MATCH FOUND!').value()
Use mapTo
to set the value by a function:
const valueInUppercase = value; // result: 'FOO'
API
switch(value)
start a switch chain
value
Value to check
case(caseValue[, matchedCallback][, notMatchedCallback])
The case method you know
caseValue
matchedCallback
Function, optional. Invoked ifcaseValue
equals deeply tovalue
notMatchedCallback
Function, optional. Invoked otherwise
cases(caseValues[, matchedCallback][, notMatchedCallback])
Check for multiple case values
caseValues
Array ofcaseValue
matchedCallback
Function, optional. Invoked ifcaseValues
includesvalue
notMatchedCallback
Function, optional. Invoked otherwise
caseWhen(matcher[, matchedCallback][, notMatchedCallback])
Check by a function
matcher
A match function used to check ifvalue
is a matchmatchedCallback
Function, optional. Invoked ifmatcher(value)
returns a truthy valuenotMatchedCallback
Function, optional. Invoked otherwise
default([noMatchFoundCallback, matchFoundCallback])
The default method you know
noMatchFoundCallback
Function, optional. Invoked if no match is foundmatchFoundCallback
Function, optional. Invoked otherwise
always(callback)
Invoked anyway
callback
Function
to(valueToReturn)
Set a new value to return by value()
if previous case is a match or it follows default()
valueToReturn
mapTo(mapper)
Similiar to to()
but set value by a mapper function
mapper
Function | String. If a string is passed, it will take as a path and invoke_.get(valueToTest, path)
from lodash
value(defaultValue)
Return the value
defaultValue
Default value will return if no match is found