Skip to content

Commit 3e021de

Browse files
authored
Merge pull request #4386 from reduxjs/matcher-golf
2 parents f17e122 + bbb3f22 commit 3e021de

File tree

1 file changed

+10
-70
lines changed

1 file changed

+10
-70
lines changed

packages/toolkit/src/matchers.ts

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import type {
1212
} from './createAsyncThunk'
1313

1414
/** @public */
15-
export type ActionMatchingAnyOf<Matchers extends [...Matcher<any>[]]> =
15+
export type ActionMatchingAnyOf<Matchers extends Matcher<any>[]> =
1616
ActionFromMatcher<Matchers[number]>
1717

1818
/** @public */
19-
export type ActionMatchingAllOf<Matchers extends [...Matcher<any>[]]> =
19+
export type ActionMatchingAllOf<Matchers extends Matcher<any>[]> =
2020
UnionToIntersection<ActionMatchingAnyOf<Matchers>>
2121

2222
const matches = (matcher: Matcher<any>, action: any) => {
@@ -36,7 +36,7 @@ const matches = (matcher: Matcher<any>, action: any) => {
3636
*
3737
* @public
3838
*/
39-
export function isAnyOf<Matchers extends [...Matcher<any>[]]>(
39+
export function isAnyOf<Matchers extends Matcher<any>[]>(
4040
...matchers: Matchers
4141
) {
4242
return (action: any): action is ActionMatchingAnyOf<Matchers> => {
@@ -53,7 +53,7 @@ export function isAnyOf<Matchers extends [...Matcher<any>[]]>(
5353
*
5454
* @public
5555
*/
56-
export function isAllOf<Matchers extends [...Matcher<any>[]]>(
56+
export function isAllOf<Matchers extends Matcher<any>[]>(
5757
...matchers: Matchers
5858
) {
5959
return (action: any): action is ActionMatchingAllOf<Matchers> => {
@@ -136,18 +136,7 @@ export function isPending<
136136
return isPending()(asyncThunks[0])
137137
}
138138

139-
return (
140-
action: any,
141-
): action is PendingActionFromAsyncThunk<AsyncThunks[number]> => {
142-
// note: this type will be correct because we have at least 1 asyncThunk
143-
const matchers: [Matcher<any>, ...Matcher<any>[]] = asyncThunks.map(
144-
(asyncThunk) => asyncThunk.pending,
145-
) as any
146-
147-
const combinedMatcher = isAnyOf(...matchers)
148-
149-
return combinedMatcher(action)
150-
}
139+
return isAnyOf(...asyncThunks.map((asyncThunk) => asyncThunk.pending))
151140
}
152141

153142
export type UnknownAsyncThunkRejectedAction = ReturnType<
@@ -199,18 +188,7 @@ export function isRejected<
199188
return isRejected()(asyncThunks[0])
200189
}
201190

202-
return (
203-
action: any,
204-
): action is RejectedActionFromAsyncThunk<AsyncThunks[number]> => {
205-
// note: this type will be correct because we have at least 1 asyncThunk
206-
const matchers: [Matcher<any>, ...Matcher<any>[]] = asyncThunks.map(
207-
(asyncThunk) => asyncThunk.rejected,
208-
) as any
209-
210-
const combinedMatcher = isAnyOf(...matchers)
211-
212-
return combinedMatcher(action)
213-
}
191+
return isAnyOf(...asyncThunks.map((asyncThunk) => asyncThunk.rejected))
214192
}
215193

216194
export type UnknownAsyncThunkRejectedWithValueAction = ReturnType<
@@ -264,24 +242,14 @@ export function isRejectedWithValue<
264242
}
265243

266244
if (asyncThunks.length === 0) {
267-
return (action: any) => {
268-
const combinedMatcher = isAllOf(isRejected(...asyncThunks), hasFlag)
269-
270-
return combinedMatcher(action)
271-
}
245+
return isAllOf(isRejected(...asyncThunks), hasFlag)
272246
}
273247

274248
if (!isAsyncThunkArray(asyncThunks)) {
275249
return isRejectedWithValue()(asyncThunks[0])
276250
}
277251

278-
return (
279-
action: any,
280-
): action is RejectedActionFromAsyncThunk<AsyncThunks[number]> => {
281-
const combinedMatcher = isAllOf(isRejected(...asyncThunks), hasFlag)
282-
283-
return combinedMatcher(action)
284-
}
252+
return isAllOf(isRejected(...asyncThunks), hasFlag)
285253
}
286254

287255
export type UnknownAsyncThunkFulfilledAction = ReturnType<
@@ -333,18 +301,7 @@ export function isFulfilled<
333301
return isFulfilled()(asyncThunks[0])
334302
}
335303

336-
return (
337-
action: any,
338-
): action is FulfilledActionFromAsyncThunk<AsyncThunks[number]> => {
339-
// note: this type will be correct because we have at least 1 asyncThunk
340-
const matchers: [Matcher<any>, ...Matcher<any>[]] = asyncThunks.map(
341-
(asyncThunk) => asyncThunk.fulfilled,
342-
) as any
343-
344-
const combinedMatcher = isAnyOf(...matchers)
345-
346-
return combinedMatcher(action)
347-
}
304+
return isAnyOf(...asyncThunks.map((asyncThunk) => asyncThunk.fulfilled))
348305
}
349306

350307
export type UnknownAsyncThunkAction =
@@ -404,22 +361,5 @@ export function isAsyncThunkAction<
404361
return isAsyncThunkAction()(asyncThunks[0])
405362
}
406363

407-
return (
408-
action: any,
409-
): action is ActionsFromAsyncThunk<AsyncThunks[number]> => {
410-
// note: this type will be correct because we have at least 1 asyncThunk
411-
const matchers: [Matcher<any>, ...Matcher<any>[]] = [] as any
412-
413-
for (const asyncThunk of asyncThunks) {
414-
matchers.push(
415-
asyncThunk.pending,
416-
asyncThunk.rejected,
417-
asyncThunk.fulfilled,
418-
)
419-
}
420-
421-
const combinedMatcher = isAnyOf(...matchers)
422-
423-
return combinedMatcher(action)
424-
}
364+
return isAnyOf(...asyncThunks.flatMap(asyncThunk => [asyncThunk.pending, asyncThunk.rejected, asyncThunk.fulfilled]))
425365
}

0 commit comments

Comments
 (0)