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

v1.1.0

Published

A utility for converting a redux reducer into a collection.

Downloads

5

Readme

redux-collect

A utility for converting a redux reducer into a collection.

npm install --save redux-collect

Usage

// es6 modules
import { collectReducer, collectSelectors, collectActions } from 'redux-collect'
// require syntax
const { collectReducer, collectSelectors, collectActions } = require('redux-collect')

Example

See the examples folder for a full, runnable example.

Say you have a reducer, selectors, and actions, representing a single entity, such as:

const carReducer = (state, action) => {
  switch (action.type) {
    case types.ADD:
      return action.car
    case types.SET_PRICE:
      return Object.assign({}, state, { price: action.price })
    default:
      return state
  }
}

const carSelectors = {
  getPrice: state => state.price,
  isModel: (state, model) => state.model === model
}

const carActions = {
  add: car => ({
    car,
    type: types.ADD
  }),
  setPrice: price => ({
    price,
    type: types.SET_PRICE
  }),
  fetch: url => dispatch => get(url).then(car => dispatch(add(car)))
}

As your application grows, you may want to be able to represent multiple cars, stored based on VIN (Vehicle Identification Numbers). redux-collect makes it easy to reuse your existing functions:

const reducer = collectReducer(carReducer, 'vin')
const selectors = collectSelectors(carSelectors)
const actions = collectActions(carActions, 'vin')

The resulting reducer, selectors, and actions now behave as a collection:

// state is now a collection
const store = createStore(reducer, {
  "1G6KD54Y73U255447": {
    "make": "Jeep",
    "model": "Renegade",
    "price": 25000
  },
  "1FDSF34F13EB85525": {
    "make": "Chevrolet",
    "model": "Corvette",
    "price": 45000
  }
})

// selectors and actions now take an additional VIN parameter
selectors.getPrice(store.getState(), "1G6KD54Y73U255447") // 25000

// the reducer handles the new action
store.dispatch(updatePrice('1G6KD54Y73U255447', 30000))

// the state is updated properly
selectors.getPrice(store.getState(), "1G6KD54Y73U255447") // 30000

// store remains transparent and serializable
console.log(store.getState())
// "1G6KD54Y73U255447": {
//   "make": "Jeep",
//   "model": "Renegade",
//   "price": 30000
// },
// "1FDSF34F13EB85525": {
//   "make": "Chevrolet",
//   "model": "Corvette",
//   "price": 45000
// }

API

collectReducer

const reducerForCollection = collectReducer(reducerForSingleEntity, pathToKeyWithinAction)

reducerForSingleEntity should return undefined to remove the entity from the collection. Otherwise, the state it returns is added to the collection.

pathToKeyWithinAction is the path where the generated reducer looks for the collection key within dispatched actions.

collectSelector

Most of the time collectSelectors is recommended.

const selectorForCollection = collectSelector(selectorForSingleEntity)

selectorForSingleEntity entity is a selector corresponding to reducerForSingleEntity, with is called like selectorForSingleEntity(state, ...args).

selectorForCollection is a generated selector corresponding to reducerForCollection, and is called liked selectorForCollection(state, key, ...args).

collectSelectors

const selectorsForSingleEntity = {
  selector1,
  selector2
}

// Same as:
// const selectorsForCollection = {
//   selector1: collectSelector(selector1),
//   selector2: collectSelector(selector2)
// }
const selectorsForCollection = collectSelectors(selectorsForSingleEntity)

Similar to collectSelector, except it takes an object containing one or more selectors.

collectAction

Most of the time, collectActions is recommended.

const actionCreatorForCollection = collectionAction(actionCreatorForSingleEntity, pathToKeyWithinAction, optionalSelectorsForThunks)

actionCreatorForSingleEntity is an action creator with no knowledge of the collection, most likely corresponding to reducerForSingleEntity.

pathToKeyWithinAction is the path where actions will be decorated with the collection key.

optionalSelectorsForThunks is an optional parameter that is recursively passed as the third parameter to thunks created by actionCreatorForSingleEntity.

collectActions

const actionCreatorsForCollection = collectionActions(actionCreatorsForSingleEntity, pathToKeyWithinAction, optionalSelectorsForThunks)

Similar to collectAction, but for an object containing multiple action creators.

bindSelector, bindSelectors, bindCollectedAction, and bindCollectedActions

const boundSelector = bindSelector(selectorForCollection, key)
const boundSelectors = bindSelector(selectorsForCollection, key)
const boundActionCreator = bindCollectedAction(actionCreatorForCollection, key)
const boundActionCreators = bindCollectedActions(actionCreatorsForCollection, key)

Helpers to bind the output of collectSelector and collectActions to key corresponding to a specific entity. e.g.:

const getJeepPrice = bindSelector(selectors.getPrice, "1G6KD54Y73U255447")
const setJeepPrice = bindCollectedAction(actions.setPrice, "1G6KD54Y73U255447")

Immutability

The collections are maintained using Immutable.js. Maps created by Immutable.Map are properly serializable. redux-collect should handle state hydration and serialization properly.