File tree Expand file tree Collapse file tree 3 files changed +26
-8
lines changed Expand file tree Collapse file tree 3 files changed +26
-8
lines changed Original file line number Diff line number Diff line change @@ -4,8 +4,8 @@ A quick way to wrap your Redux actions in a namespace.
4
4
5
5
When dealing with larger Redux applications, this can help avoid generic
6
6
action names from causing side effects. This will return an object with
7
- namespaced string constants given a string namespace and an object. See
8
- usage examples below for more info.
7
+ namespaced string constants given a string namespace and an Object or Array.
8
+ See usage examples below for more info.
9
9
10
10
## Install
11
11
@@ -18,18 +18,26 @@ npm install @bufferapp/keywrapper
18
18
``` js
19
19
import keyWrapper from ' @bufferapp/keywrapper'
20
20
21
+ // Use an Object...
21
22
const actions = keyWrapper (' QUEUE' , {
22
23
POST_DELETE : 0 ,
23
24
POST_SHARE_NOW : 0
24
25
})
25
26
27
+ // ...or an Array
28
+ const otherActions = keyWrapper (' QUEUE' , [
29
+ ' POST_DELETE' ,
30
+ ' POST_SHARE_NOW'
31
+ ])
32
+
26
33
console .log (actions)
27
34
/*
28
35
{
29
36
POST_DELETE: 'QUEUE__POST_DELETE',
30
37
POST_SHARE_NOW: 'QUEUE__POST_SHARE_NOW',
31
38
}
32
39
*/
40
+
33
41
```
34
42
35
43
## License
Original file line number Diff line number Diff line change 1
1
module . exports = function keyWrapper ( namespace , actions ) {
2
- return Object . keys ( actions )
3
- . reduce ( ( o , action ) => {
4
- o [ action ] = `${ namespace } __${ action } `
5
- return o
6
- } , { } )
2
+ const keys = Array . isArray ( actions ) ? actions : Object . keys ( actions )
3
+ return keys . reduce ( ( o , action ) => {
4
+ o [ action ] = `${ namespace } __${ action } `
5
+ return o
6
+ } , { } )
7
7
}
Original file line number Diff line number Diff line change @@ -3,12 +3,22 @@ const keyWrapper = require('./index')
3
3
4
4
const actionObject = {
5
5
CLICK_BUTTON : 0 ,
6
- ADDITEM : 0
6
+ ADDITEM : 0 ,
7
7
}
8
8
9
9
const wrappedOne = keyWrapper ( 'ONE' , actionObject )
10
10
11
11
assert . equal ( wrappedOne . CLICK_BUTTON , 'ONE__CLICK_BUTTON' )
12
12
assert . equal ( wrappedOne . ADDITEM , 'ONE__ADDITEM' )
13
13
14
+ const actionArray = [
15
+ 'CLICK_BUTTON' ,
16
+ 'ADDITEM' ,
17
+ ]
18
+
19
+ const wrappedTwo = keyWrapper ( 'TWO' , actionArray )
20
+
21
+ assert . equal ( wrappedTwo . CLICK_BUTTON , 'TWO__CLICK_BUTTON' )
22
+ assert . equal ( wrappedTwo . ADDITEM , 'TWO__ADDITEM' )
23
+
14
24
console . log ( 'Tests passed!' )
You canβt perform that action at this time.
0 commit comments