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

replate

v2.0.5

Published

Utilities for reducing redux boilerplate

Downloads

55

Readme

replate

  1. What is replate?
  2. Installation
  3. Usage

1. What is replate?

Replate was designed to reduce redux boilerplate. It offers a State class that will generate action types, action creators, and reducers for you, given minimal input.

Replate is not ideal for every type of state your app may require, but it accelerates those that seem to be most common.

Example: 'Value' State

When your state is defined by a single reducer, you can use the Value state pattern with replate. Give your state a name, a default value, and a map of actions/reducer parts. Replate will generate everything else redux needs.

// CONSTRUCTOR
const valueState = new State('counter', 0, {
  set: (state, action) => action.payload,
  inc: (state, action) => state + 1,
  dec: (state, action) => state - 1
})

// ACTION TYPES
expect(valueState.actionTypes.set).toEqual('COUNTER:SET')
expect(valueState.actionTypes.inc).toEqual('COUNTER:INC')
expect(valueState.actionTypes.dec).toEqual('COUNTER:DEC')

// ACTION CREATORS
const setAction = valueState.actions.set(10)
expect(setAction).toEqual({
  type: valueState.actionTypes.set,
  payload: 10
})

const incAction = valueState.actions.inc()
expect(incAction).toEqual({
  type: valueState.actionTypes.inc
})

const decAction = valueState.actions.dec()
expect(decAction).toEqual({
  type: valueState.actionTypes.dec
})

// REDUCER
expect(valueState.reducer(undefined, {})).toEqual(0) // initial state
expect(valueState.reducer(undefined, setAction)).toEqual(10)
expect(valueState.reducer(10, incAction)).toEqual(11)
expect(valueState.reducer(11, decAction)).toEqual(10)

Example: 'Nested' State

When your state is defined by multiple nested reducers, use the 'Nested' state pattern. Nested reducers are represented by additional State instances. These nested States will respond to actions created for the original parent collection. The example below creates a Collection state, which could be reused for normalized collections of state. In fact, replate implements something very similar with the Collection class.

// CONSTRUCTOR
const collection = new State('Collection', {}, {
  byId: new State('byId', {}, {
    upsert: (state, action) => {
      action.payload._id = action.payload._id

      return {
        ...state,
        [action.payload._id]: action.payload
      }
    },
    remove: (state, action) => {
      let newState = {...state}
      delete newState[action.payload._id]
      return newState
    }
  }),
  allIds: new State('allIds', [], {
    upsert: (state, action) => {
      if(state.includes(action.payload._id))
        return state

      let newState = state.slice()
      newState.push(action.payload._id)
      return newState
    },
    remove: (state, action) => {
      let newState = state.slice();
      newState.splice(newState.indexOf(action.payload._id), 1)
      return newState
    }
  })
})

// ACTION TYPES
expect(collection.actionTypes.upsert).toEqual('COLLECTION:UPSERT')
expect(collection.actionTypes.remove).toEqual('COLLECTION:REMOVE')

// ACTION CREATORS
const upsertAction = collection.actions.upsert({_id: 1, value: 'one'})
expect(upsertAction).toEqual({
  type: collection.actionTypes.upsert,
  payload: {_id: 1, value: 'one'}
})

const removeAction = collection.actions.remove({_id: 1})
expect(removeAction).toEqual({
  type: collection.actionTypes.remove,
  payload: {_id: 1}
})

// REDUCER

// This is a little easier if we combine
const reducer = combineReducers(collection.reducer)

// undefined state and empty action returns initial state
expect(reducer(undefined, {})).toEqual({
  byId: {},
  allIds: []
})

// we're going to want this again, so lets define it here
const nextState = {
  byId: {
    1: {
      _id: 1,
      value: 'one'
    }
  },
  allIds: [1]
}

// upsert and the state includes the new
expect(reducer(undefined, upsertAction)).toEqual(nextState)

// remove the item and we're back to the initial state
expect(reducer(nextState, removeAction)).toEqual({
  byId: {},
  allIds: []
})

2. Installation

npm install replate

3. Usage

State

Given a name, initialValue, and a reducerMap, this will generate all of the boilerplate you need to start interacting with your state.

constructor

new ValueState(stateName[, initialValue, reducerMap])

| parameter | description | | --- | --- | | stateName | The name of the state. This will be converted to upper snake case and prepended to any action type names | | initialValue | The default state value | | reducerMap | Can follow either the 'Value' or 'Nested' state pattern shown in the examples above. |

properties

| property | description | | --- | --- | | actionTypes | A map of action types. The key is derived from the reducerMap passed into the constructor. The value is a concatenation of the state name and action name. See Action Type Naming for more details. | | actions | A map of action names and methods. The method will return an object with the structure {type, payload} where payload is whatever you pass into the method. | | reducer | If you used the 'Value' pattern, a reducer method with signature reducer(state, action). If you used the nested pattern, a map of named reducers. |

Collection

At forma, we found the normalized state collection to be a fairly common pattern, so we implemented this out of the box. In fact, this was the main motivation for making this library. Instead of implementing the Nested State collection from the example above, just instantiate a Collection. This provides you with a basic normalized state collection including byId and allIds

const collection = new Collection('screens')

expect(collection.actionTypes.upsert).toEqual('SCREENS:UPSERT')
expect(collection.actionTypes.remove).toEqual('SCREENS:REMOVE')

// ...etc
constructor

new ValueState(stateName[, reducerMap])

| parameter | description | | --- | --- | | stateName | The name of the state. This will be converted to upper snake case and prepended to any action type names | | reducerMap | Nested state in addition to byId and allIds |

Action Type Naming

Action types concatenate the State name with an action name. Both are converted to upper snake case.

e.g.

const state = new State('camelCaseName', {}, {
  action1: () => ({})
})

state.actionTypes.action1 // CAMEL_CASE_NAME:ACTION_1