Skip to content

Commit 994fdb2

Browse files
committed
Add support for an array of keys πŸ—
1 parent ccb66f9 commit 994fdb2

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

β€ŽREADME.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ A quick way to wrap your Redux actions in a namespace.
44

55
When dealing with larger Redux applications, this can help avoid generic
66
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.
99

1010
## Install
1111

@@ -18,18 +18,26 @@ npm install @bufferapp/keywrapper
1818
```js
1919
import keyWrapper from '@bufferapp/keywrapper'
2020

21+
// Use an Object...
2122
const actions = keyWrapper('QUEUE', {
2223
POST_DELETE: 0,
2324
POST_SHARE_NOW: 0
2425
})
2526

27+
// ...or an Array
28+
const otherActions = keyWrapper('QUEUE', [
29+
'POST_DELETE',
30+
'POST_SHARE_NOW'
31+
])
32+
2633
console.log(actions)
2734
/*
2835
{
2936
POST_DELETE: 'QUEUE__POST_DELETE',
3037
POST_SHARE_NOW: 'QUEUE__POST_SHARE_NOW',
3138
}
3239
*/
40+
3341
```
3442

3543
## License

β€Žindex.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
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+
}, {})
77
}

β€Žindex.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@ const keyWrapper = require('./index')
33

44
const actionObject = {
55
CLICK_BUTTON: 0,
6-
ADDITEM: 0
6+
ADDITEM: 0,
77
}
88

99
const wrappedOne = keyWrapper('ONE', actionObject)
1010

1111
assert.equal(wrappedOne.CLICK_BUTTON, 'ONE__CLICK_BUTTON')
1212
assert.equal(wrappedOne.ADDITEM, 'ONE__ADDITEM')
1313

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+
1424
console.log('Tests passed!')

0 commit comments

Comments
Β (0)