Skip to content

Commit dd8ef00

Browse files
committed
Add support for symbol action types
1 parent 344b8d1 commit dd8ef00

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/__tests__/handleActions-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ describe('handleActions', () => {
2222
});
2323
});
2424

25+
it('works with symbol action types', () => {
26+
const INCREMENT = Symbol();
27+
28+
const reducer = handleActions({
29+
[INCREMENT]: ({ counter }, { payload: amount }) => ({
30+
counter: counter + amount
31+
})
32+
});
33+
34+
expect(reducer({ counter: 3 }, { type: INCREMENT, payload: 7 }))
35+
.to.deep.equal({
36+
counter: 10
37+
});
38+
});
39+
2540
it('accepts a default state as the second parameter', () => {
2641
const reducer = handleActions({
2742
INCREMENT: ({ counter }, { payload: amount }) => ({

src/handleActions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import handleAction from './handleAction';
2+
import ownKeys from './ownKeys';
23
import reduceReducers from 'reduce-reducers';
34

45
export default function handleActions(handlers, defaultState) {
5-
const reducers = Object.keys(handlers).map(type => {
6+
const reducers = ownKeys(handlers).map(type => {
67
return handleAction(type, handlers[type]);
78
});
89

src/ownKeys.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default function ownKeys(object) {
2+
if (typeof Reflect !== 'undefined') {
3+
return Reflect.ownKeys(object);
4+
}
5+
6+
let keys = Object.getOwnPropertyNames(object);
7+
8+
if (typeof Object.getOwnPropertySymbols === 'function') {
9+
keys = keys.concat(Object.getOwnPropertySymbols(object));
10+
}
11+
12+
return keys;
13+
}

0 commit comments

Comments
 (0)