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

@autotelic/redux-core-reducers

v0.2.0

Published

A collection of re-usable and configurable reducers for redux applications

Downloads

17

Readme

redux-core-reducers

A collection of re-usable and configurable reducers for redux applications.

This package exports a number of helper methods for creating simple redux reducers. It is quite a small library so you may find it helpful to read the source code to understand exactly what is happening under the hood.

Commitizen friendly

Installation

yarn add @autotelic/redux-core-reducers

Usage

createStandardReducer

Is a helper for constructing functions that are used to create reducers.

It accepts a state handler function with the signature:

const stateHandler = (state, payload, meta, additionalArgs) => { /* return the modified state */ };
  • state: Reducer state
  • payload: Action payload
  • meta: Action meta
  • AdditionalArgs: Other arguments needed by the reducer

createStandardReducer when called with a stateHandler function, returns the following function that is used to create the other reducers. This method is used to construct the other createReducer functions in this library.

function initialize(
  typeOrTypes,
  initialState = Immutable({}),
  additionalArgs,
) {
  const types = Array.isArray(typeOrTypes)
    ? typeOrTypes
    : [typeOrTypes];
  return function standardReducer(state = initialState, action) {
    const {
      type,
      payload,
      meta,
    } = action;

    if (types.includes(type)) {
      if (payload !== undefined) {
        // This is the state handler passed to createStandardReducer.
        return stateHandler(state, payload, meta, additionalArgs);
      }
      return initialState;
    }
    return state;
  };
}

Example Usage

import { createStandardReducer } from '@autotelic/redux-core-reducers';

const defaultState = {};

const standardReducer = createStandardReducer(
  (state, payload) => payload,
)(
  'SOME_ACTION',
  defaultState,
);

Core Reducers

These reducers use seamless-immutable methods in the stateHandlers for the below create reducer helpers.

Each of:

  • createNameValueReducer
  • createMergePayloadReducer
  • createReplacePayloadReducer

are used in the same way, but perform different operations on the state of the reducer.

The first argument is an action type, or an array of action types that the reducer should operate on.

The second argument is the default state for the reducer.

These are all constructed using createStandardReducer.

Examples

createReplacePayloadReducer

Replaces an action payload with the existing state (uses seamless-immutable replace).

import Immutable from 'seamless-immutable';
import { createReplacePayloadReducer } from '@autotelic/redux-core-reducers';

const defaultState = Immutable({});

const replacePayloadReducer = createReplacePayloadReducer('SOME_ACTION', defaultState);

const replacePayloadReducerMultipleActions = createReplacePayloadReducer(
  [
    'SOME_ACTION',
    'ANOTHER_ACTION,',
  ],
  defaultState,
);

createMergePayloadReducer

Merges an action payload with the existing state (uses seamless-immutable merge).

import Immutable from 'seamless-immutable';
import { createMergePayloadReducer } from '@autotelic/redux-core-reducers';

const defaultState = Immutable({});

const mergePayloadReducer = createMergePayloadReducer('SOME_ACTION', defaultState);

const mergePayloadReducerMultipleActions = createMergePayloadReducer(
  [
    'SOME_ACTION',
    'ANOTHER_ACTION,',
  ],
  defaultState,
);

createMergePayloadReducer

Removes entries from a state object where the key matches the payload Payload may be an Array of strings or a string to be compared against the state objects keys. (uses seamless-immutable without).

import Immutable from 'seamless-immutable';
import { createWithoutPayloadReducer } from '@autotelic/redux-core-reducers';

const defaultState = Immutable({});

const mergePayloadReducer = createWithoutPayloadReducer('SOME_ACTION', defaultState);

const mergePayloadReducerMultipleActions = createWithoutPayloadReducer(
  [
    'SOME_ACTION',
    'ANOTHER_ACTION,',
  ],
  defaultState,
);

createNameValueReducer

Creates a reducer that takes an action that has a payload containing name and value keys and adds those values to the state.

import Immutable from 'seamless-immutable';
import { createNameValueReducer } from '@autotelic/redux-core-reducers';

const defaultState = Immutable({});

const nameValuePayloadReducer = createNameValueReducer('SOME_ACTION', defaultState);

const nameValuePayloadReducerMultipleActions = createNameValueReducer(
  [
    'SOME_ACTION',
    'ANOTHER_ACTION,',
  ],
  defaultState,
);