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

fine-combine

v0.0.3

Published

Combines reducer collections, even when they have duplicate keys

Downloads

15

Readme

For use with Redux. Combines reducer collections, even when they have duplicate keys.

More flexibility in managing reducer collections.

Fine Combine allows different reducers for the same state branch to exist in more than one reducer collection.
It takes multiple reducer collections as arguments, and returns a merged collection that is a simple aggregation of them, EXCEPT THAT where a key is present in two or more of the original reducer collections, the reducer functions for that key are combined into a single reducer function.

The returned collection then becomes an argument of the standard Redux combineReducers() function.
If combineReducers() is used without fineCombine, duplicate reducers in the different collections would simply overwrite one another.

The original target use-case is where some reducers are auto generated (e.g. simple state changes) while others relating to the same branch of the state tree need to be hand coded (e.g. complex async operations).

Installation

To install and save to dependencies in package.json:

npm install --save fine-combine

Usage

Let's assume you have two reducer collections - reducerCollection1 and reducerCollection2 - that have one or more duplicate keys. Typical usage is as follows:

ES6:

import fineCombine from 'fine-combine';

// assume that reducerCollection1 and reducerCollection2 have one or more keys in common
const fineCombinedReducers = fineCombine(reducerCollection1, reducerCollection2);

store = createStore(
  combineReducers({
    ...fineCombinedReducers,
    routing: routerReducer
  })
)    

ES5:

var fineCombine = require('fine-combine');

// assume that reducerCollection1 and reducerCollection2 have one or more keys in common
var fineCombinedReducers = fineCombine(reducerCollection1, reducerCollection2);

store = createStore(
  combineReducers({
    ...fineCombinedReducers,
    routing: routerReducer
  })
)   

How it works

Reducers are typically organized into collections, where each key in the collection matches a branch of the state tree. Sometimes, several such collections may be passed to redux' standard combineReducers() function to build the single reducer function used in constructing the store with createStore().

e.g.

store = createStore(
   combineReducers({
       ...reducerCollection1,
       ...reducerCollection2,
       routing: routerReducer
       }), etc

If there is a duplicate key in reducerCollection1 and reducerCollection2, then reducerCollection2 will override the function from reducerCollection1. fineCombine does not replace combineReducers - it is used to preprocess reducer collections before they are passed to combineReducers.

There is no change to the individual reducer functions passed in EXCEPT in the case of duplicate keys, a new function is returned under that key which chains together the duplicate reducers. Such functions are identifiable in the returned reducer collection, because they have the function name fineCombinedReducer. All other functions will have their original name (if they had one).

The original use-case for this is combining some auto generated and some custom reducers. For example, in a "teams" collection, some actions may be very generic (popping up an edit team form) hence auto generated, while others such as the async fetching of reactive team lists may require custom reducers. We still want all the state related to "team" to be in a single branch of the state tree, so we need a fineCombine function that doesn't simply overwrite conflicting branches when the reducers are combined.

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work.

License

Copyright (c) 2016, Philip Carden. (MIT License)

See LICENSE for more info.