-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.ts
41 lines (35 loc) · 1.01 KB
/
module.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
const moduleRegex = /([^@]+)@([^/]+)\/?/;
export class Module {
constructor(
public readonly protocol: string,
public readonly path: string,
public version: string,
public files: string[],
) {}
toString(): string {
return `${this.protocol}://${this.path}`;
}
toStringWithVersion(): string {
return `${this.toString()}@${this.version}`;
}
static parse = (name: string): Module => {
const u = new URL(name);
const { protocol, hostname, pathname } = u;
const result = pathname.match(moduleRegex);
let path = "";
let version = "";
if (result) {
path = hostname + result[1];
version = result[2];
} else {
path = hostname + pathname;
}
return new Module(protocol.replace(/\:$/, ""), path, version, []);
};
}
export function compareModules(modA: Module, modB: Module): number {
return modA.path.localeCompare(modB.path);
}
export function moduleEquals(a: Module, b: Module): boolean {
return a.protocol === b.protocol && a.path === b.path;
}