1.1.2 • Published 6 years ago

match-reducer v1.1.2

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

Match-reducer

Build Status Node version JavaScript Style Guide Known Vulnerabilities

Elegant reducer

Table of Contents

Installation

npm i -S match-reducer

Usage

matchReducer

The matchReducer accept as first argument an object mapping action types with their respective reducers function. Second argument (optionnal) accept the initialState and a boolean value for the passOnlyPayload property (default to false, pass the whole action to the reducer).

import { matchReducer } from 'match-reducer'

const initialState = { counter: 0, something: 'else' }
const reducer = matchReducer(
  {
    INC: (state, payload) => ({
      counter: state.counter + payload
    }),
    DEC: (state, payload) => ({
      counter: state.counter - payload
    })
  },
  {
    initialState,
    passOnlyPayload: true
  }
)

reducer(null, { type: 'INC', payload: 1 })
// { counter: 1, something: 'else' }

makeActionsAndReducer

Take the same aguments as matchReducer and return an object with a reducer and camelCased action creators with a type and payload property.

import { makeActionsAndReducer } from 'match-reducer'

const { reducer, actionCreators } = makeActionsAndReducer(
  {
    INC_SOMETHING: (state, action) => ({
      counter: state.counter + action.payload
    })
  }
)

reducer({ counter: 0 }, actionCreators.incSomething(1) })
// { counter: 1 }

Support

Please open an issue for support.

Contributing

Please contribute using Github Flow. Create a branch, add commits, and open a pull request.