Skip to content

Commit b575016

Browse files
committed
fix(prefixReducer): ignore non-string action.type
1 parent 49912d1 commit b575016

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/prefixReducer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import mapKeys from 'lodash.mapkeys'
22
import createReducer from './createReducer'
33

4-
export default function prefixReducerActionTypes(prefix) {
4+
export default function prefixReducer(prefix) {
55
return reducer => {
66
if (reducer.actionHandlers instanceof Object) {
77
return createReducer(reducer.initialState, mapKeys(reducer.actionHandlers, (handler, key) => prefix + key))
88
}
9-
return (state, action) => action.type.startsWith(prefix)
9+
return (state, action) => typeof action.type === 'string' && action.type.startsWith(prefix)
1010
? reducer(state, {...action, type: action.type.substring(prefix.length)})
1111
: state
1212
}

test/prefixReducerTest.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ describe('prefixReducer', () => {
2626
expect(reducer2(1, {type: 'TEST.DECREMENT'})).to.equal(0)
2727
expect(reducer2(1, {type: 'BLAH.DECREMENT'})).to.equal(1)
2828
})
29+
it('ignores actions without type', () => {
30+
const reducer = (state, action) => action.type === 'INCREMENT' ? state + 1 : state
31+
const reducer2 = prefixReducer('TEST.')(reducer)
32+
expect(reducer2(1, {})).to.equal(1)
33+
})
34+
it('ignores actions non-string type', () => {
35+
const reducer = (state, action) => action.type === 'INCREMENT' ? state + 1 : state
36+
const reducer2 = prefixReducer('TEST.')(reducer)
37+
expect(reducer2(1, {type: {}})).to.equal(1)
38+
})
2939
})
3040

3141

0 commit comments

Comments
 (0)