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-action-transform-middleware

v0.5.2

Published

Apply a transformation to a specified target on a Redux action, if that target exists.

Downloads

14

Readme

#redux-action-transform-middleware

Generates Redux middleware for applying a given transformer function to a given sub-object target on each dispatched action (if it exists). This target's key is specified as a string and can be deeply nested (e.g. 'res.data').

If the key isn't found, the unmodified action will be passed to the next middleware. If it is found, the transformed action will be passed instead.

##Installation (get it from npm)

npm install redux-action-transform-middleware

##Usage

actionTransformMiddleware(target, transformer [, options])

target is the key location of the sub-object to transform.

transformer is the function to be applied to the target.

options.allowedActions is a list of Redux action types that, if specified, will cause all other action types to be ignored by the middleware. If not specified, all dispatched actions will be transformed by default.

options.excludedActions is a list of Redux action types that will be explicitly filtered by the middleware. If an action type has also been specified in allowedActions, it will be excluded but a warning will also be printed to the console.

e.g.

const allCapsMiddleware = actionTransformMiddleware(
  'content',
  str => str.toUpperCase(),
  { allowedActions: ['TITLE_REQUEST', 'BODY_REQUEST'] }
);

The above middleware could be used to convert fetched content to all caps whenever a request is made for title or body content (one would assume, based on the action types).

##Full Example

A very simple yet complete Redux app is shown below, using actionTransformMiddleware and a simple transform function to check for an action property called message.name, and lowercase it before it is received by the reducer.

/**
 * ES6/ES2015 syntax
 */
import { createStore, applyMiddleware } from 'redux';
import actionTransformMiddleware from 'redux-action-transform-middleware';

const initialState = {
  name: 'fred'
};

const reducer = (state, action) => {
  state = state || {};
  switch(action.type) {
    case 'NAME_CHANGE':
      return {
        ...state,
        name: action.message.name
      };
    default:
      return state;
  }
};

const quietDown = (name) => name.toLowerCase();
const quietDownMiddleware = actionTransformMiddleware(
  'message.name',
  quietDown
);

const store = createStore(
  reducer,
  initialState,
  applyMiddleware(quietDownMiddleware)
);

console.log(store.getState()); // { name: 'fred' }

store.dispatch({
  type: 'NAME_CHANGE',
  message: {
    name: 'AMANDA'
  }
});

console.log(store.getState()); // { name: 'amanda' }

##Notation

Nested targets can be specified using pure dot notation (e.g. a.b.c) or with square bracket notation (a[0][1]['something']) or any combination (a.b[0].something). For simplicity's sake, only strings and integers are supported as property names/indices in square bracket notation. This should cover 99.9% of use cases.