npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

redux-binary

v1.0.0

Published

Redux reducer and actions for handling binary state

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'.

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.

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.

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.

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:

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:

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:

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:

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

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
  }
};