Skip to content

Commit e84cd27

Browse files
committed
feat(addMeta): decorate actions as well as action creators
1 parent 8707973 commit e84cd27

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ setConfigEntry('hello', 'world').type // CONFIG.SET_ENTRY
120120
import {addMeta} from 'mindfront-redux-utils';
121121
```
122122

123-
An action creator decorator that assigns additional properties to created actions' `meta`.
123+
An action or action creator decorator that assigns additional properties to actions' `meta`.
124124

125125
### Example
126126
```es6
@@ -134,9 +134,13 @@ function setEntry(key, value) {
134134
}
135135
}
136136

137-
const setConfigEntry = addMeta({domain: 'config'})(setEntry)
137+
const forConfigDomain = addMeta({domain: 'config'})
138+
139+
const setConfigEntry = forConfigDomain(setEntry)
138140

139141
setConfigEntry('hello', 'world').meta // {key: 'hello', domain: 'config'}
142+
143+
forConfigDomain(setEntry('hello', 'world')).meta // {key: 'hello', domain: 'config'}
140144
```
141145

142146

src/addMeta.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
export default function addMeta(meta) {
2-
return actionCreator => (...args) => {
3-
const action = actionCreator(...args)
2+
function addMetaToAction(action) {
43
return {
5-
...action,
4+
...action,
65
meta: {
7-
...action.meta,
8-
...meta
9-
}
6+
...action.meta,
7+
...meta,
8+
}
109
}
1110
}
11+
return actionOrCreator => actionOrCreator instanceof Function
12+
? (...args) => addMetaToAction(actionOrCreator(...args))
13+
: addMetaToAction(actionOrCreator)
1214
}
1315

test/addMetaTest.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,26 @@ describe('addMeta', () => {
4040
meta: {domain: 'config'},
4141
})
4242
})
43+
it('works directly on actions instead of action creators', () => {
44+
function setEntry(key, value) {
45+
return {
46+
type: 'SET_ENTRY',
47+
payload: value,
48+
meta: {key}
49+
}
50+
}
51+
52+
expect(addMeta({domain: 'config'})(setEntry('hello', 'world'))).to.deep.equal({
53+
type: 'SET_ENTRY',
54+
payload: 'world',
55+
meta: {key: 'hello', domain: 'config'},
56+
})
57+
58+
expect(addMeta({key: 'blah', domain: 'config'})(setEntry('hello', 'world'))).to.deep.equal({
59+
type: 'SET_ENTRY',
60+
payload: 'world',
61+
meta: {key: 'blah', domain: 'config'},
62+
})
63+
})
4364
})
4465

0 commit comments

Comments
 (0)