functional-switch

A functional implementation of switch.

Usage no npm install needed!

<script type="module">
  import functionalSwitch from 'https://cdn.skypack.dev/functional-switch';
</script>

README

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