# redux-reduce-with

> Write reducers as objects

Latest version **0.1.2** (published 2018-03-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-reduce-with
pnpm add redux-reduce-with
yarn add redux-reduce-with
bun add redux-reduce-with
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2018-03-14 |
| First published | 2018-03-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 212.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Gian Marco Toso |
| Maintainers | gianmarcotoso |

## Links

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

## Recent versions

- 0.1.2 (latest) — 2018-03-14
- 0.1.1 — 2018-03-14
- 0.1.0 — 2018-03-14

## README

[![Build Status](https://travis-ci.org/gianmarcotoso/redux-reduce-with.svg?branch=master)](https://travis-ci.org/gianmarcotoso/redux-reduce-with)
[![npm version](https://badge.fury.io/js/redux-reduce-with.svg)](https://badge.fury.io/js/redux-reduce-with)

# redux-reduce-with

A small library to write Redux reducers as objects instead of using switch statements.

## Why?

I don't like switch statements!

## Installation

`npm install redux-reduce-with --save`

## How does it work?

To write a reducer with `redux-reduce-with` you simply define an object where the keys are your
action types and the values for those keys are _mutators_; these can be either functions or objects.

A _mutator function_ is a **pure** function that receives the current `state` and the current
`action` and returns the new value for the state.

A _mutator object_ is an object where each key is a key within the state that you want to mutate,
and the value for that key is a function which receives the current `action` and, as a second
parameter, the `state`. The value for the key can also be an immediate value if you don't need to
compute anything.

A simple example:

```
// We import the library
import { reduceWith } from 'redux-reduce-with'

// We import our action types
import {
    LOADING_START,
    LOADING_STOP,
    LOADING_CANCEL
} from './ActionTypes'

// We define our mutators object
const mutators = {
    // We can mutate the whole state with one function,
    // as long as we return the whole state
    [LOADING_START]: (state, action) => {
        return {
            ...state,
            isLoading: true,
            loading: state.loading + 1
        }
    },

    // We can also mutate certain keys, setting them directly
    [LOADING_STOP]: {
        loading: (action, state) => state.loading - 1,
        isLoading: (action, state) => (state.loading - 1) === 0 ? false : true
    },

    // We can also directly set an immediate value for a
    // specific key, or more than one of them
    [LOADING_CANCEL]: {
        loading: 0,
        isLoading: false
    }
}

// We wrap our mutators object, along with a default state, with reduceWith and export it.
// The exported value will be a regular reducer function!
export default reduceWith(mutators, { loading: 0, isLoading: false })
```

## License

MIT

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