-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05ba9d1
commit 31f9767
Showing
4 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { NoInfer } from './createStore' | ||
import type { Action } from './types/actions' | ||
import type { Reducer } from './types/reducers' | ||
|
||
/** | ||
* Composes multiple reducers into one. | ||
* | ||
* @param initialState The initial state, which can be a different preloaded state. | ||
* @param reducer The first reducer. Can accept a different preloaded state. | ||
* @param reducers The rest of the reducers. | ||
* @returns A reducer function that invokes every reducer passed in order, and returns the result of the last reducer. | ||
*/ | ||
export default function reduceReducers< | ||
S, | ||
A extends Action, | ||
Actions extends Action[], | ||
P | ||
>( | ||
initialState: NoInfer<P | S> | undefined, | ||
reducer: Reducer<S, A, P>, | ||
...reducers: { | ||
[K in keyof Actions]: Reducer<S, Actions[K]> | ||
} | ||
): Reducer<S, A | Actions[number], P> | ||
/** | ||
* Composes multiple reducers into one. | ||
* | ||
* @param reducer The first reducer. Can accept a different preloaded state. | ||
* @param reducers The rest of the reducers. | ||
* @returns A reducer function that invokes every reducer passed in order, and returns the result of the last reducer. | ||
*/ | ||
export default function reduceReducers< | ||
S, | ||
A extends Action, | ||
Actions extends Action[], | ||
P | ||
>( | ||
reducer: Reducer<S, A, P>, | ||
...reducers: { | ||
[K in keyof Actions]: Reducer<S, Actions[K]> | ||
} | ||
): Reducer<S, A | Actions[number], P> | ||
export default function reduceReducers<S, A extends Action, P>( | ||
...args: [P | S | undefined | Reducer<S, A, P>, ...Array<Reducer<S, A>>] | ||
): Reducer<S, A, P> { | ||
const initialState = | ||
typeof args[0] === 'function' | ||
? undefined | ||
: (args.shift() as P | S | undefined) | ||
const [firstReducer, ...restReducers] = args as [ | ||
Reducer<S, A, P>, | ||
...Reducer<S, A>[] | ||
] | ||
return (state = initialState, action) => | ||
restReducers.reduce( | ||
(state, reducer) => reducer(state, action), | ||
firstReducer(state, action) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import reduceReducers from "@internal/reduceReducers"; | ||
|
||
describe('Utils', () => { | ||
describe('reduceReducers', () => { | ||
const incrementReducer = (state = 0, action: { type: "increment" }) => | ||
action.type === 'increment' ? state + 1 : state | ||
const decrementReducer = (state = 0, action: { type: "decrement" }) => | ||
action.type === 'decrement' ? state - 1 : state | ||
|
||
it("runs multiple reducers in sequence and returns the result of the last one", () => { | ||
const combined = reduceReducers(incrementReducer, decrementReducer) | ||
expect(combined(0, { type: 'increment' })).toBe(1) | ||
expect(combined(1, { type: 'decrement' })).toBe(0) | ||
}) | ||
it("accepts an initial state argument", () => { | ||
const combined = reduceReducers(2, incrementReducer, decrementReducer) | ||
expect(combined(undefined, { type: "increment" })).toBe(3) | ||
}) | ||
it("can accept the preloaded state of the first reducer", () => { | ||
const parserReducer = (state: number | string = 0) => | ||
typeof state === 'string' ? parseInt(state, 10) : state | ||
|
||
const combined = reduceReducers(parserReducer, incrementReducer) | ||
expect(combined("1", { type: "increment"})).toBe(2) | ||
|
||
const combined2 = reduceReducers("1", parserReducer, incrementReducer) | ||
expect(combined2(undefined, { type: "increment"})).toBe(2) | ||
}) | ||
it("accepts undefined as initial state", () => { | ||
const combined = reduceReducers(undefined, incrementReducer) | ||
expect(combined(undefined, { type: "increment" })).toBe(1) | ||
}) | ||
}); | ||
}) |