Skip to content

A common utility to create high performance reducer, super lightweight without any dependencies.

Notifications You must be signed in to change notification settings

lidoravitan/create-map-reducer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Create Map Reducer

A common utility to create high performance reducer, super lightweight without any dependencies. Github

Installation

npm install create-map-reducer or
yarn add create-map-reducer

Usage

export enum ActionTypes {
  INCREMENT_BY = 'INCREMENT_BY',
  DECREMENT_BY = 'DECREMENT_BY',
  SET_COUNTER_NAME = 'SET_COUNTER_NAME',
}

export const incrementByAction = (value: number) =>
  <const>{
    type: ActionTypes.INCREMENT_BY,
    value,
  }

export const decrementByAction = (value: number) =>
  <const>{
    type: ActionTypes.DECREMENT_BY,
    value,
  }

export const setCounterNameAction = (title: string) =>
  <const>{
    type: ActionTypes.SET_COUNTER_NAME,
    title,
  }

export type Actions = ReturnType<
  | typeof setCounterNameAction
  | typeof incrementByAction
  | typeof decrementByAction
>

type Actions = Increment | Decrement
import { createReducer } from 'create-map-reducer'
import { Actions, ActionTypes } from './actions'

type CounterState = Readonly<{
  title: string
  value: number
}>

const initialState: CounterState = {
  title: 'my-counter',
  value: 0,
}

export const counterReducer = createReducer<CounterState, Actions>(
  initialState,
  {
    [ActionTypes.DECREMENT_BY]: (state, action) => ({
      ...state,
      value: action.value,
    }),
    [ActionTypes.INCREMENT_BY]: (state, action) => ({
      ...state,
      value: action.value,
    }),
    [ActionTypes.SET_COUNTER_NAME]: (state, action) => ({
      ...state,
      title: action.title,
    }),
  }
)

License

MIT

About

A common utility to create high performance reducer, super lightweight without any dependencies.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published