Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ajoslin/switch-fn
Write a functional switch statement.
https://github.com/ajoslin/switch-fn
Last synced: 17 days ago
JSON representation
Write a functional switch statement.
- Host: GitHub
- URL: https://github.com/ajoslin/switch-fn
- Owner: ajoslin
- License: mit
- Created: 2016-02-14T21:52:03.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-25T14:45:25.000Z (over 7 years ago)
- Last Synced: 2024-10-18T21:12:06.159Z (26 days ago)
- Language: JavaScript
- Homepage:
- Size: 6.84 KB
- Stars: 42
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# switch-fn [![Build Status](https://travis-ci.org/ajoslin/switch-fn.svg?branch=master)](https://travis-ci.org/ajoslin/switch-fn)
> Write a functional switch statement.
## Install
```
$ npm install --save switch-fn
```## Usage
```js
var Switch = require('switch-fn')var fn = Switch({
a: actionA,
b: actionB,
default: defaultAction
})var result = fn('a') // => calls actionA with 'a' and gives back actionA's return value
```## API
#### `Switch(cases)` -> `function`
##### cases
*Required*
Type: `object`An object, with keys being the 'cases' to match against and values being the function to call in each case.
If no case matching the input is found and a 'default' case is given, it will be used.
# More Examples
#### With a "property getter" for more complicated objects
```js
const Switch = require('switch-fn')
const pipe = require('value-pipe')const mySwitch = pipe(
value => value.username.toLowerCase(),
Switch({
alice: () => 'hello, alice',
john: () => 'hello, john'
})
)mySwitch({ username: 'Alice' }) // => 'hello, alice'
```#### Pass in an array for numbers only
```js
var Switch = require('switch-fn')var fn = Switch([onZero, onOne])
fn(0)
```#### Fibonacci
```es6
var fib = Switch({
0: (n) => n,
1: (n) => n,
default: (n) => fib(n - 1) + fib(n - 2)
});fib(10) // => 55
```## License
MIT © [Andrew Joslin](http://ajoslin.com)