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

@studiohyperdrive/redux-utils

v1.1.0

Published

Helper classes and functions to use with Redux.

Downloads

80

Readme

Redux Utils

The Redux Utils package provides some helpers and bootstrap utils to ease the redux setup.

Installation

Install the package via npm:

npm i -S @studiohyperdrive/redux-utils

and import helpers where needed:

import { progressReducer } from '@studiohyperdrive/redux-utils';

Higher order reducers (HORs)

There are 3 HORs available: the progress, basicType and target HOR. The usage of combineReducers is required for these HORs to work.

progress

The progress HOR wraps the state in an object detailing the status of your data:

{
    loading: true/false,
    created: 'Thu Feb 15 2018 13:36:31 GMT+0100 (CET)',
    lastUpdated: 'Thu Feb 15 2018 13:36:31 GMT+0100 (CET)',
    err: 'Custom error',
    result: {
        id: 'my-item'
    }
}

You can update the status by providing the loading flag or an err message in your action:

{
    type: 'DO_STUFF',
    loading: true/false,
    err: 'Something went wrong'
}

By wrapping your reducer, you maintain control over what is stored in the result, without having to bother with the loading state. You just have to provide a type and a reducer function:

const myReducer = (state, action) => {
    if (action.type === 'STUFF_LOAD') {
        return {
            ...state,
            stuff: action.stuff
        };
    }

    return state;
};

const progressReducer = progress('STUFF', myReducer);

The progressReducer will only trigger for actions starting with the type you provided. We use namespacing by trailing slash /:

type = 'ARTICLES';

'ARTICLES/LOAD' will match
'ARTICLES/LOAD' will match
'ARTICLES/PAGE' will match
'NEWS_ARTICLES' will not match
'ARTICLES_LOAD' will not match
'ARTICLE/LOAD' will not match

basicType

In a lot of applications there are similar data types that require a minimum of functionality:

  • load an item
  • load multiple items
  • append new items to the stored items
  • clear an item/items

To avoid a lot of copy pasta the redux-utils package provides a very basic HOR to handle this use case.

Simply provide a type (defaults to BASIC_DEFAULT) and an (optional) initial value:

const newsReducer = basicType({
    type: 'NEWS'
}, null);

Now you can update the news type by dispatching actions using the type you provided as a namespace:

dispatch({
    type: 'NEWS/LOAD',              // <TYPE>/LOAD
    data: {                         // will always look for a 'data' property
        id: '1',
        title: 'How cool is this?'
    }
});

Data types

By default the basicType HOR will assume you are handling a single item. If you need to handle arrays, set the dataType option to list:

const newsReducer = basicType({
    type: 'NEWS',
    dataType: 'list',
}, []);

Now you can use the same LOAD action to set the state:

dispatch({
    type: 'NEWS/LOAD',
    data: [{
        id: '1',
        title: 'How cool is this?'
    }]
});

or use the LOAD_MORE action to append new items to the state:

dispatch({
    type: 'NEWS/LOAD_MORE',
    data: [{
        id: '2',
        title: 'Need more content.'
    }]
});

Note: When you call the LOAD_MORE action on a single data type, the reducer will fallback to the LOAD action, overwriting the state.

Progress

You can use the progress HOR by setting progress to true in the settings:

const newsReducer = basicType({
    type: 'NEWS',
    dataType: 'list',
    progress: true,
}, []);

You can still dispatch loading and err states the same way:

dispatch({
    type: 'NEWS/LOAD',
    loading: true,
});

target

The target reducer stores the result of the provided reducer for the provided target in the store. This way, you can reuse reducer logic and without having to manage an extra layer of complexity.

{
    filters: {
        search: {...},
        home: {...},
        users: {...}
    }
}

The targetReducer expects a type, reducer and optional initialState to work:

const filterReducer = targetReducer({ type: 'FILTERS' }, myFilterReducer, {});

The type will be the namespace verified with the action type.

Updating a target

You can update targets by setting the target property on the dispatched action:

dispatch({
    type: 'FILTERS/LOAD',
    target: 'search',
    filters: [...],
});

The state will be updated accordingly:

{
    filters: {
        search: [...]
    }
}

Progress

You can wrap your targets in a progressReducer by setting progress to true in the settings:

const filterReducer = targetReducer({
    type: 'FILTERS',
    progress: true
}, myFilterReducer, {});

Now you can dispatch loading and err states the same as before:

dispatch({
    type: 'FILTERS/LOAD',
    loading: true,
    target: "search"
});

The state will be updated accordingly:

{
    filters: {
        search: {
            loading: true,
            err: null,
            result: null,
            ...
        }
    }
}