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

reducer-recipe

v1.0.0

Published

A redux reducer builder

Downloads

2

Readme

Reducer Builder

This package provides an efficient way to make complex reducers by combining a hierarchy of reducers. Using map of action types to small reducers that actually handle the give types, reducer-recipe produces more CPU-efficient (not benchmarked) reducers than the ones created by combileReducers(). On every action, combineReducers() calls all the small reducers, each with some switch-case structure without considering the fact that most of the small reducers actually doesn't handle the given action type. Moreover, this package let you make reducers in a different code-style if you like to avoid large switch-case/if-else statements.

Recipes

A recipe is a function that takes a builder (r) as argument and gives r instructions on how it should generate the desired reducer. To do so, recipe should call:

  1. r.default(state): with the default value for state.
  2. r.on(actionType, reducer(state, action)): to tell the builder how the final reducer should handle an action of given type.
import builder from 'reducer-recipe';

const Artist = (r) => {
    r.default({ name: 'Unknown', followers: 0, albums: [] })
        .on('set-artist-name', (state, action) => {
            return { ...state, name: action.name };
        })
        .on('set-artist-followers', (state, action) => {
            return { ...state, followers: action.followers };
        })
        .on('publish-new-album', (state, action) => {
            return { ...state, albums: [...state.albums, action.album] };
        });
};

const artistReducer = builder.build(Artist);

The code above generates the following reducer:

const artistReducer = (state = { name: 'Unknown', followers: 0, albums: [] }, action) => {
    switch (action.type) {
      case 'set-artist-name':
        return { ...state, name: action.name };
      case 'set-artist-followers':
        return { ...state, followers: action.followers };
      case 'publish-new-album':
        return { ...state, albums: [...state.albums, action.album] };
      default:
        return state;
    }
}

Combine recipes and reducers

To make a bigger recipe from smaller recipes you can use builder.combine() function:

const combinedRecipe = builder.combine({ Album, Artist});
const combinedReducer = builder.build(combinedRecipe);

Alternatively, you can call builder.buildCombined() to combine rececipes and build the reducer at once:

const combinedReducer = builder.buildCombined({ Album, Artist});

If for some reason, you need pure reducers that cannot be built from recipes, and you want to mix them with recipes, you can pass them as second argument to builder.combine() or builder.buildCombined():

const News = (state = [], action) => {
    switch (action.type) {
        case 'add-news':
            return [action.news, ...state];
        case 'pop-news':
            let newState = [...state];
            newState.pop();
            return newState;
        default:
            return state;
    }
};

const reducer = builder.buildCombined({ Album, Artist }, { News });

See the tests file to see a complete example.