forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.js
More file actions
66 lines (51 loc) · 1.24 KB
/
url.js
File metadata and controls
66 lines (51 loc) · 1.24 KB
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
57
58
59
60
61
62
63
64
65
66
"use strict";
class Url {
constructor(url) {
if (url instanceof URL) {
item = url;
} else if (typeof url === "string") {
if (url.indexOf("//") === -1) {
url = "http://" + url;
}
} else {
this.href = url.href;
this.host = url.host;
this.path = url.path;
return;
}
var item = new URL(url);
this.href = item.href;
this.host = item.hostname;
this.path = item.pathname === "/" ? "" : item.pathname;
}
isMatch(url) {
if (!url) {
return false;
}
try {
url = url instanceof Url ? url : new Url(url);
} catch {
return false;
}
return this.isHostMatch(url.host) && this.isPathMatch(url.path);
}
isHostMatch(host) {
if (host === this.host) {
return true;
}
var thisHostParts = this.host.split(".").reverse();
var hostParts = host.split(".").reverse();
var result = thisHostParts.every((part, i) => hostParts[i] === part);
return result;
}
isPathMatch(path) {
var result = this.path === '' || path === this.path || path.indexOf(this.path) === 0;
return result;
}
getId() {
return this.host + this.path.replace(/\//g, "-");
}
toString() {
return this.host + this.path;
}
}