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

@phenax/redux-utils

v0.0.6

Published

Utility functions and patterns to work with redux

Downloads

1

Readme

Redux Utils

Utility functions and patterns to work with redux and reduce some of the boilerplate involved

CircleCI npm bundle size (minified + gzip) Codecov

Read the documentation for more information

Install

To add the project to your project

yarn add @phenax/redux-utils

Motivation

The patterns and utility functions this library exposes are just a collection of solutions to problems that I've faced while working with redux. The most common one being the frequently repeated convention of using the three states, _REQUEST/_PENDING, _SUCCESS and _FAILURE for most, if not all of the actions being dispatched. So I've compiled here a few of the utility functions that I created in my personal projects.

Usage

Import it to your file

import { actionTypes, THREE_STATE_ACTION, createPartialReducer, mergeReducers } from '@phenax/redux-utils';

Create resource based action types

actionTypes function works on a simple convention of @resource/ACTION/STATE. Borrowed it from the REST world.

THREE_STATE_ACTION is just an array of the 3 action states ['PENDING', 'SUCCESS', 'FAILURE']

import { actionTypes, THREE_STATE_ACTION } from '@phenax/redux-utils';

const types = actionTypes({
  USERS: {
    ADD: THREE_STATE_ACTION,
    LIST: THREE_STATE_ACTION,
  },
});

Your dispatches will look something like this

dispatch({ type: types.USERS.ADD.SUCCESS, payload: {/* stuff */} });

Create a partial reducer

createPartialReducer function can be used to create reducers that only act on actions on that resource. For example a dispatch of type types.USERS.LIST.SUCCESS will not affect anything inside a partial reducer for types.USERS.ADD. Also the reason for choosing an object over switch case statements is that it will promote using smaller functions inside your reducers for a more composable.

import { createPartialReducer } from '@phenax/redux-utils';

const initialState = { loading: true, users: [], error: '' };

const addUserReducer = createPartialReducer(types.USERS.ADD, (state = initialState, action) => ({
  PENDING: () => ({ ...state, loading: true }),
  SUCCESS: newUser => ({
    users: [...state.users, newUser],
  }),
  FAILURE: e => ({ ...state, error: e.message }),
}));

Merge your partial reducers togather

mergeReducers function will allow you to merge/compose all the partial action reducers that you have created into one resource reducer. It will merge your reducers from LEFT-to-RIGHT but that shouldn't matter as any dispatch should only affect one partial reducer.

Note: Some may find this pattern very restrictive and that was the intention but mergeReducers is a generic function so the argument being passed doesn't have to be a partial reducer. So you can add a reducer with the good old switch-case statement in there.

import { mergeReducers } from '@phenax/redux-utils';

const userReducer = mergeReducers(addUserReducer, listUserReducer);

Usage with redux-saga and crocks

import { callAsync, putResponse } from '@phenax/redux-utils/saga';

// fetchUsers :: Params -> Async [User]

function* listUsersSaga({ payload: { params } }) {
  yield put(({ type: types.USERS.LIST.SUCCESS, payload: data }));

  const response = yield callAsync(fetchUsers, params);

  yield putResponse(types.USERS.LIST, response);
}