# @deppi/reducer

> Helper package for creating a reducer for Flux-like stores using Kmett-style lenses

Latest version **0.0.0-development.5** (published 2018-06-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @deppi/reducer
pnpm add @deppi/reducer
yarn add @deppi/reducer
bun add @deppi/reducer
```

## 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.0.0-development.5 |
| Published | 2018-06-29 |
| First published | 2018-06-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 128.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Tim Roberts |
| Maintainers | beardedtim |
| Keywords | flux-actions, reducer, functional-state, immutable-store, redux, flux, @deppi, lens, functional-lens |

## Links

- npm: https://www.npmjs.com/package/@deppi/reducer
- Repository: https://github.com/beardedtim/deppi-reducer
- Homepage: https://github.com/beardedtim/deppi-reducer#readme
- Issues: https://github.com/beardedtim/deppi-reducer/issues
- npm.io page: https://npm.io/package/@deppi/reducer

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 0.0.0-development.5 (latest) — 2018-06-29
- 0.0.0-development.4 — 2018-06-29
- 0.0.0-development.3 — 2018-06-29
- 0.0.0-development.2 — 2018-06-29
- 0.0.0-development.1 — 2018-06-29
- 0.0.0-development.0 — 2018-06-29

## README

# @deppi/reducer

> Handling updating state, the functional way

## Usage

```ts
// import functions
import { setStateOn, createReducer } from '@deppi/reducer'

// Declare some types, if that's your thing
interface State {
  name: string;
}

// We define some action that will
// trigger the updates
interface UpdateUserAction extends Action {
  payload: {
    name: string;
  }
}

// Have some initial state
const data: State = {
  name: 'tim'
}
// and a lens to the part we are updating
const nameLens = lensProp('name')

// We create an updater
const setUserName = setStateOn<State, string>(nameLens)

const reducer = createReducer({
  // and when the action UPDATE_USER_NAME
  // goes through our reducer
  ['UPDATE_USER_NAME']: [
    // we want to update the user's name as John
    setUserName.as('John'),
    // will, again, update the user's name to John
    setUserName.using<UpdateUserAction>((action, state) => action.payload.name),
    // This will set the name to JOHN since these
    // are ran in sequence and passed the next
    // state value
    setUserName.with<UpdateUserAction>(
      () => oldName => oldName.toUpperCase()
    )
  ]
})

// At some point we get given a new action
// that matches our ActionHandlers
// and we pass it the previous
// state
console.log(reducer(data, {
  type: 'UPDATE_USER_NAME',
  payload: {
    name: 'John'
  }
}))
// { name: 'JOHN' }
```

## Scripts

* _`yarn example`_: Runs the `example.js` file
* _`yarn test`_: Run test suites
  - `yarn test --watch`: Run test suites in watch mode
  - `yarn test --coverage`: Run test suites and collect coverage
* _`yarn build:prod`_: Builds the Typescript for production
* _`yarn clean`_: Removes the `./dist/` folder
* _`yarn build`_: Builds a new `./dist` folder after removing any previous one
* _`yarn prepublishOnly`_: _FOR NPM USE ONLY_ Causes the package to be built _right before_ being published

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