-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcapture-any.ts
56 lines (49 loc) · 1.55 KB
/
rcapture-any.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
import type { RouteMatcher } from '../route-matcher.js';
/**
* Builds a route matcher that matches any part of entry name and optionally captures its name.
*
* Reports the capture as {@link RouteCaptorSignatureMap.capture `capture`}.
*
* @param name - The name of the capture or nothing to capture under match index.
* @returns New route matcher.
*
* @see Use {@link rmatchAny} for anonymous capture.
*/
export function rcaptureAny(name?: string): RouteMatcher {
const key = name ?? 0;
return {
test(context): RouteMatcher.Match | undefined {
const { pattern, matcherIndex } = context;
const nextMatcher = pattern[matcherIndex + 1];
if (!nextMatcher || !nextMatcher.find) {
// This is the last matcher in pattern.
// Always match.
return {
callback: captor => captor(
'capture',
key,
context.entry.name.substring(context.nameOffset, context.entry.name.length),
context,
),
};
}
const found = nextMatcher.find({ ...context, matcherIndex: matcherIndex + 1 });
if (!found) {
return;
}
const { nameOffset } = context;
const [match, offset] = found;
return {
entries: context.route.path.length,
full: true,
callback:
offset > nameOffset
? captor => {
captor('capture', key, context.entry.name.substring(nameOffset, offset), context);
match(captor);
}
: match,
};
},
};
}