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 🙏

© 2024 – Pkg Stats / Ryan Hefner

type-to-reducer

v1.2.0

Published

Create reducer functions based on an object keyed by action types

Downloads

6,640

Readme

Circle CI

type-to-reducer

Greenkeeper badge

This module provides a function typeToReducer, which accepts an object (reducerMap) and returns a reducer function composing other reducer functions described by the reducerMap.

Why?

This is pretty much the same as the handleActions function you can find in https://github.com/acdlite/redux-actions. The differences being, type-to-reducer only exposes the function as a default, and allows nesting of the reducerMap object.

Usage

npm install type-to-reducer --save

The reducerMap you supply to the function will have keys that correspond to dispatched action types and the values for those keys will be reducer functions. When the returned reducer function is called with state and an action object, the action's type will be matched against the previously provided reducerMap keys, if a match is found, the key's value (the reducer function) will be invoked with the store's state and the action.

Also, you can describe an initial state with the second argument to typeToReducer.

Oh and you can also set reducerMaps as the values too, these objects will be nested instances of the same shaped object you supplied to typeToReducer.

If that sounded a bit complicated, the example below should make it clearer. NB, it helps if you're familiar with redux.

import typeToReducer from 'type-to-reducer'
import { GET, UPDATE } from 'app/actions/foo'

const initialState = {
  data: null,
  isPending: false,
  error: false
}

// supply the reducerMap object
export const myReducer = typeToReducer({
  // e.g. GET === 'SOME_ACTION_TYPE_STRING_FOR_GET'
  [GET]: (state, action) => ({
    ...state,
    data: action.payload
  }),
  [UPDATE]: (state, action) => ({
    ...state,
    data: action.payload
  })
}, initialState)

Then the myReducer would be used like so:

let state = { foo: 'bar' }

let newState = myReducer(state, {
  type: GET,
  payload: `an action's payload.`
})

// newState will deep equal { foo: 'bar', data: `an action's payload.` }

Group reducers by a prefix when objects are nested.

import typeToReducer from 'type-to-reducer'
import { API_FETCH } from 'app/actions/bar'

const initialState = {
  data: null,
  isPending: false,
  error: false
}

export const myReducer = typeToReducer({
  [ API_FETCH ]: {
    PENDING: () => ({
      ...initialState,
      isPending: true
    }),
    REJECTED: (state, action) => ({
      ...initialState,
      error: action.payload,
    }),
    FULFILLED: (state, action) => ({
      ...initialState,
      data: action.payload
    })
  }
}, initialState)

// usage
let previousState = Whatever;

let newState = myReducer(previousState, {
  type: API_FETCH + '_' + PENDING,
})

// newState shallow deeply equals { isPending: true }

Custom Type Delimiter

You can add a custom type delimiter instead of the default '_'.

This will set it for every reducer you create after the custom delimiter is set, yes a dirty stateful function. This is for convenience so you can set it for your whole project up front and not to pollute the main function abstraction for rare settings.

import typeToReducer, {setTypeDelimiter} from 'type-to-reducer'
import { API_FETCH } from 'app/actions/bar'

const initialState = {
  data: null,
  isPending: false,
}

setTypeDelimiter('@_@')

export const myReducer = typeToReducer({
  [ API_FETCH ]: {
    PENDING: () => ({
      ...initialState,
      isPending: true
    }),
  }
}, initialState)

// Then use the delimiter in your action type
myReducer(someState, {
  type: API_FETCH + '@_@' + PENDING,
})