# redux-pipe

> redux-pipe -----

Latest version **1.0.13** (published 2017-09-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-pipe
pnpm add redux-pipe
yarn add redux-pipe
bun add redux-pipe
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.13 |
| Published | 2017-09-07 |
| First published | 2017-05-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | giftofjehovah |

## Links

- npm: https://www.npmjs.com/package/redux-pipe
- npm.io page: https://npm.io/package/redux-pipe

## Dependencies (5)

- [webpack](https://npm.io/package/webpack.md) ^2.6.1
- [immutable](https://npm.io/package/immutable.md) 4.0.0-rc.2
- [babel-jest](https://npm.io/package/babel-jest.md) ^20.0.3
- [babel-loader](https://npm.io/package/babel-loader.md) ^7.0.0
- [babel-preset-es2015](https://npm.io/package/babel-preset-es2015.md) ^6.24.1

## Recent versions

- 1.0.13 (latest) — 2017-09-07
- 1.0.12 — 2017-08-28
- 1.0.11 — 2017-08-28
- 1.0.10 — 2017-08-28
- 1.0.9 — 2017-08-28
- 1.0.8 — 2017-06-03
- 1.0.7 — 2017-06-03
- 1.0.6 — 2017-06-03
- 1.0.5 — 2017-06-03
- 1.0.4 — 2017-06-03
- 1.0.3 — 2017-06-03
- 1.0.2 — 2017-06-03
- 1.0.1 — 2017-06-03
- 1.0.0 — 2017-05-23

## README

redux-pipe
-----

redux-pipe is a simple utility function to enable you to write more declarative code in your redux reducer by allowing logic to be abstracted and reuse if necessary. 

```
npm install redux-pipe
```
or
```
yarn add redux-pipe
```

## How to use redux-pipe?


### `pipe()`
The `pipe` function take in an array of functions called **mutators** and the current state to create the next state.

```js
pipe(mutators: Array<Function>, currentState): nextState
```
Mutator are functions that take in a redux action that returns another function that accept the state as its parameter. The last function will compute the next state and return it for the next mutator.
```js
// mutators
const stopLoader = () => state => ({...state, isLoading: false})
const setData = action => state => ({...state, data: action.payload.data})
```
This is how we can use them in our reducer. The following example is how you can stop loading spinner and set the data to the state with one action.

```js
//reducer
import {pipe} from 'redux-pipe'

export default function rootReducer (state, action) {
  switch (action.type) {
  	case 'GET_DATA_SUCCESS':
      return pipe([stopLoading(), setData(action)], state)
  }
}
```
## Additonal helper functions
### `branchIf()`
The `branchIf` function take in a predicate as its first parameter. 
It will return the mutators in second parameter if the predicate returns true.
It will return the mutators in third parameter if the predicate returns false.
```js
branchIf(predicate: Function, trueMutator: Function | trueMutators:Array<Function>, falseMutator: Function | falseMutators:Array<Function>)
```
This is how we can use them in our reducer. The following example is how you can stop loading a spinner and use the mutator `setToy` or `setMakeup` according to what our predicate returns.
```js
// predicate
const isMale = action => state => action.payload.gender === 'M'

// mutators
const stopLoader = () => state => ({...state, isLoading: false})
const setToy = action => state => ({...state, toy: action.payload.item})
const setMakeup = action => state => ({...state, makeup: action.payload.item})

// reducer
import {pipe, branchIf} from 'redux-pipe'

export default function rootReducer (state, action) {
  switch (action.type) {
    case 'SET_ITEM':
      return pipe([
        stopLoader(),
        branchIf(isMale(action), setToy(action), setMakeup(action))
      ], state)
  }
}
```

---
_Source: https://npm.io/package/redux-pipe · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
