|
1 | | -function isFunction(val) { |
2 | | - return typeof val === 'function'; |
3 | | -} |
| 1 | +import isFunction from 'lodash/isFunction'; |
| 2 | +import identity from 'lodash/identity'; |
| 3 | +import isNil from 'lodash/isNil'; |
| 4 | +import isSymbol from 'lodash/isSymbol'; |
4 | 5 |
|
5 | 6 | export default function handleAction(type, reducers, defaultState) { |
6 | | - const typeValue = isFunction(type) |
7 | | - ? type.toString() |
8 | | - : type; |
9 | | - |
10 | | - return (state = defaultState, action) => { |
11 | | - // If action type does not match, return previous state |
12 | | - if (action.type !== typeValue) return state; |
| 7 | + const typeValue = isSymbol(type) |
| 8 | + ? type |
| 9 | + : type.toString(); |
13 | 10 |
|
14 | | - const handlerKey = action.error === true ? 'throw' : 'next'; |
| 11 | + const [nextReducer, throwReducer] = isFunction(reducers) |
| 12 | + ? [reducers, reducers] |
| 13 | + : [reducers.next, reducers.throw].map(reducer => (isNil(reducer) ? identity : reducer)); |
15 | 14 |
|
16 | | - // If function is passed instead of map, use as reducer |
17 | | - if (isFunction(reducers)) { |
18 | | - reducers.next = reducers.throw = reducers; |
| 15 | + return (state = defaultState, action) => { |
| 16 | + if (action.type !== typeValue) { |
| 17 | + return state; |
19 | 18 | } |
20 | 19 |
|
21 | | - // Otherwise, assume an action map was passed |
22 | | - const reducer = reducers[handlerKey]; |
23 | | - |
24 | | - return isFunction(reducer) |
25 | | - ? reducer(state, action) |
26 | | - : state; |
| 20 | + return (action.error === true ? throwReducer : nextReducer)(state, action); |
27 | 21 | }; |
28 | 22 | } |
0 commit comments