-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
matcherTree.ts
219 lines (177 loc) · 4.89 KB
/
matcherTree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { RouteRecordMatcher } from './pathMatcher'
import { comparePathParserScore } from './pathParserRanker'
type MatcherTree = {
add: (matcher: RouteRecordMatcher) => void
remove: (matcher: RouteRecordMatcher) => void
find: (path: string) => RouteRecordMatcher | undefined
toArray: () => RouteRecordMatcher[]
}
function normalizePath(path: string) {
// We match case-insensitively initially, then let the matcher check more rigorously
path = path.toUpperCase()
// TODO: Check more thoroughly whether this is really necessary
while (path.endsWith('/')) {
path = path.slice(0, -1)
}
return path
}
function chooseBestMatcher(
firstMatcher: RouteRecordMatcher | undefined,
secondMatcher: RouteRecordMatcher | undefined
) {
if (secondMatcher) {
if (
!firstMatcher ||
comparePathParserScore(firstMatcher, secondMatcher) > 0
) {
firstMatcher = secondMatcher
}
}
return firstMatcher
}
export function createMatcherTree(): MatcherTree {
const root = createMatcherNode()
const exactMatchers: Record<string, RouteRecordMatcher[]> =
Object.create(null)
return {
add(matcher) {
if (matcher.staticPath) {
const path = normalizePath(matcher.record.path)
exactMatchers[path] = exactMatchers[path] || []
insertMatcher(matcher, exactMatchers[path])
} else {
root.add(matcher)
}
},
remove(matcher) {
if (matcher.staticPath) {
const path = normalizePath(matcher.record.path)
if (exactMatchers[path]) {
// TODO: Remove array if length is zero
remove(matcher, exactMatchers[path])
}
} else {
root.remove(matcher)
}
},
find(path) {
const matchers = exactMatchers[normalizePath(path)]
return chooseBestMatcher(
matchers && matchers.find(matcher => matcher.re.test(path)),
root.find(path)
)
},
toArray() {
const arr = root.toArray()
for (const key in exactMatchers) {
arr.unshift(...exactMatchers[key])
}
return arr
},
}
}
function createMatcherNode(depth = 1): MatcherTree {
let segments: Record<string, MatcherTree> | null = null
let wildcards: RouteRecordMatcher[] | null = null
return {
add(matcher) {
const { staticTokens } = matcher
const myToken = staticTokens[depth - 1]?.toUpperCase()
if (myToken != null) {
if (!segments) {
segments = Object.create(null)
}
if (!segments![myToken]) {
segments![myToken] = createMatcherNode(depth + 1)
}
segments![myToken].add(matcher)
return
}
if (!wildcards) {
wildcards = []
}
insertMatcher(matcher, wildcards)
},
remove(matcher) {
// TODO: Remove any empty data structures
if (segments) {
const myToken = matcher.staticTokens[depth - 1]?.toUpperCase()
if (myToken != null) {
if (segments[myToken]) {
segments[myToken].remove(matcher)
return
}
}
}
if (wildcards) {
remove(matcher, wildcards)
}
},
find(path) {
const tokens = path.split('/')
const myToken = tokens[depth]
let matcher: RouteRecordMatcher | undefined
if (segments && myToken != null) {
const segmentMatcher = segments[myToken.toUpperCase()]
if (segmentMatcher) {
matcher = segmentMatcher.find(path)
}
}
if (wildcards) {
matcher = chooseBestMatcher(
matcher,
wildcards.find(matcher => matcher.re.test(path))
)
}
return matcher
},
toArray() {
const matchers: RouteRecordMatcher[] = []
for (const key in segments) {
// TODO: push may not scale well enough
matchers.push(...segments[key].toArray())
}
if (wildcards) {
matchers.push(...wildcards)
}
return matchers
},
}
}
function remove<T>(item: T, items: T[]) {
const index = items.indexOf(item)
if (index > -1) {
items.splice(index, 1)
}
}
function insertMatcher(
matcher: RouteRecordMatcher,
matchers: RouteRecordMatcher[]
) {
const index = findInsertionIndex(matcher, matchers)
matchers.splice(index, 0, matcher)
}
function findInsertionIndex(
matcher: RouteRecordMatcher,
matchers: RouteRecordMatcher[]
) {
let i = 0
while (
i < matchers.length &&
comparePathParserScore(matcher, matchers[i]) >= 0 &&
// Adding children with empty path should still appear before the parent
// https://github.com/vuejs/router/issues/1124
(matcher.record.path !== matchers[i].record.path ||
!isRecordChildOf(matcher, matchers[i]))
)
i++
return i
}
function isRecordChildOf(
record: RouteRecordMatcher,
parent: RouteRecordMatcher
): boolean {
return parent.children.some(
child => child === record || isRecordChildOf(record, child)
)
}