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-creator

v0.2.1

Published

A series of helper methods to reduce the boilerplate of Redux

Downloads

16

Readme

New in 0.2.0

  • createThunk now supports a deserializer function to parse response data
  • createThunk and createReducer now require strong parameters
  • $apply now properly applies to the current state value instead of the payload while using createReducer

Redux Creator

This is a lightweight library designed to help reduce the amount of boilerplate usually required in Redux workflows. There are several easy to use functions that can help reduce the size of your redux/ducks files by dozens of lines!

Installation

Redux Creator is incredibly easy to install, simply run one of these lines in your terminal:

NPM: npm install redux-creator --save

Yarn: yarn add redux-creator

Usage

createAction

This is a fairly straightforward function that takes an action type as a method and returns a redux function. The only opinion it holds is that the data passed into the action body is referred to as payload.

Example

const UPDATE_MESSAGE = 'messages/UPDATE_MESSAGE';

export const updateMessage = createAction(UPDATE_MESSAGE); 
/*=> (payload) => ({
  type: 'messages/UPDATE_MESSAGE',
  payload
}) */

createThunk

Another easy to use function when making API calls in your redux cycle. This function takes an API util function and an action as arguments, and returns the default 'thunk' pattern of asynchronous actions. Thunks return promisable objects by default, and this helper is no different.

Example

export const sendMessage = createThunk({
  api: MessageAPI.send,
  action: updateMessage,
});
/* => (data) => (dispatch) => {
  MessageAPI.send(data)
    .then((resp) => (
      dispatch(updateMessage(resp));
      return resp;
    })
) */

createThunk can also be used to handle errors, with an errorHandler function:

export const sendMessage = createThunk({
  api: MessageAPI.send,
  action: updateMessage,
  errorHandler: updateMessageErrors
});
/* => (data) => (dispatch) => (
  MessageAPI.send(data)
    .then((resp) => {
      dispatch(updateMessage(resp));
      return resp;
    })
    .catch((err) => {
      dispatch(updateMessageErrors(err));
      return err;
    })
) */

An optional deserializer layer can also be applied to createThunk if you are expecting to recieve API data that requires parsing. Deserializers should be provided as functions:

export const sendMessage = createThunk({
  api: MessageAPI.send,
  action: updateMessage,
  deserializer: parseMessageResponse
});
/* => (data) => (dispatch) => (
  MessageAPI.send(data)
    .then((resp) => parseMessageResponse(resp))
    .then((resp) => {
      dispatch(updateMessage(resp));
      return resp;
    })
) */

configureStore

This is a helper method which takes in the root reducer in your redux and an array of middleware and creates a store with those middlewares applied.

Example

export default configureStore(RootReducer, [thunk])
// => (preloadedState = {}) => createStore(RootReducer, preloadedState, applyMiddleware(thunk))

createReducer

This is the most complicated of the helper functions but also has the biggest payoff. Reducers are often bulky and hard to read, and createReducer aims to create a prettier looking reducer that's much easier to grok. This is the most opinionated of the helper methods, and a somewhat specific syntax must be followed in order for it to properly work.

Example

const UPDATE_MESSAGE = 'messages/UPDATE_MESSAGE';
const UPDATE_MESSAGE_ERRORS = 'messages/UPDATE_MESSAGE_ERRORS';

const messageReducerActions = {
  [UPDATE_MESSAGE]: '{ "currentUser": { "message": { "$set": $payload } } }',
  [UPDATE_MESSAGE_ERRORS]: '{ "currentUser": { "errors": { "message": { "$set": $payload } } } }'
}

export default createReducer({
  reducerCases: messageReducerActions,
  initialState
});
/* => (state = initialState, action) => {
  let reducerObj = {
    'messages/UPDATE_MESSAGE': (payload) => update(state, { currentUser: { message: { $set: payload } }),
    'messages/UPDATE_MESSAGE_ERRORS': (payload) => update(state, { currentUser: { errors: { message: { $set: payload } } } })
  };
  return reducerObj[action.type](action.payload) || state;
}
*/

The pattern is as follows: createReducer accepts an Object of action types as keys and JSON strings as values. The JSON strings use the same syntax as the update method from the immutability-helper library, which is used internally as shown, but the strings must be valid JSON to work. After calling for an update operation like $set or $merge, the value of that key must be marked as $payload. Because the value is just a string it won't throw any invalid JSON syntax errors until it is parsed by createReducer, at which time $payload will be substituted with the proper value. This is also the reason createAction uses the naming convention of $payload on its action data; createReducer relies on action.payload to function properly.

Handling Apply

If you wish to use immutability-helper's built-in $apply method it is important to note the format of the reducer case. Instead of the typical JSON string, createReducer expects an object with the keys string, for the JSON string including $callback instead of $payload, and callback, which is the actual callback function.

MIT License

Copyright (c) 2017 Sean Beyer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.