Skip to content

Commit df7a832

Browse files
committed
create prefixActionCreator
1 parent 37bfb1e commit df7a832

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,25 @@ reducer({}, {type: 'COUNTER_1.INCREMENT'}) // {counter1: 1}
8888
reducer({counter1: 3, counter2: 3}, {type: 'COUNTER_1.DECREMENT'}) // {counter1: 2, counter2: 3}
8989
reducer({counter1: 3, counter2: 3}, {type: 'COUNTER_2.INCREMENT'}) // {counter1: 3, counter2: 4}
9090

91+
## prefixActionCreator(prefix: string): (actionCreator: ActionCreator) => ActionCreator
92+
```js
93+
import {prefixActionCreator} from 'mindfront-redux-utils';
94+
```
95+
96+
An action creator decorator that prepends `prefix` to the `type` of the created actions.
97+
98+
### Example
99+
import {prefixActionCreator} from 'mindfront-redux-utils'
100+
101+
function setEntry(key, value) {
102+
return {
103+
type: 'SET_ENTRY',
104+
payload: value,
105+
meta: {key}
106+
}
107+
}
108+
109+
const setConfigEntry = prefixActionCreator('CONFIG.')(setEntry)
110+
111+
setConfigEntry('hello', 'world').type // CONFIG.SET_ENTRY
112+

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import createMiddleware from './createMiddleware'
66
import composeMiddleware from './composeMiddleware'
77
import createPluggableMiddleware from './createPluggableMiddleware'
88
import prefixReducer from './prefixReducer'
9+
import prefixActionCreator from './prefixActionCreator'
910

1011
export {
1112
createReducer,
@@ -14,5 +15,6 @@ export {
1415
composeMiddleware,
1516
createPluggableMiddleware,
1617
prefixReducer,
18+
prefixActionCreator,
1719
}
1820

src/prefixActionCreator.js

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

test/prefixActionCreatorTest.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {expect} from 'chai'
2+
import prefixActionCreator from '../src/prefixActionCreator'
3+
4+
describe('prefixActionCreator', () => {
5+
it('works', () => {
6+
function setEntry(key, value) {
7+
return {
8+
type: 'SET_ENTRY',
9+
payload: value,
10+
meta: {key}
11+
}
12+
}
13+
14+
expect(prefixActionCreator('TEST.')(setEntry)('hello', 'world')).to.deep.equal({
15+
type: 'TEST.SET_ENTRY',
16+
payload: 'world',
17+
meta: {key: 'hello'}
18+
})
19+
})
20+
})
21+
22+

0 commit comments

Comments
 (0)