Skip to content

try-again-apps/redux-common-reducers

Repository files navigation

redux-common-reducers 🐣

npm package Build Status Coverage Status Known Vulnerabilities

dependencies Status devDependencies Status

Installation

npm install -S redux-common-reducers

or

yarn add redux-common-reducers

Usage

booleanReducer(trueActions, falseActions, [initialValue = false])

Arguments

  • trueActions (Array) - The actions' names which sets value to true.
  • falseActions (Array) - The actions' names which sets value to false.
  • [initialValue=false] (boolean) - The initial value of state.

Example

import { booleanReducer } from 'redux-common-reducers';

const myView = combineReducers({
  isDoingSth: booleanReducer(
    ['TRUE_ACTION1', 'TRUE_ACTION2'],
    ['FALSE_ACTION1', 'FALSE_ACTION2'],
    true)
})

is same as:

const isDoingSth = (state = true, action) => {
  switch (action.type) {
    case 'TRUE_ACTION1':
    case 'TRUE_ACTION2':
      return true;
    case 'FALSE_ACTION1':
    case 'FALSE_ACTION2':
      return false;
    default:
      return state;
  }
}

const myView = combineReducers({
  isDoingSth
})

numberReducer(changeActions, pathToValue, [initialValue = 0])

Arguments

  • changeActions (Array) - The actions' names which changes value of state.
  • pathToValue (Array|string) - The path of the property to get. Lodash get is used under the hood so same rules apply as there. Check lodash.get docs. 👀
  • [initialValue=0] (number) - The initial value of state.

Example

import { numberReducer } from 'redux-common-reducers';

const stats = combineReducers({
  errorCount: numberReducer(
    ['CHANGE_ACTION1', 'CHANGE_ACTION2'],
    'path.to.somewhere'
    42)
})

is same as:

const errorCount = (state = 42, action) => {
  switch (action.type) {
    case 'CHANGE_ACTION1':
    case 'CHANGE_ACTION2':
      return action.path.to.somewhere;
    default:
      return state;
  }
}

const stats = combineReducers({
  errorCount
})

stringReducer(changeActions, resetActions, pathToValue, [initialValue = ''])

Arguments

  • changeActions (Array) - The actions' names which changes value of state.
  • resetActions (Array) - The actions' names which resets value to default value (initialValue).
  • pathToValue (Array|string) - The path of the property to get. Lodash get is used under the hood so same rules apply as there. Check lodash.get docs. 👀
  • [initialValue=''] (string) - The initial value of state.

Example

import { stringReducer } from 'redux-common-reducers';

const myView = combineReducers({
  name: stringReducer(
    ['CHANGE_ACTION1', 'CHANGE_ACTION2'],
    ['RESET_ACTION1'],
    'path.to.somewhere')
})

The stringReducer will look for value in action.path.to.somewhere. This is action that will trigger change of name to new value:

{ type: 'CAHNGE_ACTION1', path: { to: { somewhere: 'new value' } } }