Skip to content

Commit 025de45

Browse files
committed
create addMeta
1 parent df7a832 commit 025de45

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,25 @@ const setConfigEntry = prefixActionCreator('CONFIG.')(setEntry)
110110

111111
setConfigEntry('hello', 'world').type // CONFIG.SET_ENTRY
112112

113+
## addMeta(meta: Object): (actionCreator: ActionCreator) => ActionCreator
114+
```js
115+
import {addMeta} from 'mindfront-redux-utils';
116+
```
117+
118+
An action creator decorator that assigns additional properties to created actions' `meta`.
119+
120+
### Example
121+
import {addMeta} from 'mindfront-redux-utils'
122+
123+
function setEntry(key, value) {
124+
return {
125+
type: 'SET_ENTRY',
126+
payload: value,
127+
meta: {key}
128+
}
129+
}
130+
131+
const setConfigEntry = addMeta({domain: 'config'})(setEntry)
132+
133+
setConfigEntry('hello', 'world').meta // {key: 'hello', domain: 'config'}
134+

src/addMeta.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default function addMeta(meta) {
2+
return actionCreator => (...args) => {
3+
const action = actionCreator(...args)
4+
return {
5+
...action,
6+
meta: {
7+
...action.meta,
8+
...meta
9+
}
10+
}
11+
}
12+
}
13+

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import composeMiddleware from './composeMiddleware'
77
import createPluggableMiddleware from './createPluggableMiddleware'
88
import prefixReducer from './prefixReducer'
99
import prefixActionCreator from './prefixActionCreator'
10+
import addMeta from './addMeta'
1011

1112
export {
1213
createReducer,
@@ -16,5 +17,6 @@ export {
1617
createPluggableMiddleware,
1718
prefixReducer,
1819
prefixActionCreator,
20+
addMeta,
1921
}
2022

test/addMetaTest.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {expect} from 'chai'
2+
import addMeta from '../src/addMeta'
3+
4+
describe('addMeta', () => {
5+
it('adds meta to created actions', () => {
6+
function setEntry(key, value) {
7+
return {
8+
type: 'SET_ENTRY',
9+
payload: value,
10+
meta: {key}
11+
}
12+
}
13+
14+
const setEntry2 = addMeta({domain: 'config'})(setEntry)
15+
expect(setEntry2('hello', 'world')).to.deep.equal({
16+
type: 'SET_ENTRY',
17+
payload: 'world',
18+
meta: {key: 'hello', domain: 'config'},
19+
})
20+
21+
const setEntry3 = addMeta({key: 'blah', domain: 'config'})(setEntry)
22+
expect(setEntry3('hello', 'world')).to.deep.equal({
23+
type: 'SET_ENTRY',
24+
payload: 'world',
25+
meta: {key: 'blah', domain: 'config'},
26+
})
27+
})
28+
it('works if the original action has no meta', () => {
29+
function setValue(value) {
30+
return {
31+
type: 'SET_VALUE',
32+
payload: value,
33+
}
34+
}
35+
36+
const setValue2 = addMeta({domain: 'config'})(setValue)
37+
expect(setValue2('hello')).to.deep.equal({
38+
type: 'SET_VALUE',
39+
payload: 'hello',
40+
meta: {domain: 'config'},
41+
})
42+
})
43+
})
44+

0 commit comments

Comments
 (0)