# redux-binary

> Redux reducer and actions for handling binary state

Latest version **1.0.0** (published 2016-02-08) · MIT license · 0 weekly downloads

## Install

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

## 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.0 |
| Published | 2016-02-08 |
| First published | 2016-02-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Jeremy Fairbank |
| Maintainers | elpapapollo |
| Keywords | redux, binary, state |

## Links

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

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.2.1
- [invariant](https://npm.io/package/invariant.md) ^2.2.0

## 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

- 1.0.0 (latest) — 2016-02-08

## README

# redux-binary
Create simple Redux actions and a reducer for managing binary state.

## Install

    $ npm install redux-binary

## Usage

Use `reduxBinary` to create a reducer and actions for state that can take on
only two values. The default binary values are `false` and `true`, with the
initial state being set to whatever the "off" value is. In this case, that would
be the default value `false`. `reduxBinary` returns an object containing the
reducer, binary actions, and binary action type names.

The first argument is a suffix that will be used to generate the action type
names. The action type names are generated by appending `'ON_'` and `'OFF_'` to
the suffix and uppercasing the whole action type name. The returned action types
are keyed with `'ON'` and `'OFF'`.

```js
import reduxBinary from 'redux-binary';

const suffix = 'editing';
const { actionTypes } = reduxBinary(suffix, 'edit', 'stopEditing')

actionTypes.ON === 'ON_EDITING';
actionTypes.OFF === 'OFF_EDITING';
```

The returned action creators, keyed by `actions`, are named by the second and
third arguments. The second argument is the name of the "on" action creator, and
the third argument is the name of the "off" action creator.

```js
import reduxBinary from 'redux-binary';

const onActionName = 'edit';
const offActionName = 'stopEditing';

const { actionTypes, actions } = reduxBinary(
  'editing', onActionName, offActionName
);

actions[onActionName] === actions.edit;
actions[offActionName] === actions.stopEditing;

actions.edit();        // { type: actionTypes.ON };
actions.stopEditing(); // { type: actionTypes.OFF };
```

The reducer only handles the two generated actions. As you might guess, the
reducer flips the state from "off" to "on" and vice-versa. If the state is
already "on", then the "on" action does not change it. The same idea applies to
the "off" state.

```js
import reduxBinary from 'redux-binary';

const { reducer, actions } = reduxBinary(
  'editing', 'edit', 'stopEditing'
);

reducer(undefined, {}) === false; // Initial default state of false

reducer(false, actions.edit())        === true;
reducer(true,  actions.edit())        === true;
reducer(false, actions.stopEditing()) === false;
reducer(true,  actions.stopEditing()) === false;
```

You can define what the binary state is by supplying objects to the second and
third arguments as well. Pass in a single key-value object where the key is the
action creator name and the value is the state for that particular argument
number (i.e. the value inside second object argument would be the "on" state).
In this case, the initial state will be equal to whatever your "off" state is.

```js
import reduxBinary from 'redux-binary';

const onActionDef = { edit: 1 };
const offActionDef = { stopEditing: 0 };

const { reducer, actions } = reduxBinary(
  'editing', onActionDef, offActionDef
);

reducer(undefined, {}) === 0; // Initial "off" state of 0

reducer(false, actions.edit())        === 1;
reducer(true,  actions.edit())        === 1;
reducer(false, actions.stopEditing()) === 0;
reducer(true,  actions.stopEditing()) === 0;
```

If you don't want your initial state to be "off", then you can also pass in an
object for the first argument, the suffix. Similar to the object for defining
your action creators and binary state, this object must be a single key-value
object where the key is the suffix name and the value is the initial state.
**NOTE:** the initial state **must** be one of the binary state values.

Example with default state values:

```js
import reduxBinary from 'redux-binary';

const suffixDef = { editing: true };

const { reducer, actions } = reduxBinary(
  suffixDef, 'edit', 'stopEditing'
);

reducer(undefined, {}) === true; // Initial value of true
```

Example with custom state values:

```js
import reduxBinary from 'redux-binary';

const suffixDef = { editing: true };
const onActionDef = { edit: 1 };
const offActionDef = { stopEditing: 0 };

const { reducer, actions } = reduxBinary(
  suffixDef, onActionDef, offActionDef
);

reducer(undefined, {}) === 1; // Initial value of 1
```

Example with improper initial state value:

```js
import reduxBinary from 'redux-binary';

// These will throw
reduxBinary(
  { editing: 1 }, 'edit', 'stopEditing'
};

reduxBinary(
  { editing: true }, { edit: 1 }, { stopEditing: 0 }
);
```

It goes without saying, but your "on" and "off" action creator names and custom
state values can't be the same either:

```js
import reduxBinary from 'redux-binary';

// These will throw
reduxBinary('editing', 'edit', 'edit');

reduxBinary(
  'editing', { edit: 1 }, { edit: 0 }
};

reduxBinary(
  'editing', { edit: 1 }, { stopEditing: 1 }
);
```

## API

```js
type OnValue = any;
type OffValue = any;
type State = OnValue | OffValue;
type Action = { type: string };

reduxBinary(
  suffixDef: string | { [dynamic]: State },
  onActionDef: string | { [dynamic]: OnValue },
  offActionDef: string | { [dynamic]: OffValue }
): {
  reducer: (state: State, action: Action) => State;

  actionTypes: {
    ON: string;
    OFF: string
  };

  actions: {
    `ON_${dynamic}`: () => Action,
    `OFF_${dynamic}`: () => Action
  }
};
```

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