Skip to content

Commit af7af4d

Browse files
committed
refactor(matcher): improve readability
1 parent c81d596 commit af7af4d

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

src/operations/match.ts

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,36 @@ function _matchAll<T>(
1818
method: string,
1919
segments: string[],
2020
index: number,
21+
matches: T[] = [],
2122
): T[] {
22-
const matchedNodes: T[] = [];
23-
2423
const segment = segments[index];
2524

26-
// 1. Node self data
27-
if (index === segments.length && node.methods) {
28-
const match = node.methods[method] || node.methods[""];
25+
// Wildcard
26+
if (node.wildcard && node.wildcard.methods) {
27+
const match = node.wildcard.methods[method] || node.wildcard.methods[""];
2928
if (match) {
30-
matchedNodes.unshift(match[0 /* data */]);
29+
matches.push(match[0 /* data */]);
3130
}
3231
}
3332

34-
// 2. Static
35-
const staticChild = node.static?.[segment];
36-
if (staticChild) {
37-
matchedNodes.unshift(
38-
..._matchAll(ctx, staticChild, method, segments, index + 1),
39-
);
40-
}
41-
42-
// 3. Param
33+
// Param
4334
if (node.param) {
44-
matchedNodes.unshift(
45-
..._matchAll(ctx, node.param, method, segments, index + 1),
46-
);
35+
_matchAll(ctx, node.param, method, segments, index + 1, matches);
4736
}
4837

49-
// 4. Wildcard
50-
if (node.wildcard && node.wildcard.methods) {
51-
const match = node.wildcard.methods[method] || node.wildcard.methods[""];
38+
// Node self data (only if we reached the end of the path)
39+
if (index === segments.length && node.methods) {
40+
const match = node.methods[method] || node.methods[""];
5241
if (match) {
53-
matchedNodes.unshift(match[0 /* data */]);
42+
matches.push(match[0 /* data */]);
5443
}
5544
}
5645

57-
// No match
58-
return matchedNodes;
46+
// Static
47+
const staticChild = node.static?.[segment];
48+
if (staticChild) {
49+
_matchAll(ctx, staticChild, method, segments, index + 1, matches);
50+
}
51+
52+
return matches;
5953
}

0 commit comments

Comments
 (0)