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