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

v2.3.0

Published

Flux boilerplate reducer (reducer & action generator)

Downloads

4

Readme

ci codecov downloads node npm MIT npm bundle size

Intro

reducer-generator package can help for flux/redux developers to reduce boilerplate when creating actions, action creators and reducers. It can automate part of work and generate actions and action creators by itself.

Demo

Example of simple todo app can be found here.

API

generateReducer(reducerCaseMap, namespace, additionalActions)

reducer-generator has only default export - generateReducer() function. It expects an object as only required argument. Action type and action creator function will be created for each key of this object. Each value should describe reducer case to react on specific action. Reducer case function should receive two arguments: current state and action payload.

// simpleCounter.js

import generateReducer from 'reducer-generator';

const counter = generateReducer({
    increment: ({ counterValue, ...rest }) => ({ counterValue: counterValue + 1, ...rest }),
    decrement: ({ counterValue, ...rest }) => ({ counterValue: counterValue - 1, ...rest }),
    reset: state => ({ ...state, counterValue: 0 })
});

After call above generateReducer() function will return equivalen for the following object:

{
    TYPES: {
        increment: 'increment-<unique-increment-id>',
        decrement: 'decrement-<unique-decrement-id>',
        reset: 'reset-<unique-reset-id>'
    },
    ACTIONS: {
        increment: payload => ({ type: 'increment-<unique-increment-id>', payload }),
        decrement: payload => ({ type: 'decrement-<unique-decrement-id>', payload }),
        reset: payload => ({ type: 'reset-<unique-reset-id>', payload })
    },
    reducer: (state, { type, payload }) => {
        switch (type) {
            case 'increment-<unique-increment-id>':
                return { ...state, counterValue: state.counterValue + 1 };
            case 'decrement-<unique-decrement-id>':
                return { ...state, counterValue: counterValue - 1 };
            case 'reset-<unique-reset-id>':
                return { ...state, counterValue: 0 };
            default:
                return state;
        }
    }
}

Action types

By default unique action types will be created using uuid package. Here are some examples of unique action types:

const TYPES = {
    increment: "increment-27b7068b-683d-5126-bf3b-914377e27023"
    decrement: "decrement-5fce52ab-ab1c-54bf-944b-3a0e6d868b83"
    reset: "reset-8ad2e79f-fb96-5dee-b563-eb56b854fe19"
}

In some cases we might need constant values for action types. For instance, when we want to save change history and be able to undo/redo in different session. In this case instead of uuid we can use namespace by passing it as a 2nd argument of generateReducer() function:

const { TYPES } = generateReducer({
    increment: () => { ... },
    decrement: () => { ... },
    reset: () => { ... }
}, 'counter');

Namespace will be prefixed to each action type const:

const TYPES = {
    increment: 'counter.increment',
    decrement: 'counter.decrement',
    reset: 'counter.reset'
}

Additional actions

We also might want to have some synthetic actions. For instance to use with takeLatest() from redux-saga. To create additional action creators and action types we can pass array of key for those as 3rd argument:

const { TYPES } = generateReducer({
    increment: () => { ... },
    decrement: () => { ... },
    reset: () => { ... }
}, 'counter', [ 'resetAsync' ]);

It will add resetAsync action type and action creator without changes to reducer:

const TYPES = {
    ...
    resetAsync: 'counter.resetAsync'
}

Combining actions

reducer-generator provides a simple feature to combine actions. It may help to improve performance in some cases. For instance, when several state updates called synchronously and we're not using any other tool to fix this issue.

const { ACTIONS, reducer } = generateReducer(cases);

const updated = reducer(state, ACTIONS.all(
    ACTIONS.reset(),
    ACTIONS.increment(),
    ACTIONS.increment()
));

Code above performs 3 changes but calls reducer() only once. It means there will be only one update and even synchronous update will cause only one render.