Skip to content

Commit 114115b

Browse files
Convert null payloadCreator to the identity (#170)
Close #169
1 parent 6679a29 commit 114115b

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { createAction } from 'redux-actions';
2424

2525
Wraps an action creator so that its return value is the payload of a Flux Standard Action.
2626

27-
`payloadCreator` must be a function or `undefined`. If `payloadCreator` is `undefined`, the identity function is used.
27+
`payloadCreator` must be a function, `undefined`, or `null`. If `payloadCreator` is `undefined` or `null`, the identity function is used.
2828

2929
Example:
3030

src/__tests__/createAction-test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ describe('createAction()', () => {
2323
});
2424
});
2525

26-
it('should throw an error if payloadCreator is not a function or undefined', () => {
27-
const wrongTypePayloadCreators = [1, false, null, 'string', {}, []];
26+
it('should throw an error if payloadCreator is not a function, undefined, null', () => {
27+
const wrongTypePayloadCreators = [1, false, 'string', {}, []];
2828

2929
wrongTypePayloadCreators.forEach(wrongTypePayloadCreator => {
3030
expect(() => {
3131
createAction(type, wrongTypePayloadCreator);
3232
})
3333
.to.throw(
3434
Error,
35-
'Expected payloadCreator to be a function or undefined'
35+
'Expected payloadCreator to be a function, undefined or null'
3636
);
3737
});
3838
});
@@ -48,6 +48,17 @@ describe('createAction()', () => {
4848
expect(isFSA(action)).to.be.true;
4949
});
5050

51+
it('uses identity function if payloadCreator is null', () => {
52+
const actionCreator = createAction(type, null);
53+
const foobar = { foo: 'bar' };
54+
const action = actionCreator(foobar);
55+
expect(action).to.deep.equal({
56+
type,
57+
payload: foobar
58+
});
59+
expect(isFSA(action)).to.be.true;
60+
});
61+
5162
it('accepts a second parameter for adding meta to object', () => {
5263
const actionCreator = createAction(type, undefined, ({ cid }) => ({ cid }));
5364
const foobar = { foo: 'bar', cid: 5 };

src/createAction.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import identity from 'lodash/identity';
22
import isFunction from 'lodash/isFunction';
33
import isUndefined from 'lodash/isUndefined';
4+
import isNull from 'lodash/isNull';
45
import invariant from 'invariant';
56

67
export default function createAction(type, payloadCreator = identity, metaCreator) {
78
invariant(
8-
isFunction(payloadCreator),
9-
'Expected payloadCreator to be a function or undefined'
9+
isFunction(payloadCreator) || isNull(payloadCreator),
10+
'Expected payloadCreator to be a function, undefined or null'
1011
);
1112

13+
const finalPayloadCreator = isNull(payloadCreator)
14+
? identity
15+
: payloadCreator;
16+
1217
const actionCreator = (...args) => {
1318
const hasError = args[0] instanceof Error;
1419

1520
const action = {
1621
type
1722
};
1823

19-
const payload = hasError ? args[0] : payloadCreator(...args);
24+
const payload = hasError ? args[0] : finalPayloadCreator(...args);
2025
if (!isUndefined(payload)) {
2126
action.payload = payload;
2227
}

0 commit comments

Comments
 (0)