{"action":"created","issue":{"url":"https://api.github.com/repos/rackt/redux/issues/883","labels_url":"https://api.github.com/repos/rackt/redux/issues/883/labels{/name}","comments_url":"https://api.github.com/repos/rackt/redux/issues/883/comments","events_url":"https://api.github.com/repos/rackt/redux/issues/883/events","html_url":"https://github.com/rackt/redux/issues/883","id":110954070,"number":883,"title":"take away the huge switch block","user":{"login":"yeatszhang","id":6488747,"avatar_url":"https://avatars.githubusercontent.com/u/6488747?v=3","gravatar_id":"","url":"https://api.github.com/users/yeatszhang","html_url":"https://github.com/yeatszhang","followers_url":"https://api.github.com/users/yeatszhang/followers","following_url":"https://api.github.com/users/yeatszhang/following{/other_user}","gists_url":"https://api.github.com/users/yeatszhang/gists{/gist_id}","starred_url":"https://api.github.com/users/yeatszhang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yeatszhang/subscriptions","organizations_url":"https://api.github.com/users/yeatszhang/orgs","repos_url":"https://api.github.com/users/yeatszhang/repos","events_url":"https://api.github.com/users/yeatszhang/events{/privacy}","received_events_url":"https://api.github.com/users/yeatszhang/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rackt/redux/labels/ecosystem","name":"ecosystem","color":"ff69b4"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-10-12T11:11:29Z","updated_at":"2015-10-12T13:30:19Z","closed_at":"2015-10-12T11:59:37Z","body":" I really hate the huge switch, so I write a util function to help write reducer in a better way. ( My English is not that good, but I will try to show my idea. )\r\n\r\n## example\r\n\r\nwe can write like this.\r\n\r\n```\r\nimport { ADD_TODO, DELETE_TODO, EDIT_TODO, COMPLETE_TODO, COMPLETE_ALL, CLEAR_COMPLETED } from '../constants/ActionTypes';\r\n\r\nconst initialState = [{\r\n  text: 'Use Redux',\r\n  completed: false,\r\n  id: 0\r\n}];\r\n\r\nexport default createReducer({\r\n  [ADD_TODO]: (state, { text }) => [{\r\n    id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,\r\n    completed: false,\r\n    text\r\n  }, ...state],\r\n\r\n  [DELETE_TODO]: (state, { id }) => state.filter(todo =>\r\n    todo.id !== action.id\r\n  ),\r\n\r\n  [EDIT_TODO]: (state, { id, text }) => state.map(todo =>\r\n    todo.id === id ?\r\n      Object.assign({}, todo, { text }) :\r\n      todo\r\n  ),\r\n\r\n  [COMPLETE_ALL]: state => {\r\n    const areAllMarked = state.every(todo => todo.completed);\r\n    return state.map(todo => Object.assign({}, todo, {\r\n      completed: !areAllMarked\r\n    }));\r\n  },\r\n\r\n  [CLEAR_COMPLETED]: state => state.filter(todo => todo.completed === false)\r\n}, initialState)\r\n\r\n```\r\n\r\n## source code \r\n\r\n```\r\n/**\r\n * an elegance way to write reducer\r\n * @param funcMap the functions map\r\n * @param initState initiate state\r\n * @returns {Function}\r\n */\r\nfunction createReducer(funcMap, initialState) {\r\n  if (!isPlainObject(funcMap)) {\r\n    throw new Error('funcMap need to be a plain object');\r\n  }\r\n\r\n  return (state = initState, action) =>  map.hasOwnProperty(action.type) ? \r\n  funcMap[action.type](state, action) : \r\n  state;\r\n}\r\n``` \r\n\r\n\r\n\r\n## features\r\n\r\n* simple, whether use or not depends on you.\r\n* won't worry about the variable name duplicated\r\n* can use destruct `action` and `state`\r\n* can use arrow function\r\n* hashTable is faster than `switch` when having a lot of cases \r\n* don't need annoying `break` and `default`\r\n"},"comment":{"url":"https://api.github.com/repos/rackt/redux/issues/comments/147400864","html_url":"https://github.com/rackt/redux/issues/883#issuecomment-147400864","issue_url":"https://api.github.com/repos/rackt/redux/issues/883","id":147400864,"user":{"login":"yeatszhang","id":6488747,"avatar_url":"https://avatars.githubusercontent.com/u/6488747?v=3","gravatar_id":"","url":"https://api.github.com/users/yeatszhang","html_url":"https://github.com/yeatszhang","followers_url":"https://api.github.com/users/yeatszhang/followers","following_url":"https://api.github.com/users/yeatszhang/following{/other_user}","gists_url":"https://api.github.com/users/yeatszhang/gists{/gist_id}","starred_url":"https://api.github.com/users/yeatszhang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yeatszhang/subscriptions","organizations_url":"https://api.github.com/users/yeatszhang/orgs","repos_url":"https://api.github.com/users/yeatszhang/repos","events_url":"https://api.github.com/users/yeatszhang/events{/privacy}","received_events_url":"https://api.github.com/users/yeatszhang/received_events","type":"User","site_admin":false},"created_at":"2015-10-12T13:30:19Z","updated_at":"2015-10-12T13:30:19Z","body":"@johanneslumpe  thanks for recommending this useful package. I like the way it simplify actionCreator and reducer. But I think it isn't necessary to use a reduce method to handle multiple actions. Maybe hash is enough and more efficient.  But the little problem can't stop me using it  ^ ^ . I'd like to have a discussion with the author and contribute to it."}}