Advertisement
Guest User

Untitled

a guest
May 20th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. function counter(state = 0, action) {
  2. switch (action.type) {
  3. case 'INCREMENT':
  4. return state + 1
  5. case 'DECREMENT':
  6. return state - 1
  7. default:
  8. return state
  9. }
  10. }
  11.  
  12. //change to
  13.  
  14. const counter = (state = 0, action) =>
  15. action.type === 'INCREMENT' ? state + 1
  16. : action.type === 'DECREMENT' ? state - 1
  17. : state
  18.  
  19. // change to
  20.  
  21. const switchcase = cases => defaultCase => key =>
  22. cases.hasOwnProperty(key) ? cases[key] : defaultCase
  23.  
  24. const counter = (state = 0, action) =>
  25. switchcase({
  26. 'INCREMENT': state + 1,
  27. 'DECREMENT': state -1
  28. })(state)(action.type)
  29.  
  30. // change to
  31.  
  32. const switchcaseF = cases => defaultCase => key =>
  33. switchcase(cases)(defaultCase)(key)()
  34.  
  35. const counter = (state = 0, action) =>
  36. switchcaseF({
  37. 'INCREMENT': () => state + 1,
  38. 'DECREMENT': () => state -1
  39. })(() => state)(action.type)
  40.  
  41. // change to
  42. const executeIfFunction = f =>
  43. f instanceof Function ? f() : f
  44.  
  45. const switchcaseF = cases => defaultCase => key =>
  46. executeIfFunction(switchcase(cases)(defaultCase)(key))
  47.  
  48. const counter = (state = 0, action) =>
  49. switchcaseF({
  50. 'INCREMENT': () => state + 1,
  51. 'DECREMENT': () => state -1
  52. })(state)(action.type)
  53.  
  54. const counter = (state = 0, action) =>
  55. switchcaseF({
  56. 'RESET': 0,
  57. 'INCREMENT': () => state + 1,
  58. 'DECREMENT': () => state -1
  59. })(state)(action.type)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement