Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fetchte/switchreducer

A small but useful redux switch case helper
https://github.com/fetchte/switchreducer

helper react reducer redux

Last synced: about 1 month ago
JSON representation

A small but useful redux switch case helper

Awesome Lists containing this project

README

        

# switchreducerr
[![npm](https://img.shields.io/npm/l/switchreducer.svg)](https://github.com/artisin/switchreducer/blob/master/LICENSE.txt)
[![npm](https://img.shields.io/npm/v/switchreducer.svg)](https://www.npmjs.com/package/switchreducer)
[![wercker status](https://app.wercker.com/status/e388b87aecbfc99fb4b0abb9c16a602c/s/master "wercker status")](https://app.wercker.com/project/byKey/e388b87aecbfc99fb4b0abb9c16a602c)
[![David](https://img.shields.io/david/artisin/switchreducer.svg)](https://github.com/artisin/switchreducer/blob/master/package.json)

## Description

A simple and small [Redux](http://redux.js.org/) library designed to reduce the verbose boiler plate of the "default" [switch statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) [reducer](http://redux.js.org/docs/Glossary.html#reducer) pattern.

## `switchreducer`

The operation of `switchreducer` is dead simple. It takes two arguments, the initial state, and a function that's passed a single argument object parameter of the `state` and the `action` properties, ie `({state, ...action})`. This function expects a return of a lookup table composed of "action" cases which are invoked when a "action" type matches.

```js
switchreducer(, ({, ...}) =>

);
```

__Example__

```js
import switchreducer from 'switchreducer';

// actions + state
const SET_FILTER = 'SET_FILTER';
const SET_COLOR = 'SET_COLOR';
const initialState = {};

// reducer
const myReducer = switchreducer(initialState, ({state, payload}) => ({
[SET_FILTER]: () => Object.assign({}, state, {filter: payload}),
[SET_COLOR]: () => Object.assign({}, state, {color: payload}),
}));
```

__"default" reducer comparison__

For comparison here's what the above reducer would look like using the "default" switch statement pattern.

```js
// actions + state
const SET_FILTER = 'SET_FILTER';
const SET_COLOR = 'SET_COLOR';
const initialState = {};

// reducer
const myReducer = (state = initialState, action) => {
const { type, payload } = action;
switch (type) {
case SET_FILTER:
return Object.assign({}, state, {filter: payload});
case SET_COLOR:
return Object.assign({}, state, {color: payload}),
default:
return state;
}
};
```

## `switchcase`

You may prefer and/or there may be cases that the `switchcase` lookup table helper is a better fit. The `switchcase` function is the "real" logic behind `switchreducer`. It expects a "action" lookup table and two curried arguments the state and the "action" type.

```js
switchcase()()();
```

__Example__

```js
import { switchcase } from 'switchreducer';

// actions + state
const SET_FILTER = 'SET_FILTER';
const SET_COLOR = 'SET_COLOR';
const initialState = {};

// reducer
const myReducer = (state = initialState, action) => {
const { type, payload } = action;
return switchcase(({
[SET_FILTER]: () => Object.assign({}, state, {filter: payload}),
[SET_COLOR]: () => Object.assign({}, state, {color: payload}),
}))(state)(type);
};
```

---

Best, te