mezza
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

Mezza 🎯

An object switch-case. Selects the appropriate case from the object with variants. Core part of mezzanine

npm install --save mezza

NPM version Unix Build Status Windows Build Status Coveralls Status Dependency Status

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

Edit Mezza example

License

The project is released under the Mit License

Package Sidebar

Install

npm i mezza

Weekly Downloads

4

Version

0.2.2

License

MIT

Last publish

Collaborators

  • drelliot