From 68338db2bc909671a6d0ed943aa0480f725b5714 Mon Sep 17 00:00:00 2001 From: Thammi Date: Wed, 26 Feb 2020 16:19:57 +0100 Subject: [PATCH 1/3] Accept falsy initalValue --- src/index.js | 25 +++++++++++++++---------- test/index.test.js | 7 +++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index a2021e7..df7176e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ export default (...args) => { - const initialState = typeof args[0] !== 'function' && args.shift(); + const initialState = typeof args[0] !== 'function' ? args.shift() : null; const reducers = args; if (typeof initialState === 'undefined') { @@ -12,18 +12,23 @@ export default (...args) => { const prevStateIsUndefined = typeof prevState === 'undefined'; const valueIsUndefined = typeof value === 'undefined'; - if (prevStateIsUndefined && valueIsUndefined && initialState) { + if (prevStateIsUndefined && valueIsUndefined && initialState != null) { return initialState; } - return reducers.reduce((newState, reducer, index) => { - if (typeof reducer === 'undefined') { - throw new TypeError( - `An undefined reducer was passed in at index ${index}` - ); - } + return reducers.reduce( + (newState, reducer, index) => { + if (typeof reducer === 'undefined') { + throw new TypeError( + `An undefined reducer was passed in at index ${index}` + ); + } - return reducer(newState, value, ...args); - }, prevStateIsUndefined && !valueIsUndefined && initialState ? initialState : prevState); + return reducer(newState, value, ...args); + }, + prevStateIsUndefined && !valueIsUndefined && initialState != null + ? initialState + : prevState + ); }; }; diff --git a/test/index.test.js b/test/index.test.js index 0da5619..c1d6f92 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -20,6 +20,13 @@ test('passes `initialState` when state is `undefined` and value is defined', () expect(reducer(undefined, 1)).toEqual({ A: 1, B: 0 }); }); +test('passes falsy `initialState` when state is `undefined` and value is defined', () => { + const initialState = ''; + const reducer = reduceReducers(initialState, (state, payload) => state); + + expect(reducer(undefined, 1)).toEqual(''); +}); + test('throws an error if initialState is undefined', () => { expect(() => { reduceReducers(undefined); From 8bf8e2c4df102856e2566a866fd220c0a5dde35e Mon Sep 17 00:00:00 2001 From: Thammi Date: Wed, 26 Feb 2020 16:39:01 +0100 Subject: [PATCH 2/3] Minor stylistic change for check against initialValue --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index df7176e..f2362d6 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,7 @@ export default (...args) => { const prevStateIsUndefined = typeof prevState === 'undefined'; const valueIsUndefined = typeof value === 'undefined'; - if (prevStateIsUndefined && valueIsUndefined && initialState != null) { + if (prevStateIsUndefined && valueIsUndefined && initialState !== null) { return initialState; } @@ -26,7 +26,7 @@ export default (...args) => { return reducer(newState, value, ...args); }, - prevStateIsUndefined && !valueIsUndefined && initialState != null + prevStateIsUndefined && !valueIsUndefined && initialState !== null ? initialState : prevState ); From 10adc2558c8bf983432ffb705c056bff3f0704f4 Mon Sep 17 00:00:00 2001 From: Thammi Date: Wed, 26 Feb 2020 16:48:06 +0100 Subject: [PATCH 3/3] Make linter happy --- src/index.js | 2 +- test/index.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index f2362d6..d3e994f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ export default (...args) => { - const initialState = typeof args[0] !== 'function' ? args.shift() : null; + const initialState = typeof args[0] === 'function' ? null : args.shift(); const reducers = args; if (typeof initialState === 'undefined') { diff --git a/test/index.test.js b/test/index.test.js index c1d6f92..bd00811 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -22,7 +22,7 @@ test('passes `initialState` when state is `undefined` and value is defined', () test('passes falsy `initialState` when state is `undefined` and value is defined', () => { const initialState = ''; - const reducer = reduceReducers(initialState, (state, payload) => state); + const reducer = reduceReducers(initialState, state => state); expect(reducer(undefined, 1)).toEqual(''); });