Skip to content

Commit e788a34

Browse files
committed
Allow to fill meta prop of an action
1 parent 21cac4a commit e788a34

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/__tests__/createAction-test.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import isPlainObject from 'lodash.isplainobject';
44
describe('createAction()', () => {
55
describe('resulting action creator', () => {
66
const type = 'TYPE';
7-
const actionCreator = createAction(type, b => b);
8-
const foobar = { foo: 'bar' };
7+
const actionCreator = createAction(type, b => b, ({ cid }) => ({cid}));
8+
const foobar = { foo: 'bar', cid: 5 };
99
const action = actionCreator(foobar);
1010

1111
it('returns plain object', () => {
@@ -19,14 +19,18 @@ describe('createAction()', () => {
1919
it('has no extraneous keys', () => {
2020
expect(action).to.deep.equal({
2121
type,
22-
payload: foobar
22+
payload: foobar,
23+
meta: {
24+
cid: 5
25+
}
2326
});
2427
});
2528

26-
it('uses identity function if actionCreator is not a function', () => {
29+
it('uses identity function if actionCreator and/or metaCreator is not a function', () => {
2730
expect(createAction(type)(foobar)).to.deep.equal({
2831
type,
29-
payload: foobar
32+
payload: foobar,
33+
meta: foobar
3034
});
3135
});
3236
});

src/createAction.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ function identity(t) {
22
return t;
33
}
44

5-
export default function createAction(type, actionCreator) {
5+
export default function createAction(type, actionCreator, metaCreator) {
66
const finalActionCreator = typeof actionCreator === 'function'
77
? actionCreator
88
: identity;
99

10+
const finalMetaCreator = typeof metaCreator === 'function'
11+
? metaCreator
12+
: identity;
13+
1014
return (...args) => ({
1115
type,
12-
payload: finalActionCreator(...args)
16+
payload: finalActionCreator(...args),
17+
meta: finalMetaCreator(...args)
1318
});
1419
}

0 commit comments

Comments
 (0)