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

redux-reducer-array-helpers

v3.3.1

Published

redux helpers for arrays in reducers

Downloads

14

Readme

npm version Build Status Coverage Status Inline docs Hit Count Build Dependencies Dev Dependencies

NPM

contributions welcome

Installation

install

$ npm i redux-reducer-array-helpers 
$ yarn add redux-reducer-array-helpers

Motivation

You start with a button and an action to toggle its state. When you change to a list of buttons, the action needs to be modified to use an identifier of the button itself that was clicked (usually and index).

The idea here is to have the action unchanged and wrap it into another action creator that injects the index

It also supports multiple dispatches (for example in a thunk action used with redux-thunk)

CHANGELOG

  • V3.3 : Added indexer reducer creator
  • V3 : Added support for named keys besides index keys (examples below)

API

Reducer creator

/**
 * Will create a reducer to handle the given action types, splitting state by indexes for the wrappedReducer to handle
 * @param {function} wrappedReducer - reducer to decorate
 * @param {object} action types - key to fetch the index
 * @param {object} [options]
 * @param {object} [options.initialState] initial state for the reducer
 * @param {string} [options.key] - key to fetch the index
 * @returns the mapped reducer that handles the types for sliced index state
 */
function indexedReducer(wrappedReducer, actionTypes, options)

Reducer action type handler

/**
 * Reducer decorator to fetch the payload index and nextIndex
 * @param {function} wrappedReducer - reducer to decorate
 * @param {string} [key] - key to fetch the index
 * @returns the mapped reducer that slices state with the current index, and applies to the inner reducer
 *          the next index on the action payload
 */
function unbindIndexToReducer(wrappedReducer, key)

action creators creator

/**
 * Factory of index-based action creators
 * @param {object|function} actionCreators - redux action creator(s)
 * @param {string|object} keyIndexMap - string or key value pair dictionary with key : index to bind in each action generated by each action creator
 * @returns the mapped action creators with the index bound to the generated actions
 */
function bindIndexToActionCreators(actionCreators, keyOrKeyIndexMap)

Example

  • action creators injection
export function doToggleColor() {
  return {
    type: actionTypes.DO_TOGGLE_COLOR
  };
}
  • numeric Index Usage
import { bindIndexToActionCreators } from 'redux-array-reducer-helpers'
import { doToggleColor } from 'actions/button_actions'

// index = 2 // Example
//... etc
bindIndexToActionCreators(doToggleColor, { colors : index })
//... etc
//state.colors = [someColorObj, someColorObj]
// colorItemReducer = reducer that handles one someColorObj


import { unbindIndexToReducer } from 'redux-array-reducer-helpers'

  function doToggleColorHandler(state, action) {
    const newColors = unbindIndexToReducer(colorItemReducer, 'colors')(state.colors, action);

    return { ...state, colors: newColors };
  }
  • named Index Usage
import { bindIndexToActionCreators } from 'redux-array-reducer-helpers'
import { doToggleColor } from 'actions/button_actions'

//... etc
bindIndexToActionCreators(doToggleColor, 'redColor')
//... etc
//state.colors = { redColor : someColorObj, blueColor : someColorObj }
// colorItemReducer = reducer that handles one someColorObj

import { unbindIndexToReducer } from 'redux-array-reducer-helpers'

  function doToggleRedColorHandler(state, action) {
    const newColors = unbindIndexToReducer(colorItemReducer, 'redColor')(state.colors, action);

    return { ...state, colors: newColors };
  }

or generic usage (this will assume the indexes property on action payload only contains named indexes)

  function doToggleColorHandler(state, action) {
    const newColors = unbindIndexToReducer(colorItemReducer)(state.colors, action);

    return { ...state, colors: newColors };
  }
  • reducer creator (sets the handlers by convention)

import { indexedReducer } from 'redux-array-reducer-helpers'

export default indexedReducer(wrappedReducer, actionTypes);

License

MIT