-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.ts
45 lines (40 loc) · 1.12 KB
/
regex.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
export interface Regex {
validate: RegExp;
removal: RegExp;
}
/*
TODO all of the regexes currently follow:
Match: /https\?:\\\/\\\/([\w.]+)\\\/\[\^ "'`\]\+\/g
Replace: "$1"
All cases follow similar of not exactly the same removal case. When more regexes are added, if they all follow this pattern, it may be possible to easily update new registries that follow this.
*/
export const regexes: Regex[] = [
{
validate: /https?:\/\/deno.land\/[^ "'`]+/g,
removal: /@[\w\d+\.]+/g,
},
{
validate: /https?:\/\/esm.sh\/[^ "'`]+/g,
removal: /@[\d\.]+/g,
},
{
validate: /https?:\/\/cdn.jsdelivr.net\/[^ "'`]+/g,
removal: /@[\d\.]+/g,
},
{
validate: /https?:\/\/unpkg.com\/[^ "'`]+/g,
removal: /@[\w\d+\.]+/g,
},
];
/**
* Applies a URL regex to a URL. Finds the first match of the validate regexp and removes any matches of the removal regexp.
* @param url The URL to apply it on
*/
export function apply(url: string): string | undefined {
for (const regex of regexes) {
if (regex && regex.validate.test(url)) {
return url.replace(regex.removal, "");
}
}
return undefined;
}