1 | // @ts-ignore
|
2 | export function routeMatcher(paths) {
|
3 | // EXAMPLE. For the following paths:
|
4 | /* [
|
5 | "/orgs/:org/invitations",
|
6 | "/repos/:owner/:repo/collaborators/:username"
|
7 | ] */
|
8 | // @ts-ignore
|
9 | const regexes = paths.map((path) => path
|
10 | .split("/")
|
11 | // @ts-ignore
|
12 | .map((c) => (c.startsWith(":") ? "(?:.+?)" : c))
|
13 | .join("/"));
|
14 | // 'regexes' would contain:
|
15 | /* [
|
16 | '/orgs/(?:.+?)/invitations',
|
17 | '/repos/(?:.+?)/(?:.+?)/collaborators/(?:.+?)'
|
18 | ] */
|
19 | // @ts-ignore
|
20 | const regex = `^(?:${regexes.map((r) => `(?:${r})`).join("|")})[^/]*$`;
|
21 | // 'regex' would contain:
|
22 | /*
|
23 | ^(?:(?:\/orgs\/(?:.+?)\/invitations)|(?:\/repos\/(?:.+?)\/(?:.+?)\/collaborators\/(?:.+?)))[^\/]*$
|
24 |
|
25 | It may look scary, but paste it into https://www.debuggex.com/
|
26 | and it will make a lot more sense!
|
27 | */
|
28 | return new RegExp(regex, "i");
|
29 | }
|