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

general-reducer

v1.1.0

Published

General purpose reducer generator

Downloads

3

Readme

ci codecov downloads node npm MIT npm bundle size Conventional Commits

general-reducer

General purpose reducer generator

Description

general-reducer provides utilities to generate universal reducer with corresponding actions.

Installation

As any other npm package general-reducer can be added to your project by following command:

npm i -S general-reducer

API

Universal reducer

general-reducer package provides general purpose reducer to perform most common operations on store. You just need to configure your store with it.

  • with react hook:

    import React, { useReducer } from 'react';
    import { reducer } from 'general-reducer';
    
    const Component = () => {
        const [ value, dispatch ] = useReducer(reducer);
    
        ...
    }
  • with redux:

    import { createStore } from 'redux'
    import { reducer } from 'general-reducer';
    
    const store = createStore(reducer);
  • with react-easy-flux:

    import { createStorage } from 'react-easy-flux';
    import { reducer } from 'general-reducer';
    
    const {
        Provider,
        useStorage,
        useActions
    } = createStorage(reducer);

Since general-reducer uses immutable-object-update under the hood, it performs immutable state update and returns frozen object.

Built-in actions

general-reducer exposes ACTIONS map object with actions for most common operations with state:

Most of actions consumes at least 1 argument - path to updated element, it might be an array of items or dot-separated string. When action dispatched reducer will apply corresponding operation from immutable-state-update package:

import { reducer, ACTIONS } from 'general-reducer';

const state = {
    a: {
        a1: 1,
        a2: 2
    },
    b: {
        b1: 3,
        b2: 4
    }
};

const updated = reducer(state, ACTIONS.set('b.b1', 5));

// or

const updated = reducer(state, ACTIONS.set([ 'b', 'b1' ], 5));

As a result we will receive new object with structure below:

{
    a: {
        a1: 1,
        a2: 2
    },
    b: {
        b1: 3,
        b2: 5
    }
}

Action combination

Since general-reducer uses under the hook, we're able to use composite action to combine several simple actions and possibly improve performance a bit:

const trim = (path, n) => ACTIONS.all(
    ACTIONS.shift(path, n)
    ACTIONS.pop(path, n)
);

const updated = reducer(state, trim('a.b', 2));

Custom actions

It is possible to add custom actions to perform some complex updates. To so createGeneralReducer() function is exposed. It consumes map object with custom action update functions. Each update function will consume part of a state as an argument and should return updated value.

const {
    reducer,
    ACTIONS
} = createGeneralReducer({
    selectAll: items => items.map(
        item => ({ ...item, selected: true })
    ),
    unselectAll: items => items.map(
        item => ({ ...item, selected: false })
    ),
    removeSelected: items => items.filter(
        ({ selected }) => !selected
    )
});

Returned actions will consume path to state piece to be updated.

const state = {
    items: [
        { selected: true, text: 'Steal pants' },
        { selected: false, text: '?????' },
        { selected: false, text: 'Profit!' }
    ]
};

const updated = reducer(state, ACTIONS.removeSelected('items'));

/*
{
    items: [
        { selected: false, text: '?????' },
        { selected: false, text: 'Profit!' }
    ]
};
*/

If you need any additional arguments in your update function just pass those into action creator after path:

const {
    reducer,
    ACTIONS
} = createGeneralReducer({
    add: (value, diff) => value + diff
});

const state = {
    a: 10
};

const updated = reducer(state, ACTIONS.add('a', 5));

/*
{
    a: 15
}
*/

Actions namespace

By default general-reducer uses 'general' as a namespace for all actins, it means two different general reducers will have the same action types:

const { TYPES } = createGeneralReducer({
    customCase: (...) => ...
});

/*
TYPES = {
    insert: 'general.insert',
    insertAll: 'general.insertAll',
    pop: 'general.pop',
    ...
    customCase: 'general.customCase'
}
*/

Because of that it can be problematic to combine general reducers. To generate unique action types just provide different namespace as 2nd argument of createGeneralReducer() function:

const { TYPES } = createGeneralReducer({
    customCase: (...) => ...
}, 'customNamespace');

/*
TYPES = {
    insert: 'customNamespace.insert',
    insertAll: 'customNamespace.insertAll',
    pop: 'customNamespace.pop',
    ...
    customCase: 'customNamespace.customCase'
}
*/

Definitely, in most cases to extend general reducer it's better to provide custom actions or include general reducer into custom one:

function customReducer(state, action) {
    switch (action.type) {
        ...
        default:
            return generalReducer(state, action);
    }
}