functional-switch

1.1.3 • Public • Published

Functional Switch

A functional implementation of switch.

This library is in alpha now. It would be great if you help me try it.

Installation

npm install functional-switch --save

Usage

import { cond, when, otherwise } from 'functional-switch'
 
// simple example
const isFruitOrVegetable = cond(
  when('apple', 'isFruit'),
  when('orange', 'isFruit'),
  when('eggplant', () => {
    return 'isVegetable'
  }),
  otherwise('isFruit')
)
 
isFruitOrVegetable('apple') // isFruit

API

when()

when(
  clause: any,
  statement: any
): [clause, statement]

otherwise()

otherwise(
  statement: any
): [statement]

cond()

cond(
  ...conditions: [clause, statement] | [statement]
): (expression: any) => any

Package Sidebar

Install

npm i functional-switch

Weekly Downloads

8

Version

1.1.3

License

MIT

Last publish

Collaborators

  • wuct