Skip to content

Convert null payloadCreator to the identity #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { createAction } from 'redux-actions';

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

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

Example:

Expand Down
17 changes: 14 additions & 3 deletions src/__tests__/createAction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ describe('createAction()', () => {
});
});

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

wrongTypePayloadCreators.forEach(wrongTypePayloadCreator => {
expect(() => {
createAction(type, wrongTypePayloadCreator);
})
.to.throw(
Error,
'Expected payloadCreator to be a function or undefined'
'Expected payloadCreator to be a function, undefined or null'
);
});
});
Expand All @@ -48,6 +48,17 @@ describe('createAction()', () => {
expect(isFSA(action)).to.be.true;
});

it('uses identity function if payloadCreator is null', () => {
const actionCreator = createAction(type, null);
const foobar = { foo: 'bar' };
const action = actionCreator(foobar);
expect(action).to.deep.equal({
type,
payload: foobar
});
expect(isFSA(action)).to.be.true;
});

it('accepts a second parameter for adding meta to object', () => {
const actionCreator = createAction(type, undefined, ({ cid }) => ({ cid }));
const foobar = { foo: 'bar', cid: 5 };
Expand Down
11 changes: 8 additions & 3 deletions src/createAction.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import identity from 'lodash/identity';
import isFunction from 'lodash/isFunction';
import isUndefined from 'lodash/isUndefined';
import isNull from 'lodash/isNull';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd instead use isNil, so we check for null OR undefined.

Copy link
Contributor Author

@yangmillstheory yangmillstheory Nov 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined is coerced at the parameter level, so I don't think we need to check for it downstream. Let me know if I'm missing something.

Copy link
Member

@timche timche Nov 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, didn't see it 👍

import invariant from 'invariant';

export default function createAction(type, payloadCreator = identity, metaCreator) {
invariant(
isFunction(payloadCreator),
'Expected payloadCreator to be a function or undefined'
isFunction(payloadCreator) || isNull(payloadCreator),
'Expected payloadCreator to be a function, undefined or null'
);

const finalPayloadCreator = isNull(payloadCreator)
? identity
: payloadCreator;

const actionCreator = (...args) => {
const hasError = args[0] instanceof Error;

const action = {
type
};

const payload = hasError ? args[0] : payloadCreator(...args);
const payload = hasError ? args[0] : finalPayloadCreator(...args);
if (!isUndefined(payload)) {
action.payload = payload;
}
Expand Down