-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (37 loc) · 1.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* This function handles the bind actions creator to the dispatch.
* @param {Function} fnActionCreator Action creator
* @param {Function} fnDispatch Dispatch
* @return {Function} Action Dispatcher
*/
function bindActionCreator(fnActionCreator, fnDispatch) {
return (...listArguments) => fnDispatch(fnActionCreator(...listArguments, fnDispatch));
}
/**
* This function handles the nested action creators parser.
* @param {Variable} varActionCreators Action creators
* @param {Function} fnDispatch Dispatch
* @return {Variable}
*/
function bindActionCreators(varActionCreators, fnDispatch) {
if (typeof varActionCreators === 'function') {
return bindActionCreator(varActionCreators, fnDispatch);
}
if (typeof varActionCreators !== 'object' || varActionCreators === null) {
throw new Error(
`bindActionCreators expected an object or a function, instead received ${actionCreators === null ? 'null' : typeof actionCreators}. ` +
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
)
}
return Object.keys(varActionCreators).reduce((objActionCreators, strKey) => {
const varActionCreator = varActionCreators[strKey];
let varResult = null;
if (typeof varActionCreator === 'function') {
varResult = bindActionCreator(varActionCreator, fnDispatch);
} else if (typeof varActionCreator === 'object') {
varResult = bindActionCreators(varActionCreator, fnDispatch);
}
return Object.assign(objActionCreators, { [strKey]: varResult } );
}, {});
}
module.exports = bindActionCreators;