# redux-composable-reducers

> Transducers (reducer composition) tailored to redux-style actions and state trees.

Latest version **0.1.0** (published 2020-02-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-composable-reducers
pnpm add redux-composable-reducers
yarn add redux-composable-reducers
bun add redux-composable-reducers
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2020-02-18 |
| First published | 2020-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 72.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Austin New |
| Maintainers | literalsands |
| Keywords | redux, reducer, transducer, compose, FSA, xform |

## Links

- npm: https://www.npmjs.com/package/redux-composable-reducers
- Repository: https://github.com/literalsands/redux-composable-reducers
- Homepage: https://github.com/literalsands/redux-composable-reducers#readme
- Issues: https://github.com/literalsands/redux-composable-reducers/issues
- npm.io page: https://npm.io/package/redux-composable-reducers

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2020-02-18

## README

# `redux-composable-reducers`

  _A collection of functions for composing and simplifying reducers in redux._

🚧 🚧 
Under active development.
Pull requests and discussion are very welcome.
Some functions names and signatures are likely to change.
🚧 🚧 

## Functions

into-like
`transducer(keyReducer<reducer>)(withKeyStateReducer<reducer>)`
`transducer(keyReducer<string>)(withKeyStateReducer<reducer>)`
`transducer(keyReducerObject<object>)`

branch-like
`transducer(branchObject<object>)(pathReducer<reducer>)`
`transducer(reducerReducer<reducer>)`

reduce-like
`transducer(<reducer>, ...)(reducer)`

### Action Transducers

_action_ - Reduce the action.

- _payload_ - Reduce the action payload.
- _error_ - Reduce the action error.
- _meta_ - Reduce the action meta.
- _type_ - Reduce the action type.

### Object Key Transducers

_reduceKey_ - `state => state[reduceKey(state, action)]` Reduce a key to return as the state.
_intoObject_ - Reduce the state into a key.
_intoObjectKey_ - Reduce the key into a key.
_intoArray_ - Reduce the state into a key.

### Utilities

_pass_ - `state => state` Just pass the state through.
_asReducer_ - Coerce value to a reducer.

_map_ - Map all the values of an object or array.
_reduce_ - Reduce the reducers with the initial state, and the same action.
_expand_ - Create more actions to be reduced.
_merge_ - Merge a state and a nextState.

_filter_ - Apply next reducer if a filter reduces to truthy.
_branch_ - Reduce a reducer and then reduce with that reducer.
_compose_ - Combine transducers.
_initialState_ - Initialize the state before reducing.
_dependency_ - Reduce when something on the state has changed.
_toAction_ - Set the action of a reducer to a reduced value.
_toState_ - Set the state of a reducer to a reduced value.

# Why?

- The ability leverage and re-use code, even for complex behavior.

- Simpler measures of correct-ness than the implied methodology.

## Drawbacks

- Composed functions are often less performant than well-written, inlined versions.

- Composed functions can be hard to debug.

## Gotchas

# Concepts

## Reducers

Reducers are functions that take two or fewer arguments and return a value.

```javascript
(accumulator, value) => accumulator + value;
```

Reducers can be deterministic, stable, normal, consistent, and declarative.

- Deterministic functions return the same value when given the same arguments. `f(a, b) == f(a, b)`
- Stable functions return the same value when setting their first argument to their output. `f(a, b) == f(f(a, b), b)`
- Normal functions return the same value, but for any second argument, in any order. `f(f(a, c), b) == f(f(f(a, b), c), b)`
  - In practise, this implies eventual consistency of the data and resilience to duplicate messages.
- Declarative functions never mutate the given arguments.
- Consistent functions always return a value of the same type and never return undefined.

## Transducers

Transducers are functions that take one or more reducers and return a reducer.

## Transducer Reducers

Transducer-reducers are functions that one or more reducers or transducers and return a transducer.

# Examples

## Obligatory Todos Example

```js
intoObjectKey({
  todos: compose(
    initialState([]),
    type({
      ADD_TODO: intoArray(todos => todos.length)(
        intoObjectKey({
          id: action("id"),
          text: action("text"),
          completed: false
        })
      ),
      TOGGLE_TODO: intoArray((todos, action) =>
        todos.findIndex(todo => todo.id === action.id)
      )(
        intoObjectKey({
          completed: completed => !completed
        })
      )
    })
  ),
  visibility: compose(
    initialState(VisibiltyFilters.SHOW_ALL),
    type({
      SET_VISIBILITY_FILTER: action("filter")
    })
  )
});
```

# Todo

 - [ ] More complete examples.
 - [ ] Thorough tests for every function.
 - [ ] Document through JSDoc and Typescript.
 - [ ] Convert to Typescript.
 - [ ] Optimize functions.
 - [ ] Non-reducer options through wrappers.
 - [ ] Streamline and simplify functions and settle on names for functions.

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