From e788a3448954fb87509478c21210d5205021b6b5 Mon Sep 17 00:00:00 2001 From: Johannes Lumpe Date: Wed, 8 Jul 2015 09:08:03 +0300 Subject: [PATCH 1/2] Allow to fill `meta` prop of an action --- src/__tests__/createAction-test.js | 14 +++++++++----- src/createAction.js | 9 +++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/__tests__/createAction-test.js b/src/__tests__/createAction-test.js index e9c39bda..72bf6be6 100644 --- a/src/__tests__/createAction-test.js +++ b/src/__tests__/createAction-test.js @@ -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', () => { @@ -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 }); }); }); diff --git a/src/createAction.js b/src/createAction.js index 278246e1..d7510797 100644 --- a/src/createAction.js +++ b/src/createAction.js @@ -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) }); } From 0b82c83bc64924cb0f32e6f28b3ee56a8518713d Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Mon, 13 Jul 2015 14:54:08 -0700 Subject: [PATCH 2/2] No meta by default --- src/__tests__/createAction-test.js | 38 ++++++++++++++++++++++-------- src/createAction.js | 17 ++++++------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/__tests__/createAction-test.js b/src/__tests__/createAction-test.js index 72bf6be6..80c457ab 100644 --- a/src/__tests__/createAction-test.js +++ b/src/__tests__/createAction-test.js @@ -4,33 +4,51 @@ import isPlainObject from 'lodash.isplainobject'; describe('createAction()', () => { describe('resulting action creator', () => { const type = 'TYPE'; - const actionCreator = createAction(type, b => b, ({ cid }) => ({cid})); - const foobar = { foo: 'bar', cid: 5 }; - const action = actionCreator(foobar); it('returns plain object', () => { + const actionCreator = createAction(type, b => b); + const foobar = { foo: 'bar' }; + const action = actionCreator(foobar); expect(isPlainObject(action)).to.be.true; }); it('uses return value as payload', () => { + const actionCreator = createAction(type, b => b); + const foobar = { foo: 'bar' }; + const action = actionCreator(foobar); expect(action.payload).to.equal(foobar); }); it('has no extraneous keys', () => { + const actionCreator = createAction(type, b => b); + const foobar = { foo: 'bar' }; + const action = actionCreator(foobar); expect(action).to.deep.equal({ type, - payload: foobar, - meta: { - cid: 5 - } + payload: foobar }); }); - it('uses identity function if actionCreator and/or metaCreator is not a function', () => { - expect(createAction(type)(foobar)).to.deep.equal({ + it('uses identity function if actionCreator is not a function', () => { + const actionCreator = createAction(type); + const foobar = { foo: 'bar' }; + const action = actionCreator(foobar); + expect(action).to.deep.equal({ + type, + payload: foobar + }); + }); + + it('accepts a second parameter for adding meta to object', () => { + const actionCreator = createAction(type, null, ({ cid }) => ({ cid })); + const foobar = { foo: 'bar', cid: 5 }; + const action = actionCreator(foobar); + expect(action).to.deep.equal({ type, payload: foobar, - meta: foobar + meta: { + cid: 5 + } }); }); }); diff --git a/src/createAction.js b/src/createAction.js index d7510797..0fc59298 100644 --- a/src/createAction.js +++ b/src/createAction.js @@ -7,13 +7,14 @@ export default function createAction(type, actionCreator, metaCreator) { ? actionCreator : identity; - const finalMetaCreator = typeof metaCreator === 'function' - ? metaCreator - : identity; + return (...args) => { + const action = { + type, + payload: finalActionCreator(...args) + }; + + if (typeof metaCreator === 'function') action.meta = metaCreator(...args); - return (...args) => ({ - type, - payload: finalActionCreator(...args), - meta: finalMetaCreator(...args) - }); + return action; + }; }