Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A functional redux utility to replace the switch case statement.

License

NotificationsYou must be signed in to change notification settings

IsTheJack/reducer-matching

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A functional redux util to replace the switch case statement.

Get the pattern below as alternative to switch case statement.

Instalation

ThereducerMatching is an useful function and can replace the switch statement in the project, assuming a functional posture in the code.

npm install --save reducer-matching

reducerMatching(conditions: Array) : newState: Object

varreducerMatch:[string,function]=[types.INC_COUNT,(state,action)=>'do something...'];

In case of no matches, the reducer matching will return the state without changes.

importreducerMatchingfrom'reducer-matching';constinitialState={};constreducer=(state=initialState,action)=>reducerMatching([types.INC_COUNT,incrementCounter],[types.DEC_COUNT,decrementCounter],[types.FETCH_USERS,fetchUsers],[types.FETCH_USERS_SUCCESS,fetchUsersSuccess],[types.FETCH_USERS_FAILURE,fetchUsersFailure])(state,action);// ... some reducersfunctionfetchUsersSuccess(state,action){return{    ...state,isFetchingUsers:false,users:action.payload,};}// ... other reducersexportdefaultreducer;

Running the Tests

The tests was make in Jest.

npm run test

Contributing

Any PR is welcome! :D

Built With

  • Ramda - A practical functional library for JavaScript programmers.

Motivation

  • Before:
constreducer=(state=appInitialState,action)=>{switch(action.type){casetypes.INC_COUNT:return{ ...state,count:state.count+1};casetypes.DEC_COUNT:return{ ...state,count:state.count-1};casetypes.INC_TEN_COUNT:return{ ...state,count:state.count+10};casetypes.DEC_TEN_COUNT:return{ ...state,count:state.count-10};casetypes.FETCH_USERS:return{ ...state,isFetchingUsers:true};casetypes.FETCH_USERS_SUCCESS:return{     ...state,isFetchingUsers:false,users:action.payload,};casetypes.FETCH_USERS_FAILURE:return{     ...state,isFetchingUsers:false,users:null,};default:returnstate;}};
  • After:
constreducer=(state=appInitialState,action)=>reducerMatching([types.INC_COUNT,incrementCounter],[types.DEC_COUNT,decrementCounter],[types.INC_TEN_COUNT,sumTenToCounter],[types.DEC_TEN_COUNT,subTenToCounter],[types.FETCH_USERS,fetchUsers],[types.FETCH_USERS_SUCCESS,fetchUsersSuccess],[types.FETCH_USERS_FAILURE,fetchUsersFailure])(state,action);

Author

Comming soon:

  • More examples and documentation

About

A functional redux utility to replace the switch case statement.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp