@@ -6,17 +6,18 @@ import reduce from 'lodash/reduce';
66import isString from 'lodash/isString' ;
77import isFunction from 'lodash/isFunction' ;
88import createAction from './createAction' ;
9+ import invariant from 'invariant' ;
910
1011export default function createActions ( actionsMap , ...identityActions ) {
11- if ( identityActions . every ( isString ) ) {
12- if ( isString ( actionsMap ) ) {
13- return fromIdentityActions ( [ actionsMap , ...identityActions ] ) ;
14- } else if ( isPlainObject ( actionsMap ) ) {
15- return { ...fromActionsMap ( actionsMap ) , ...fromIdentityActions ( identityActions ) } ;
16- }
12+ invariant (
13+ identityActions . every ( isString ) &&
14+ ( isString ( actionsMap ) || isPlainObject ( actionsMap ) ) ,
15+ 'Expected optional object followed by string action types'
16+ ) ;
17+ if ( isString ( actionsMap ) ) {
18+ return fromIdentityActions ( [ actionsMap , ...identityActions ] ) ;
1719 }
18-
19- throw new TypeError ( 'Expected optional object followed by string action types' ) ;
20+ return { ...fromActionsMap ( actionsMap ) , ...fromIdentityActions ( identityActions ) } ;
2021}
2122
2223function isValidActionsMapValue ( actionsMapValue ) {
@@ -32,12 +33,11 @@ function isValidActionsMapValue(actionsMapValue) {
3233
3334function fromActionsMap ( actionsMap ) {
3435 return reduce ( actionsMap , ( actionCreatorsMap , actionsMapValue , type ) => {
35- if ( ! isValidActionsMapValue ( actionsMapValue ) ) {
36- throw new TypeError (
37- 'Expected function, undefined, or array with payload and meta ' +
38- `functions for ${ type } ` ) ;
39- }
40-
36+ invariant (
37+ isValidActionsMapValue ( actionsMapValue ) ,
38+ 'Expected function, undefined, or array with payload and meta ' +
39+ `functions for ${ type } `
40+ ) ;
4141 const actionCreator = isArray ( actionsMapValue )
4242 ? createAction ( type , ...actionsMapValue )
4343 : createAction ( type , actionsMapValue ) ;
@@ -47,9 +47,7 @@ function fromActionsMap(actionsMap) {
4747}
4848
4949function fromIdentityActions ( identityActions ) {
50- return fromActionsMap (
51- identityActions . reduce (
52- ( actionsMap , actionType ) => ( { ...actionsMap , [ actionType ] : identity } )
53- , { } )
54- ) ;
50+ return fromActionsMap ( identityActions . reduce (
51+ ( actionsMap , actionType ) => ( { ...actionsMap , [ actionType ] : identity } )
52+ , { } ) ) ;
5553}
0 commit comments