@@ -20,6 +20,22 @@ function normalizePath(path: string) {
20
20
return path
21
21
}
22
22
23
+ function chooseBestMatcher (
24
+ firstMatcher : RouteRecordMatcher | undefined ,
25
+ secondMatcher : RouteRecordMatcher | undefined
26
+ ) {
27
+ if ( secondMatcher ) {
28
+ if (
29
+ ! firstMatcher ||
30
+ comparePathParserScore ( firstMatcher , secondMatcher ) > 0
31
+ ) {
32
+ firstMatcher = secondMatcher
33
+ }
34
+ }
35
+
36
+ return firstMatcher
37
+ }
38
+
23
39
export function createMatcherTree ( ) : MatcherTree {
24
40
const root = createMatcherNode ( )
25
41
const exactMatchers : Record < string , RouteRecordMatcher [ ] > =
@@ -53,15 +69,10 @@ export function createMatcherTree(): MatcherTree {
53
69
find ( path ) {
54
70
const matchers = exactMatchers [ normalizePath ( path ) ]
55
71
56
- if ( matchers ) {
57
- for ( const matcher of matchers ) {
58
- if ( matcher . re . test ( path ) ) {
59
- return matcher
60
- }
61
- }
62
- }
63
-
64
- return root . find ( path )
72
+ return chooseBestMatcher (
73
+ matchers && matchers . find ( matcher => matcher . re . test ( path ) ) ,
74
+ root . find ( path )
75
+ )
65
76
} ,
66
77
67
78
toArray ( ) {
@@ -127,24 +138,24 @@ function createMatcherNode(depth = 1): MatcherTree {
127
138
find ( path ) {
128
139
const tokens = path . split ( '/' )
129
140
const myToken = tokens [ depth ]
141
+ let matcher : RouteRecordMatcher | undefined
130
142
131
143
if ( segments && myToken != null ) {
132
144
const segmentMatcher = segments [ myToken . toUpperCase ( ) ]
133
145
134
146
if ( segmentMatcher ) {
135
- const match = segmentMatcher . find ( path )
136
-
137
- if ( match ) {
138
- return match
139
- }
147
+ matcher = segmentMatcher . find ( path )
140
148
}
141
149
}
142
150
143
151
if ( wildcards ) {
144
- return wildcards . find ( matcher => matcher . re . test ( path ) )
152
+ matcher = chooseBestMatcher (
153
+ matcher ,
154
+ wildcards . find ( matcher => matcher . re . test ( path ) )
155
+ )
145
156
}
146
157
147
- return
158
+ return matcher
148
159
} ,
149
160
150
161
toArray ( ) {
0 commit comments