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

lore-reducers

v0.13.0

Published

Library to reduce boilerplate for Redux reducers

Downloads

11

Readme

lore-reducers

Installation

Lore Reducers is available as an npm package.

npm install lore-reducers

After npm install, you'll find all the .js files in the /src folder and their compiled versions in the /lib folder.

Usage

lore-reducers is an abstraction teir to reduce the boilerplate associated with created reducers for use with Redux. It provides a set of common reducer functions as well as a blueprint you can use to build reducers.

The blueprint looks like this:

var { utils, blueprint } = require('lore-reducers');

var all = blueprint({
    state: 'INITIAL_STATE',
    data: []
  }, [
    {
      actionType: types.ADD_TODO,
      method: utils.addOrUpdateModel
    },{
      actionType: types.UPDATE_TODO,
      method: utils.updateModel
    },{
      actionType: types.REMOVE_TODO,
      method: utils.removeModel
    },{
      actionType: types.FETCH_TODO,
      method: utils.mergeModels
    },{
    actionType: types.FETCH_TODOS_IN_LIST,
    method: utils.mergeModels
  }
  ]
);

The following common functions are also available within the utils object. Given a reducer that looks like this:

function(state, action){...}
  • addModel: take the action.payload add it to state.data
  • updateModel: find the model within state.data that matches action.payload and replace it
  • removeModel: find the model within state.data that matches action.payload and remove it
  • replaceModels: poorly named, but effectively replaces the current state with action.payload
  • mergeModels: takes the set of models in action.payload and merges them into state.data (adding or updating each model as applicable)
  • copyState: copy action.payload.state and action.payload.error to state.state and state.error respectively
  • getIndex: if action.payload is in state.data, returns the index, otherwise returns -1
  • addOrUpdateModel: updates the model in state.data if action.payload already exists, otherwise adds it to state.data
  • mergeModelsAndCopyState: calls mergeModels and also runs copyState to copy state and error attributes to state

The following functions are also available, which are mean to be used as reducers themselves:

  • function byId(state): takes all models in state.data and creates a dictionary from them using their id as the key
  • function byCid(state): takes all models in state.data and creates a dictionary from them using their cid as the key

Reasoning for Interface

The thought process behind this was to reduce boilerplate while being extensible, and providing an interface that was generic enough to adapt to the wide array of (often changing) reducer needs on the client side.

The central logic of a reducer is a case statement that says "if the action.type matches X, apply this reducing function to the state". With that in mind, the only unique about a reducer is the ActionType it's listening for and the method it should apply to state. So that's why those are the two arguments in the object.

Additionally, this interface can also let you pass in custom functions aren't in the utils helpers in order to accomodate custom application needs.

Another design goal was to create an interface that could easily be generated by a series of conventions.

The actionType follows the pattern of MethodName_ModelName, and in most cases MethodName (add, update, remove) maps to a common function (addModel, updateModel, removeModel). So if a framework created a set of conventions for the ActionTypes, and made sure they were consistent between the Actions and Reducers, many of the reducers could be reduced to an empty config files, similar to the magic that Sails applies to Models and Controllers which both start off as empty objects.