Skip to content

Commit 227db63

Browse files
yangmillstheorytimche
authored andcommitted
standardize on invariant for runtime checks
1 parent f9bf59e commit 227db63

File tree

4 files changed

+34
-36
lines changed

4 files changed

+34
-36
lines changed

src/__tests__/combineActions-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { expect } from 'chai';
44
describe('combineActions', () => {
55
it('should throw an error if any action is not a function or string', () => {
66
expect(() => combineActions(1, 'ACTION_2'))
7-
.to.throw(TypeError, 'Expected action types to be strings, symbols, or action creators');
7+
.to.throw(Error, 'Expected action types to be strings, symbols, or action creators');
88

99
expect(() => combineActions('ACTION_1', () => {}, null))
10-
.to.throw(TypeError, 'Expected action types to be strings, symbols, or action creators');
10+
.to.throw(Error, 'Expected action types to be strings, symbols, or action creators');
1111
});
1212

1313
it('should accept action creators and action type strings', () => {

src/__tests__/createActions-test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ describe('createActions', () => {
55
it('should throw an error when given arguments that contain a non-string', () => {
66
const expectedError = 'Expected optional object followed by string action types';
77

8-
expect(() => createActions(1)).to.throw(TypeError, expectedError);
9-
expect(() => createActions({ ACTION_1: undefined }, [])).to.throw(TypeError, expectedError);
10-
expect(() => createActions('ACTION_1', true)).to.throw(TypeError, expectedError);
8+
expect(() => createActions(1)).to.throw(Error, expectedError);
9+
expect(() => createActions({ ACTION_1: undefined }, [])).to.throw(Error, expectedError);
10+
expect(() => createActions('ACTION_1', true)).to.throw(Error, expectedError);
1111
});
1212

1313
it('should throw an error when given bad payload creators', () => {
1414
expect(
1515
() => createActions({ ACTION_1: {} })
1616
).to.throw(
17-
TypeError,
17+
Error,
1818
'Expected function, undefined, or array with payload and meta functions for ACTION_1'
1919
);
2020

@@ -24,7 +24,7 @@ describe('createActions', () => {
2424
ACTION_2: 'string'
2525
})
2626
).to.throw(
27-
TypeError,
27+
Error,
2828
'Expected function, undefined, or array with payload and meta functions for ACTION_2'
2929
);
3030
});
@@ -38,7 +38,7 @@ describe('createActions', () => {
3838
]
3939
})
4040
).to.throw(
41-
TypeError,
41+
Error,
4242
'Expected function, undefined, or array with payload and meta functions for ACTION_1'
4343
);
4444

@@ -54,7 +54,7 @@ describe('createActions', () => {
5454
]
5555
})
5656
).to.throw(
57-
TypeError,
57+
Error,
5858
'Expected function, undefined, or array with payload and meta functions for ACTION_2'
5959
);
6060
});
@@ -63,7 +63,7 @@ describe('createActions', () => {
6363
expect(
6464
() => createActions({ ACTION_1: undefined }, 'ACTION_2')
6565
).to.throw(
66-
TypeError,
66+
Error,
6767
'Expected function, undefined, or array with payload and meta functions for ACTION_1'
6868
);
6969

@@ -73,7 +73,7 @@ describe('createActions', () => {
7373
ACTION_2: undefined
7474
})
7575
).to.throw(
76-
TypeError,
76+
Error,
7777
'Expected function, undefined, or array with payload and meta functions for ACTION_2'
7878
);
7979
});
@@ -84,7 +84,7 @@ describe('createActions', () => {
8484
ACTION_1: [() => {}]
8585
})
8686
).to.throw(
87-
TypeError,
87+
Error,
8888
'Expected function, undefined, or array with payload and meta functions for ACTION_1'
8989
);
9090
});

src/combineActions.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import isFunction from 'lodash/isFunction';
33
import isEmpty from 'lodash/isEmpty';
44
import toString from 'lodash/toString';
55
import isSymbol from 'lodash/isSymbol';
6+
import invariant from 'invariant';
67

78
export const ACTION_TYPE_DELIMITER = '||';
89

@@ -18,11 +19,10 @@ function isValidActionTypes(actionTypes) {
1819
}
1920

2021
export default function combineActions(...actionsTypes) {
21-
if (!isValidActionTypes(actionsTypes)) {
22-
throw new TypeError('Expected action types to be strings, symbols, or action creators');
23-
}
24-
22+
invariant(
23+
isValidActionTypes(actionsTypes),
24+
'Expected action types to be strings, symbols, or action creators'
25+
);
2526
const combinedActionType = actionsTypes.map(toString).join(ACTION_TYPE_DELIMITER);
26-
2727
return { toString: () => combinedActionType };
2828
}

src/createActions.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import reduce from 'lodash/reduce';
66
import isString from 'lodash/isString';
77
import isFunction from 'lodash/isFunction';
88
import createAction from './createAction';
9+
import invariant from 'invariant';
910

1011
export 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

2223
function isValidActionsMapValue(actionsMapValue) {
@@ -32,12 +33,11 @@ function isValidActionsMapValue(actionsMapValue) {
3233

3334
function 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

4949
function 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

Comments
 (0)