Skip to content

Commit eb11ac0

Browse files
committed
Don't recheck already filtered params
1 parent 53ae890 commit eb11ac0

File tree

1 file changed

+33
-41
lines changed

1 file changed

+33
-41
lines changed

src/matcher.ts

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -71,67 +71,59 @@ export const getMatchResult = (
7171
location: Location,
7272
matcher: Matcher,
7373
): { key: string; name: string; params: Params } | undefined => {
74-
const { path: locationPath } = location;
75-
const { isArea, path: matcherPath } = matcher;
76-
7774
if (
78-
(!isArea && locationPath.length !== matcherPath.length) ||
79-
(isArea && locationPath.length < matcherPath.length)
75+
(!matcher.isArea && location.path.length !== matcher.path.length) ||
76+
(matcher.isArea && location.path.length < matcher.path.length)
8077
) {
8178
return;
8279
}
8380

8481
const pathParams: Params = {};
82+
const searchParams: Params = {};
8583

86-
for (let index = 0; index < matcherPath.length; index++) {
87-
const locationPart = locationPath[index];
88-
const matcherPart = matcherPath[index];
84+
for (let index = 0; index < matcher.path.length; index++) {
85+
const part = location.path[index];
86+
const test = matcher.path[index];
8987

90-
if (matcherPart == null) {
88+
if (test == null) {
9189
continue;
9290
}
9391

94-
if (typeof matcherPart === "string") {
95-
if (locationPart === matcherPart) {
92+
if (typeof test === "string") {
93+
if (part === test) {
9694
continue;
9795
} else {
9896
return;
9997
}
10098
}
10199

102-
if (locationPart == null) {
100+
if (part == null) {
103101
return;
104102
}
105103

106-
const { name, union } = matcherPart;
104+
const { name, union } = test;
107105

108-
if (union == null || union.includes(locationPart)) {
109-
pathParams[name] = locationPart;
106+
if (union == null || union.includes(part)) {
107+
pathParams[name] = part;
110108
} else {
111109
return;
112110
}
113111
}
114112

115-
const searchParams: Params = {};
116-
117113
for (const key in matcher.search) {
118114
if (Object.prototype.hasOwnProperty.call(matcher.search, key)) {
119-
const matcherPart = matcher.search[key];
120-
const locationPart = location.search[key];
115+
const part = location.search[key];
116+
const test = matcher.search[key];
121117

122-
if (matcherPart == null || locationPart == null) {
118+
if (part == null || test == null) {
123119
continue;
124120
}
125121

126-
const { multiple, union } = matcherPart;
127-
128-
const locationParts =
129-
typeof locationPart === "string" ? [locationPart] : locationPart;
122+
const { multiple, union } = test;
123+
const parts = typeof part === "string" ? [part] : part;
130124

131125
const values =
132-
union == null
133-
? locationParts
134-
: locationParts.filter((item) => union.includes(item));
126+
union == null ? parts : parts.filter((item) => union.includes(item));
135127

136128
if (multiple) {
137129
searchParams[key] = values;
@@ -140,7 +132,7 @@ export const getMatchResult = (
140132

141133
const value = values[0];
142134

143-
if (value != null && (union == null || union.includes(value))) {
135+
if (value != null) {
144136
searchParams[key] = value;
145137
}
146138
}
@@ -183,22 +175,22 @@ export const matchToUrl = (matcher: Matcher, params: Params = {}): string => {
183175
const object: Search = {};
184176

185177
for (const key in params) {
186-
const matcherPart = matcher.search[key];
187178
const param = params[key];
179+
const test = matcher.search[key];
188180

189-
if (matcherPart != null && param != null) {
190-
const { union } = matcherPart;
191-
192-
if (typeof param === "string") {
193-
if (union == null || union.includes(param)) {
194-
object[key] = param;
195-
}
196-
} else {
197-
object[key] =
198-
union == null
199-
? param
200-
: param.filter((item) => union.includes(item));
181+
if (param == null || test == null) {
182+
continue;
183+
}
184+
185+
const { union } = test;
186+
187+
if (typeof param === "string") {
188+
if (union == null || union.includes(param)) {
189+
object[key] = param;
201190
}
191+
} else {
192+
object[key] =
193+
union == null ? param : param.filter((item) => union.includes(item));
202194
}
203195
}
204196

0 commit comments

Comments
 (0)