Skip to content

Commit

Permalink
Allow to fill meta prop of an action
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneslumpe committed Jul 8, 2015
1 parent 21cac4a commit e788a34
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/__tests__/createAction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import isPlainObject from 'lodash.isplainobject';
describe('createAction()', () => {
describe('resulting action creator', () => {
const type = 'TYPE';
const actionCreator = createAction(type, b => b);
const foobar = { foo: 'bar' };
const actionCreator = createAction(type, b => b, ({ cid }) => ({cid}));
const foobar = { foo: 'bar', cid: 5 };
const action = actionCreator(foobar);

it('returns plain object', () => {
Expand All @@ -19,14 +19,18 @@ describe('createAction()', () => {
it('has no extraneous keys', () => {
expect(action).to.deep.equal({
type,
payload: foobar
payload: foobar,
meta: {
cid: 5
}
});
});

it('uses identity function if actionCreator is not a function', () => {
it('uses identity function if actionCreator and/or metaCreator is not a function', () => {
expect(createAction(type)(foobar)).to.deep.equal({
type,
payload: foobar
payload: foobar,
meta: foobar
});
});
});
Expand Down
9 changes: 7 additions & 2 deletions src/createAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ function identity(t) {
return t;
}

export default function createAction(type, actionCreator) {
export default function createAction(type, actionCreator, metaCreator) {
const finalActionCreator = typeof actionCreator === 'function'
? actionCreator
: identity;

const finalMetaCreator = typeof metaCreator === 'function'
? metaCreator
: identity;

return (...args) => ({
type,
payload: finalActionCreator(...args)
payload: finalActionCreator(...args),
meta: finalMetaCreator(...args)
});
}

0 comments on commit e788a34

Please sign in to comment.