quasiurl 1.0.3 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +75 -19
- package/index.ts +77 -24
- package/package.json +1 -1
- package/dist/tests.js +0 -127
- package/tests.ts +0 -122
package/dist/index.js
CHANGED
|
@@ -1,26 +1,82 @@
|
|
|
1
1
|
// index.ts
|
|
2
2
|
var QuasiURL = class {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
search;
|
|
3
|
+
_protocol = "";
|
|
4
|
+
_hostname = "";
|
|
5
|
+
_port = "";
|
|
6
|
+
_pathname = "";
|
|
7
|
+
_search = "";
|
|
8
|
+
_hash = "";
|
|
10
9
|
constructor(url) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.search = search;
|
|
20
|
-
this.hash = hash;
|
|
21
|
-
this.href = `${origin}${pathname}${search}${hash}`;
|
|
22
|
-
this.hostname = head?.[3] ?? "";
|
|
10
|
+
this.href = url;
|
|
11
|
+
}
|
|
12
|
+
get href() {
|
|
13
|
+
return `${this.origin}${this.pathname}${this.search}${this.hash}`;
|
|
14
|
+
}
|
|
15
|
+
set href(value) {
|
|
16
|
+
let head = value.match(/^((\w+:)?\/\/([^/:]+)(:(\d+))?)(\/.*|$)/);
|
|
17
|
+
let tail = value.match(/(\?[^#]+)?(#.+)?$/);
|
|
23
18
|
this.protocol = head?.[2] ?? "";
|
|
19
|
+
this.hostname = head?.[3] ?? "";
|
|
20
|
+
this.port = head?.[5] ?? "";
|
|
21
|
+
this.pathname = value.replace(/^(\w+:)?\/\/[^/:]+(:\d+)?/, "").replace(/\?.*$/, "").replace(/#.*$/, "");
|
|
22
|
+
this.search = tail?.[1] ?? "";
|
|
23
|
+
this.hash = tail?.[2] ?? "";
|
|
24
|
+
}
|
|
25
|
+
get protocol() {
|
|
26
|
+
return this._protocol;
|
|
27
|
+
}
|
|
28
|
+
set protocol(value) {
|
|
29
|
+
if (!value || /^\w+:$/.test(value)) this._protocol = value;
|
|
30
|
+
}
|
|
31
|
+
get hostname() {
|
|
32
|
+
return this._hostname;
|
|
33
|
+
}
|
|
34
|
+
set hostname(value) {
|
|
35
|
+
if (!value.includes("/")) this._hostname = value;
|
|
36
|
+
}
|
|
37
|
+
get port() {
|
|
38
|
+
return this._port;
|
|
39
|
+
}
|
|
40
|
+
set port(value) {
|
|
41
|
+
let s = String(value);
|
|
42
|
+
if (!s || /^\d+$/.test(s)) this._port = s;
|
|
43
|
+
}
|
|
44
|
+
get host() {
|
|
45
|
+
return `${this.hostname}${this.port ? `:${this.port}` : ""}`;
|
|
46
|
+
}
|
|
47
|
+
set host(value) {
|
|
48
|
+
let [hostname, port = ""] = value.split(":");
|
|
49
|
+
this.hostname = hostname;
|
|
50
|
+
this.port = port;
|
|
51
|
+
}
|
|
52
|
+
get origin() {
|
|
53
|
+
if (!this.protocol && !this.host) return "";
|
|
54
|
+
return `${this.protocol}//${this.host}`;
|
|
55
|
+
}
|
|
56
|
+
set origin(value) {
|
|
57
|
+
let [protocol, host = ""] = value.split("//");
|
|
58
|
+
this.protocol = protocol;
|
|
59
|
+
this.host = host;
|
|
60
|
+
}
|
|
61
|
+
get pathname() {
|
|
62
|
+
let p = this._pathname;
|
|
63
|
+
return `${p.startsWith("/") || p.startsWith("{/") ? "" : "/"}${p}`;
|
|
64
|
+
}
|
|
65
|
+
set pathname(value) {
|
|
66
|
+
this._pathname = value;
|
|
67
|
+
}
|
|
68
|
+
get search() {
|
|
69
|
+
return this._search && `?${this._search}`;
|
|
70
|
+
}
|
|
71
|
+
set search(value) {
|
|
72
|
+
let s = String(value);
|
|
73
|
+
this._search = s.startsWith("?") ? s.slice(1) : s;
|
|
74
|
+
}
|
|
75
|
+
get hash() {
|
|
76
|
+
return this._hash && `#${this._hash}`;
|
|
77
|
+
}
|
|
78
|
+
set hash(value) {
|
|
79
|
+
this._hash = value.startsWith("#") ? value.slice(1) : value;
|
|
24
80
|
}
|
|
25
81
|
toString() {
|
|
26
82
|
return this.href;
|
package/index.ts
CHANGED
|
@@ -4,34 +4,87 @@
|
|
|
4
4
|
* - preserves templating characters without URL-encoding them.
|
|
5
5
|
*/
|
|
6
6
|
export class QuasiURL {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
search: string;
|
|
14
|
-
|
|
7
|
+
_protocol = "";
|
|
8
|
+
_hostname = "";
|
|
9
|
+
_port = "";
|
|
10
|
+
_pathname = "";
|
|
11
|
+
_search = "";
|
|
12
|
+
_hash = "";
|
|
15
13
|
constructor(url: string) {
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
this.href = url;
|
|
15
|
+
}
|
|
16
|
+
get href() {
|
|
17
|
+
return `${this.origin}${this.pathname}${this.search}${this.hash}`;
|
|
18
|
+
}
|
|
19
|
+
set href(value: string) {
|
|
20
|
+
let head = value.match(/^((\w+:)?\/\/([^/:]+)(:(\d+))?)(\/.*|$)/);
|
|
21
|
+
let tail = value.match(/(\?[^#]+)?(#.+)?$/);
|
|
18
22
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
this.protocol = head?.[2] ?? "";
|
|
24
|
+
this.hostname = head?.[3] ?? "";
|
|
25
|
+
this.port = head?.[5] ?? "";
|
|
26
|
+
this.pathname = value
|
|
27
|
+
.replace(/^(\w+:)?\/\/[^/:]+(:\d+)?/, "")
|
|
22
28
|
.replace(/\?.*$/, "")
|
|
23
29
|
.replace(/#.*$/, "");
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
this.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.
|
|
30
|
+
this.search = tail?.[1] ?? "";
|
|
31
|
+
this.hash = tail?.[2] ?? "";
|
|
32
|
+
}
|
|
33
|
+
get protocol() {
|
|
34
|
+
return this._protocol;
|
|
35
|
+
}
|
|
36
|
+
set protocol(value: string) {
|
|
37
|
+
if (!value || /^\w+:$/.test(value)) this._protocol = value;
|
|
38
|
+
}
|
|
39
|
+
get hostname() {
|
|
40
|
+
return this._hostname;
|
|
41
|
+
}
|
|
42
|
+
set hostname(value: string) {
|
|
43
|
+
if (!value.includes("/")) this._hostname = value;
|
|
44
|
+
}
|
|
45
|
+
get port(): string {
|
|
46
|
+
return this._port;
|
|
47
|
+
}
|
|
48
|
+
set port(value: string | number) {
|
|
49
|
+
let s = String(value);
|
|
50
|
+
if (!s || /^\d+$/.test(s)) this._port = s;
|
|
51
|
+
}
|
|
52
|
+
get host() {
|
|
53
|
+
return `${this.hostname}${this.port ? `:${this.port}` : ""}`;
|
|
54
|
+
}
|
|
55
|
+
set host(value: string) {
|
|
56
|
+
let [hostname, port = ""] = value.split(":");
|
|
57
|
+
this.hostname = hostname;
|
|
58
|
+
this.port = port;
|
|
59
|
+
}
|
|
60
|
+
get origin() {
|
|
61
|
+
if (!this.protocol && !this.host) return "";
|
|
62
|
+
return `${this.protocol}//${this.host}`;
|
|
63
|
+
}
|
|
64
|
+
set origin(value: string) {
|
|
65
|
+
let [protocol, host = ""] = value.split("//");
|
|
66
|
+
this.protocol = protocol;
|
|
67
|
+
this.host = host;
|
|
68
|
+
}
|
|
69
|
+
get pathname() {
|
|
70
|
+
let p = this._pathname;
|
|
71
|
+
return `${p.startsWith("/") || p.startsWith("{/") ? "" : "/"}${p}`;
|
|
72
|
+
}
|
|
73
|
+
set pathname(value: string) {
|
|
74
|
+
this._pathname = value;
|
|
75
|
+
}
|
|
76
|
+
get search(): string {
|
|
77
|
+
return this._search && `?${this._search}`;
|
|
78
|
+
}
|
|
79
|
+
set search(value: string | URLSearchParams) {
|
|
80
|
+
let s = String(value);
|
|
81
|
+
this._search = s.startsWith("?") ? s.slice(1) : s;
|
|
82
|
+
}
|
|
83
|
+
get hash() {
|
|
84
|
+
return this._hash && `#${this._hash}`;
|
|
85
|
+
}
|
|
86
|
+
set hash(value: string) {
|
|
87
|
+
this._hash = value.startsWith("#") ? value.slice(1) : value;
|
|
35
88
|
}
|
|
36
89
|
toString() {
|
|
37
90
|
return this.href;
|
package/package.json
CHANGED
package/dist/tests.js
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
// index.ts
|
|
2
|
-
var QuasiURL = class {
|
|
3
|
-
hash;
|
|
4
|
-
hostname;
|
|
5
|
-
href;
|
|
6
|
-
origin;
|
|
7
|
-
pathname;
|
|
8
|
-
protocol;
|
|
9
|
-
search;
|
|
10
|
-
constructor(url2) {
|
|
11
|
-
let head = url2.match(/^((\w+:)?\/\/([^/]+))(\/.*|$)/);
|
|
12
|
-
let tail = url2.match(/(\?[^#]+)(#.+)?$/);
|
|
13
|
-
let origin = head?.[1] ?? "";
|
|
14
|
-
let pathname = url2.replace(/^((\w+:)?\/\/[^/]+)/, "").replace(/\?.*$/, "").replace(/#.*$/, "");
|
|
15
|
-
let search = tail?.[1] ?? "";
|
|
16
|
-
let hash = tail?.[2] ?? "";
|
|
17
|
-
this.origin = origin;
|
|
18
|
-
this.pathname = pathname;
|
|
19
|
-
this.search = search;
|
|
20
|
-
this.hash = hash;
|
|
21
|
-
this.href = `${origin}${pathname}${search}${hash}`;
|
|
22
|
-
this.hostname = head?.[3] ?? "";
|
|
23
|
-
this.protocol = head?.[2] ?? "";
|
|
24
|
-
}
|
|
25
|
-
toString() {
|
|
26
|
-
return this.href;
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
// tests.ts
|
|
31
|
-
var k = 0;
|
|
32
|
-
function assert(predicate) {
|
|
33
|
-
let n = `00${++k}`.slice(-3);
|
|
34
|
-
if (predicate) console.log(n, "passed");
|
|
35
|
-
else {
|
|
36
|
-
console.error(n, "failed");
|
|
37
|
-
console.error();
|
|
38
|
-
console.error("[!] failed");
|
|
39
|
-
process.exit(1);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function createQuasiURL(url2) {
|
|
43
|
-
console.log(`
|
|
44
|
-
${JSON.stringify(url2)}`);
|
|
45
|
-
return new QuasiURL(url2);
|
|
46
|
-
}
|
|
47
|
-
var url = createQuasiURL("/x");
|
|
48
|
-
assert(url.origin === "");
|
|
49
|
-
assert(url.pathname === "/x");
|
|
50
|
-
assert(url.search === "");
|
|
51
|
-
assert(url.hash === "");
|
|
52
|
-
assert(url.href === "/x");
|
|
53
|
-
assert(url.hostname === "");
|
|
54
|
-
assert(url.protocol === "");
|
|
55
|
-
url = createQuasiURL("/sections/:id");
|
|
56
|
-
assert(url.origin === "");
|
|
57
|
-
assert(url.pathname === "/sections/:id");
|
|
58
|
-
assert(url.search === "");
|
|
59
|
-
assert(url.hash === "");
|
|
60
|
-
assert(url.href === "/sections/:id");
|
|
61
|
-
assert(url.hostname === "");
|
|
62
|
-
assert(url.protocol === "");
|
|
63
|
-
url = createQuasiURL("/x{/:name}");
|
|
64
|
-
assert(url.origin === "");
|
|
65
|
-
assert(url.pathname === "/x{/:name}");
|
|
66
|
-
assert(url.search === "");
|
|
67
|
-
assert(url.hash === "");
|
|
68
|
-
assert(url.href === "/x{/:name}");
|
|
69
|
-
assert(url.hostname === "");
|
|
70
|
-
assert(url.protocol === "");
|
|
71
|
-
url = createQuasiURL("https://a.aa/x{/:name}");
|
|
72
|
-
assert(url.origin === "https://a.aa");
|
|
73
|
-
assert(url.pathname === "/x{/:name}");
|
|
74
|
-
assert(url.search === "");
|
|
75
|
-
assert(url.hash === "");
|
|
76
|
-
assert(url.href === "https://a.aa/x{/:name}");
|
|
77
|
-
assert(url.hostname === "a.aa");
|
|
78
|
-
assert(url.protocol === "https:");
|
|
79
|
-
url = createQuasiURL("/x?a=1&b=nnn#start");
|
|
80
|
-
assert(url.origin === "");
|
|
81
|
-
assert(url.pathname === "/x");
|
|
82
|
-
assert(url.search === "?a=1&b=nnn");
|
|
83
|
-
assert(url.hash === "#start");
|
|
84
|
-
assert(url.href === "/x?a=1&b=nnn#start");
|
|
85
|
-
assert(url.hostname === "");
|
|
86
|
-
assert(url.protocol === "");
|
|
87
|
-
url = createQuasiURL("https://a.aa/chapter/1");
|
|
88
|
-
assert(url.origin === "https://a.aa");
|
|
89
|
-
assert(url.pathname === "/chapter/1");
|
|
90
|
-
assert(url.search === "");
|
|
91
|
-
assert(url.hash === "");
|
|
92
|
-
assert(url.href === "https://a.aa/chapter/1");
|
|
93
|
-
assert(url.hostname === "a.aa");
|
|
94
|
-
assert(url.protocol === "https:");
|
|
95
|
-
url = createQuasiURL("https://a.aa");
|
|
96
|
-
assert(url.origin === "https://a.aa");
|
|
97
|
-
assert(url.pathname === "");
|
|
98
|
-
assert(url.search === "");
|
|
99
|
-
assert(url.hash === "");
|
|
100
|
-
assert(url.href === "https://a.aa");
|
|
101
|
-
assert(url.hostname === "a.aa");
|
|
102
|
-
assert(url.protocol === "https:");
|
|
103
|
-
url = createQuasiURL("");
|
|
104
|
-
assert(url.origin === "");
|
|
105
|
-
assert(url.pathname === "");
|
|
106
|
-
assert(url.search === "");
|
|
107
|
-
assert(url.hash === "");
|
|
108
|
-
assert(url.href === "");
|
|
109
|
-
assert(url.hostname === "");
|
|
110
|
-
assert(url.protocol === "");
|
|
111
|
-
url = createQuasiURL("?x=321&y=567#start");
|
|
112
|
-
assert(url.origin === "");
|
|
113
|
-
assert(url.pathname === "");
|
|
114
|
-
assert(url.search === "?x=321&y=567");
|
|
115
|
-
assert(url.hash === "#start");
|
|
116
|
-
assert(url.href === "?x=321&y=567#start");
|
|
117
|
-
assert(url.hostname === "");
|
|
118
|
-
assert(url.protocol === "");
|
|
119
|
-
url = createQuasiURL("x/y?a=1");
|
|
120
|
-
assert(url.origin === "");
|
|
121
|
-
assert(url.pathname === "x/y");
|
|
122
|
-
assert(url.search === "?a=1");
|
|
123
|
-
assert(url.hash === "");
|
|
124
|
-
assert(url.href === "x/y?a=1");
|
|
125
|
-
assert(url.hostname === "");
|
|
126
|
-
assert(url.protocol === "");
|
|
127
|
-
console.log("\npassed");
|
package/tests.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { QuasiURL } from ".";
|
|
2
|
-
|
|
3
|
-
let k = 0;
|
|
4
|
-
|
|
5
|
-
function assert(predicate: boolean) {
|
|
6
|
-
let n = `00${++k}`.slice(-3);
|
|
7
|
-
|
|
8
|
-
if (predicate) console.log(n, "passed");
|
|
9
|
-
else {
|
|
10
|
-
console.error(n, "failed");
|
|
11
|
-
console.error();
|
|
12
|
-
console.error("[!] failed");
|
|
13
|
-
process.exit(1);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function createQuasiURL(url: string) {
|
|
18
|
-
console.log(`\n${JSON.stringify(url)}`);
|
|
19
|
-
return new QuasiURL(url);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
let url = createQuasiURL("/x");
|
|
23
|
-
|
|
24
|
-
assert(url.origin === "");
|
|
25
|
-
assert(url.pathname === "/x");
|
|
26
|
-
assert(url.search === "");
|
|
27
|
-
assert(url.hash === "");
|
|
28
|
-
assert(url.href === "/x");
|
|
29
|
-
assert(url.hostname === "");
|
|
30
|
-
assert(url.protocol === "");
|
|
31
|
-
|
|
32
|
-
url = createQuasiURL("/sections/:id");
|
|
33
|
-
|
|
34
|
-
assert(url.origin === "");
|
|
35
|
-
assert(url.pathname === "/sections/:id");
|
|
36
|
-
assert(url.search === "");
|
|
37
|
-
assert(url.hash === "");
|
|
38
|
-
assert(url.href === "/sections/:id");
|
|
39
|
-
assert(url.hostname === "");
|
|
40
|
-
assert(url.protocol === "");
|
|
41
|
-
|
|
42
|
-
url = createQuasiURL("/x{/:name}");
|
|
43
|
-
|
|
44
|
-
assert(url.origin === "");
|
|
45
|
-
assert(url.pathname === "/x{/:name}");
|
|
46
|
-
assert(url.search === "");
|
|
47
|
-
assert(url.hash === "");
|
|
48
|
-
assert(url.href === "/x{/:name}");
|
|
49
|
-
assert(url.hostname === "");
|
|
50
|
-
assert(url.protocol === "");
|
|
51
|
-
|
|
52
|
-
url = createQuasiURL("https://a.aa/x{/:name}");
|
|
53
|
-
|
|
54
|
-
assert(url.origin === "https://a.aa");
|
|
55
|
-
assert(url.pathname === "/x{/:name}");
|
|
56
|
-
assert(url.search === "");
|
|
57
|
-
assert(url.hash === "");
|
|
58
|
-
assert(url.href === "https://a.aa/x{/:name}");
|
|
59
|
-
assert(url.hostname === "a.aa");
|
|
60
|
-
assert(url.protocol === "https:");
|
|
61
|
-
|
|
62
|
-
url = createQuasiURL("/x?a=1&b=nnn#start");
|
|
63
|
-
|
|
64
|
-
assert(url.origin === "");
|
|
65
|
-
assert(url.pathname === "/x");
|
|
66
|
-
assert(url.search === "?a=1&b=nnn");
|
|
67
|
-
assert(url.hash === "#start");
|
|
68
|
-
assert(url.href === "/x?a=1&b=nnn#start");
|
|
69
|
-
assert(url.hostname === "");
|
|
70
|
-
assert(url.protocol === "");
|
|
71
|
-
|
|
72
|
-
url = createQuasiURL("https://a.aa/chapter/1");
|
|
73
|
-
|
|
74
|
-
assert(url.origin === "https://a.aa");
|
|
75
|
-
assert(url.pathname === "/chapter/1");
|
|
76
|
-
assert(url.search === "");
|
|
77
|
-
assert(url.hash === "");
|
|
78
|
-
assert(url.href === "https://a.aa/chapter/1");
|
|
79
|
-
assert(url.hostname === "a.aa");
|
|
80
|
-
assert(url.protocol === "https:");
|
|
81
|
-
|
|
82
|
-
url = createQuasiURL("https://a.aa");
|
|
83
|
-
|
|
84
|
-
assert(url.origin === "https://a.aa");
|
|
85
|
-
assert(url.pathname === "");
|
|
86
|
-
assert(url.search === "");
|
|
87
|
-
assert(url.hash === "");
|
|
88
|
-
assert(url.href === "https://a.aa");
|
|
89
|
-
assert(url.hostname === "a.aa");
|
|
90
|
-
assert(url.protocol === "https:");
|
|
91
|
-
|
|
92
|
-
url = createQuasiURL("");
|
|
93
|
-
|
|
94
|
-
assert(url.origin === "");
|
|
95
|
-
assert(url.pathname === "");
|
|
96
|
-
assert(url.search === "");
|
|
97
|
-
assert(url.hash === "");
|
|
98
|
-
assert(url.href === "");
|
|
99
|
-
assert(url.hostname === "");
|
|
100
|
-
assert(url.protocol === "");
|
|
101
|
-
|
|
102
|
-
url = createQuasiURL("?x=321&y=567#start");
|
|
103
|
-
|
|
104
|
-
assert(url.origin === "");
|
|
105
|
-
assert(url.pathname === "");
|
|
106
|
-
assert(url.search === "?x=321&y=567");
|
|
107
|
-
assert(url.hash === "#start");
|
|
108
|
-
assert(url.href === "?x=321&y=567#start");
|
|
109
|
-
assert(url.hostname === "");
|
|
110
|
-
assert(url.protocol === "");
|
|
111
|
-
|
|
112
|
-
url = createQuasiURL("x/y?a=1");
|
|
113
|
-
|
|
114
|
-
assert(url.origin === "");
|
|
115
|
-
assert(url.pathname === "x/y");
|
|
116
|
-
assert(url.search === "?a=1");
|
|
117
|
-
assert(url.hash === "");
|
|
118
|
-
assert(url.href === "x/y?a=1");
|
|
119
|
-
assert(url.hostname === "");
|
|
120
|
-
assert(url.protocol === "");
|
|
121
|
-
|
|
122
|
-
console.log("\npassed");
|