mezza
v0.2.2
Published
An object switch-case. Selects the appropriate case from the object with variants
Downloads
12
Readme
Mezza 🎯
An object switch-case. Selects the appropriate case from the object with variants. Core part of mezzanine
npm install --save mezza
Usage
import choose from 'mezza'
function findUsers(username) {
/* Some function that returns a list of users */
}
const cases = {
Some: value => Array.isArray(value) && value.length > 1,
One : value => Array.isArray(value) && value.length === 1,
None: value => Array.isArray(value) && value.length === 0
}
const actions = {
Some: list => 'Find ' + list.length + ' users',
One : list => 'Find one user: ' + list[0],
None: () => 'Nothing found',
_ : () => 'Oops, something going wrong!' //Optional default case
}
function printUsers(username) {
const found = findUsers(username)
const resultText = choose(cases, actions, found)
return resultText
}
Method is curried by default
const printText = choose(cases, actions)
printText(['username']) // => Found one user: username
printText({}) // => 'Oops, something going wrong!'
Try this example in browser
License
The project is released under the Mit License