File tree Expand file tree Collapse file tree 3 files changed +35
-8
lines changed
Expand file tree Collapse file tree 3 files changed +35
-8
lines changed Original file line number Diff line number Diff line change @@ -120,7 +120,7 @@ setConfigEntry('hello', 'world').type // CONFIG.SET_ENTRY
120120import {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
139141setConfigEntry (' hello' , ' world' ).meta // {key: 'hello', domain: 'config'}
142+
143+ forConfigDomain (setEntry (' hello' , ' world' )).meta // {key: 'hello', domain: 'config'}
140144```
141145
142146
Original file line number Diff line number Diff line change 11export 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments