functional-switch
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "functional-switch", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A functional implementation of switch.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,35 @@ # Functional Switch | ||
It just an idea right now. Discussion is here: https://gist.github.com/wuct/19e85b98cc7fe12a7b0a | ||
It just an idea right now and I have not written any code, yet. Discussions are welcome! | ||
## Installation | ||
`npm install functional-switch --save` | ||
## Usage | ||
```js | ||
import { cond, when, otherwise } from 'functional-switch' | ||
// simple example | ||
const isFruitOrVegetable = cond( | ||
when('apple', 'isFruit'), | ||
when('orange', 'isFruit'), | ||
when('eggplant', 'isVegetable'), | ||
otherwise('isFruit') | ||
) | ||
isFruitOrVegetable('apple') // isFruit | ||
``` | ||
`when` is curried | ||
```js | ||
when('apple')('isFruit') | ||
``` | ||
`when` also accepts a function or a regular expression | ||
```js | ||
when(/apple/) | ||
when(str => str.includes('apple')) | ||
``` |
2862
39