quasiurl 1.0.2 → 1.1.0
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/LICENSE +21 -0
- package/dist/index.js +75 -19
- package/dist/tests.js +179 -27
- package/index.ts +77 -24
- package/package.json +2 -2
- package/tests.ts +124 -8
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Alexander Tkačenko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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/dist/tests.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(url2) {
|
|
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 = url2;
|
|
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;
|
|
@@ -52,6 +108,43 @@ assert(url.hash === "");
|
|
|
52
108
|
assert(url.href === "/x");
|
|
53
109
|
assert(url.hostname === "");
|
|
54
110
|
assert(url.protocol === "");
|
|
111
|
+
console.log("\nset pathname");
|
|
112
|
+
url.pathname = "a/b";
|
|
113
|
+
assert(url.origin === "");
|
|
114
|
+
assert(url.pathname === "/a/b");
|
|
115
|
+
assert(url.search === "");
|
|
116
|
+
assert(url.hash === "");
|
|
117
|
+
assert(url.href === "/a/b");
|
|
118
|
+
assert(url.hostname === "");
|
|
119
|
+
assert(url.protocol === "");
|
|
120
|
+
console.log("\nset origin");
|
|
121
|
+
url.origin = "https://a.aa";
|
|
122
|
+
assert(url.origin === "https://a.aa");
|
|
123
|
+
assert(url.pathname === "/a/b");
|
|
124
|
+
assert(url.search === "");
|
|
125
|
+
assert(url.hash === "");
|
|
126
|
+
assert(url.href === "https://a.aa/a/b");
|
|
127
|
+
assert(url.hostname === "a.aa");
|
|
128
|
+
assert(url.protocol === "https:");
|
|
129
|
+
console.log("\nreset origin, set search");
|
|
130
|
+
url.origin = "";
|
|
131
|
+
url.search = "x=1";
|
|
132
|
+
assert(url.origin === "");
|
|
133
|
+
assert(url.pathname === "/a/b");
|
|
134
|
+
assert(url.search === "?x=1");
|
|
135
|
+
assert(url.hash === "");
|
|
136
|
+
assert(url.href === "/a/b?x=1");
|
|
137
|
+
assert(url.hostname === "");
|
|
138
|
+
assert(url.protocol === "");
|
|
139
|
+
console.log("\nset href");
|
|
140
|
+
url.href = "/x#intro";
|
|
141
|
+
assert(url.origin === "");
|
|
142
|
+
assert(url.pathname === "/x");
|
|
143
|
+
assert(url.search === "");
|
|
144
|
+
assert(url.hash === "#intro");
|
|
145
|
+
assert(url.href === "/x#intro");
|
|
146
|
+
assert(url.hostname === "");
|
|
147
|
+
assert(url.protocol === "");
|
|
55
148
|
url = createQuasiURL("/sections/:id");
|
|
56
149
|
assert(url.origin === "");
|
|
57
150
|
assert(url.pathname === "/sections/:id");
|
|
@@ -68,6 +161,24 @@ assert(url.hash === "");
|
|
|
68
161
|
assert(url.href === "/x{/:name}");
|
|
69
162
|
assert(url.hostname === "");
|
|
70
163
|
assert(url.protocol === "");
|
|
164
|
+
console.log("\nupdate path template");
|
|
165
|
+
url.pathname = "{/:name}";
|
|
166
|
+
assert(url.origin === "");
|
|
167
|
+
assert(url.pathname === "{/:name}");
|
|
168
|
+
assert(url.search === "");
|
|
169
|
+
assert(url.hash === "");
|
|
170
|
+
assert(url.href === "{/:name}");
|
|
171
|
+
assert(url.hostname === "");
|
|
172
|
+
assert(url.protocol === "");
|
|
173
|
+
console.log("\nset origin to path template");
|
|
174
|
+
url.origin = "https://a.aa";
|
|
175
|
+
assert(url.origin === "https://a.aa");
|
|
176
|
+
assert(url.pathname === "{/:name}");
|
|
177
|
+
assert(url.search === "");
|
|
178
|
+
assert(url.hash === "");
|
|
179
|
+
assert(url.href === "https://a.aa{/:name}");
|
|
180
|
+
assert(url.hostname === "a.aa");
|
|
181
|
+
assert(url.protocol === "https:");
|
|
71
182
|
url = createQuasiURL("https://a.aa/x{/:name}");
|
|
72
183
|
assert(url.origin === "https://a.aa");
|
|
73
184
|
assert(url.pathname === "/x{/:name}");
|
|
@@ -91,37 +202,78 @@ assert(url.search === "");
|
|
|
91
202
|
assert(url.hash === "");
|
|
92
203
|
assert(url.href === "https://a.aa/chapter/1");
|
|
93
204
|
assert(url.hostname === "a.aa");
|
|
205
|
+
assert(url.host === "a.aa");
|
|
206
|
+
assert(url.port === "");
|
|
207
|
+
assert(url.protocol === "https:");
|
|
208
|
+
console.log("\nset port");
|
|
209
|
+
url.port = 123;
|
|
210
|
+
assert(url.origin === "https://a.aa:123");
|
|
211
|
+
assert(url.pathname === "/chapter/1");
|
|
212
|
+
assert(url.search === "");
|
|
213
|
+
assert(url.hash === "");
|
|
214
|
+
assert(url.href === "https://a.aa:123/chapter/1");
|
|
215
|
+
assert(url.hostname === "a.aa");
|
|
216
|
+
assert(url.host === "a.aa:123");
|
|
217
|
+
assert(url.port === "123");
|
|
218
|
+
assert(url.protocol === "https:");
|
|
219
|
+
url = createQuasiURL("https://a.aa:42/chapter/1");
|
|
220
|
+
assert(url.origin === "https://a.aa:42");
|
|
221
|
+
assert(url.pathname === "/chapter/1");
|
|
222
|
+
assert(url.search === "");
|
|
223
|
+
assert(url.hash === "");
|
|
224
|
+
assert(url.href === "https://a.aa:42/chapter/1");
|
|
225
|
+
assert(url.hostname === "a.aa");
|
|
226
|
+
assert(url.host === "a.aa:42");
|
|
227
|
+
assert(url.port === "42");
|
|
94
228
|
assert(url.protocol === "https:");
|
|
95
229
|
url = createQuasiURL("https://a.aa");
|
|
96
230
|
assert(url.origin === "https://a.aa");
|
|
97
|
-
assert(url.pathname === "");
|
|
231
|
+
assert(url.pathname === "/");
|
|
98
232
|
assert(url.search === "");
|
|
99
233
|
assert(url.hash === "");
|
|
100
|
-
assert(url.href === "https://a.aa");
|
|
234
|
+
assert(url.href === "https://a.aa/");
|
|
101
235
|
assert(url.hostname === "a.aa");
|
|
102
236
|
assert(url.protocol === "https:");
|
|
103
237
|
url = createQuasiURL("");
|
|
104
238
|
assert(url.origin === "");
|
|
105
|
-
assert(url.pathname === "");
|
|
239
|
+
assert(url.pathname === "/");
|
|
106
240
|
assert(url.search === "");
|
|
107
241
|
assert(url.hash === "");
|
|
108
|
-
assert(url.href === "");
|
|
242
|
+
assert(url.href === "/");
|
|
109
243
|
assert(url.hostname === "");
|
|
110
244
|
assert(url.protocol === "");
|
|
111
245
|
url = createQuasiURL("?x=321&y=567#start");
|
|
112
246
|
assert(url.origin === "");
|
|
113
|
-
assert(url.pathname === "");
|
|
247
|
+
assert(url.pathname === "/");
|
|
114
248
|
assert(url.search === "?x=321&y=567");
|
|
115
249
|
assert(url.hash === "#start");
|
|
116
|
-
assert(url.href === "
|
|
250
|
+
assert(url.href === "/?x=321&y=567#start");
|
|
117
251
|
assert(url.hostname === "");
|
|
118
252
|
assert(url.protocol === "");
|
|
119
253
|
url = createQuasiURL("x/y?a=1");
|
|
120
254
|
assert(url.origin === "");
|
|
121
|
-
assert(url.pathname === "x/y");
|
|
255
|
+
assert(url.pathname === "/x/y");
|
|
122
256
|
assert(url.search === "?a=1");
|
|
123
257
|
assert(url.hash === "");
|
|
124
|
-
assert(url.href === "x/y?a=1");
|
|
258
|
+
assert(url.href === "/x/y?a=1");
|
|
259
|
+
assert(url.hostname === "");
|
|
260
|
+
assert(url.protocol === "");
|
|
261
|
+
console.log("\nURLSearchParams");
|
|
262
|
+
url.search = new URLSearchParams({ n: "m" });
|
|
263
|
+
assert(url.origin === "");
|
|
264
|
+
assert(url.pathname === "/x/y");
|
|
265
|
+
assert(url.search === "?n=m");
|
|
266
|
+
assert(url.hash === "");
|
|
267
|
+
assert(url.href === "/x/y?n=m");
|
|
268
|
+
assert(url.hostname === "");
|
|
269
|
+
assert(url.protocol === "");
|
|
270
|
+
console.log('\n"?" as search');
|
|
271
|
+
url.search = "?";
|
|
272
|
+
assert(url.origin === "");
|
|
273
|
+
assert(url.pathname === "/x/y");
|
|
274
|
+
assert(url.search === "");
|
|
275
|
+
assert(url.hash === "");
|
|
276
|
+
assert(url.href === "/x/y");
|
|
125
277
|
assert(url.hostname === "");
|
|
126
278
|
assert(url.protocol === "");
|
|
127
279
|
console.log("\npassed");
|
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "quasiurl",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "URL for templating",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"typecheck": "tsc --noEmit"
|
|
17
17
|
},
|
|
18
18
|
"author": "axtk",
|
|
19
|
-
"license": "
|
|
19
|
+
"license": "MIT",
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|
|
22
22
|
"url": "git://github.com/t8js/quasiurl.git"
|
package/tests.ts
CHANGED
|
@@ -29,6 +29,51 @@ assert(url.href === "/x");
|
|
|
29
29
|
assert(url.hostname === "");
|
|
30
30
|
assert(url.protocol === "");
|
|
31
31
|
|
|
32
|
+
console.log("\nset pathname");
|
|
33
|
+
url.pathname = "a/b";
|
|
34
|
+
|
|
35
|
+
assert(url.origin === "");
|
|
36
|
+
assert(url.pathname === "/a/b");
|
|
37
|
+
assert(url.search === "");
|
|
38
|
+
assert(url.hash === "");
|
|
39
|
+
assert(url.href === "/a/b");
|
|
40
|
+
assert(url.hostname === "");
|
|
41
|
+
assert(url.protocol === "");
|
|
42
|
+
|
|
43
|
+
console.log("\nset origin");
|
|
44
|
+
url.origin = "https://a.aa";
|
|
45
|
+
|
|
46
|
+
assert(url.origin === "https://a.aa");
|
|
47
|
+
assert(url.pathname === "/a/b");
|
|
48
|
+
assert(url.search === "");
|
|
49
|
+
assert(url.hash === "");
|
|
50
|
+
assert(url.href === "https://a.aa/a/b");
|
|
51
|
+
assert(url.hostname === "a.aa");
|
|
52
|
+
assert(url.protocol === "https:");
|
|
53
|
+
|
|
54
|
+
console.log("\nreset origin, set search");
|
|
55
|
+
url.origin = "";
|
|
56
|
+
url.search = "x=1";
|
|
57
|
+
|
|
58
|
+
assert(url.origin === "");
|
|
59
|
+
assert(url.pathname === "/a/b");
|
|
60
|
+
assert(url.search === "?x=1");
|
|
61
|
+
assert(url.hash === "");
|
|
62
|
+
assert(url.href === "/a/b?x=1");
|
|
63
|
+
assert(url.hostname === "");
|
|
64
|
+
assert(url.protocol === "");
|
|
65
|
+
|
|
66
|
+
console.log("\nset href");
|
|
67
|
+
url.href = "/x#intro";
|
|
68
|
+
|
|
69
|
+
assert(url.origin === "");
|
|
70
|
+
assert(url.pathname === "/x");
|
|
71
|
+
assert(url.search === "");
|
|
72
|
+
assert(url.hash === "#intro");
|
|
73
|
+
assert(url.href === "/x#intro");
|
|
74
|
+
assert(url.hostname === "");
|
|
75
|
+
assert(url.protocol === "");
|
|
76
|
+
|
|
32
77
|
url = createQuasiURL("/sections/:id");
|
|
33
78
|
|
|
34
79
|
assert(url.origin === "");
|
|
@@ -49,6 +94,28 @@ assert(url.href === "/x{/:name}");
|
|
|
49
94
|
assert(url.hostname === "");
|
|
50
95
|
assert(url.protocol === "");
|
|
51
96
|
|
|
97
|
+
console.log("\nupdate path template");
|
|
98
|
+
url.pathname = "{/:name}";
|
|
99
|
+
|
|
100
|
+
assert(url.origin === "");
|
|
101
|
+
assert(url.pathname === "{/:name}");
|
|
102
|
+
assert(url.search === "");
|
|
103
|
+
assert(url.hash === "");
|
|
104
|
+
assert(url.href === "{/:name}");
|
|
105
|
+
assert(url.hostname === "");
|
|
106
|
+
assert(url.protocol === "");
|
|
107
|
+
|
|
108
|
+
console.log("\nset origin to path template");
|
|
109
|
+
url.origin = "https://a.aa";
|
|
110
|
+
|
|
111
|
+
assert(url.origin === "https://a.aa");
|
|
112
|
+
assert(url.pathname === "{/:name}");
|
|
113
|
+
assert(url.search === "");
|
|
114
|
+
assert(url.hash === "");
|
|
115
|
+
assert(url.href === "https://a.aa{/:name}");
|
|
116
|
+
assert(url.hostname === "a.aa");
|
|
117
|
+
assert(url.protocol === "https:");
|
|
118
|
+
|
|
52
119
|
url = createQuasiURL("https://a.aa/x{/:name}");
|
|
53
120
|
|
|
54
121
|
assert(url.origin === "https://a.aa");
|
|
@@ -77,45 +144,94 @@ assert(url.search === "");
|
|
|
77
144
|
assert(url.hash === "");
|
|
78
145
|
assert(url.href === "https://a.aa/chapter/1");
|
|
79
146
|
assert(url.hostname === "a.aa");
|
|
147
|
+
assert(url.host === "a.aa");
|
|
148
|
+
assert(url.port === "");
|
|
149
|
+
assert(url.protocol === "https:");
|
|
150
|
+
|
|
151
|
+
console.log("\nset port");
|
|
152
|
+
url.port = 123;
|
|
153
|
+
|
|
154
|
+
assert(url.origin === "https://a.aa:123");
|
|
155
|
+
assert(url.pathname === "/chapter/1");
|
|
156
|
+
assert(url.search === "");
|
|
157
|
+
assert(url.hash === "");
|
|
158
|
+
assert(url.href === "https://a.aa:123/chapter/1");
|
|
159
|
+
assert(url.hostname === "a.aa");
|
|
160
|
+
assert(url.host === "a.aa:123");
|
|
161
|
+
assert(url.port === "123");
|
|
162
|
+
assert(url.protocol === "https:");
|
|
163
|
+
|
|
164
|
+
url = createQuasiURL("https://a.aa:42/chapter/1");
|
|
165
|
+
|
|
166
|
+
assert(url.origin === "https://a.aa:42");
|
|
167
|
+
assert(url.pathname === "/chapter/1");
|
|
168
|
+
assert(url.search === "");
|
|
169
|
+
assert(url.hash === "");
|
|
170
|
+
assert(url.href === "https://a.aa:42/chapter/1");
|
|
171
|
+
assert(url.hostname === "a.aa");
|
|
172
|
+
assert(url.host === "a.aa:42");
|
|
173
|
+
assert(url.port === "42");
|
|
80
174
|
assert(url.protocol === "https:");
|
|
81
175
|
|
|
82
176
|
url = createQuasiURL("https://a.aa");
|
|
83
177
|
|
|
84
178
|
assert(url.origin === "https://a.aa");
|
|
85
|
-
assert(url.pathname === "");
|
|
179
|
+
assert(url.pathname === "/");
|
|
86
180
|
assert(url.search === "");
|
|
87
181
|
assert(url.hash === "");
|
|
88
|
-
assert(url.href === "https://a.aa");
|
|
182
|
+
assert(url.href === "https://a.aa/");
|
|
89
183
|
assert(url.hostname === "a.aa");
|
|
90
184
|
assert(url.protocol === "https:");
|
|
91
185
|
|
|
92
186
|
url = createQuasiURL("");
|
|
93
187
|
|
|
94
188
|
assert(url.origin === "");
|
|
95
|
-
assert(url.pathname === "");
|
|
189
|
+
assert(url.pathname === "/");
|
|
96
190
|
assert(url.search === "");
|
|
97
191
|
assert(url.hash === "");
|
|
98
|
-
assert(url.href === "");
|
|
192
|
+
assert(url.href === "/");
|
|
99
193
|
assert(url.hostname === "");
|
|
100
194
|
assert(url.protocol === "");
|
|
101
195
|
|
|
102
196
|
url = createQuasiURL("?x=321&y=567#start");
|
|
103
197
|
|
|
104
198
|
assert(url.origin === "");
|
|
105
|
-
assert(url.pathname === "");
|
|
199
|
+
assert(url.pathname === "/");
|
|
106
200
|
assert(url.search === "?x=321&y=567");
|
|
107
201
|
assert(url.hash === "#start");
|
|
108
|
-
assert(url.href === "
|
|
202
|
+
assert(url.href === "/?x=321&y=567#start");
|
|
109
203
|
assert(url.hostname === "");
|
|
110
204
|
assert(url.protocol === "");
|
|
111
205
|
|
|
112
206
|
url = createQuasiURL("x/y?a=1");
|
|
113
207
|
|
|
114
208
|
assert(url.origin === "");
|
|
115
|
-
assert(url.pathname === "x/y");
|
|
209
|
+
assert(url.pathname === "/x/y");
|
|
116
210
|
assert(url.search === "?a=1");
|
|
117
211
|
assert(url.hash === "");
|
|
118
|
-
assert(url.href === "x/y?a=1");
|
|
212
|
+
assert(url.href === "/x/y?a=1");
|
|
213
|
+
assert(url.hostname === "");
|
|
214
|
+
assert(url.protocol === "");
|
|
215
|
+
|
|
216
|
+
console.log("\nURLSearchParams");
|
|
217
|
+
url.search = new URLSearchParams({ n: "m" });
|
|
218
|
+
|
|
219
|
+
assert(url.origin === "");
|
|
220
|
+
assert(url.pathname === "/x/y");
|
|
221
|
+
assert(url.search === "?n=m");
|
|
222
|
+
assert(url.hash === "");
|
|
223
|
+
assert(url.href === "/x/y?n=m");
|
|
224
|
+
assert(url.hostname === "");
|
|
225
|
+
assert(url.protocol === "");
|
|
226
|
+
|
|
227
|
+
console.log('\n"?" as search');
|
|
228
|
+
url.search = "?";
|
|
229
|
+
|
|
230
|
+
assert(url.origin === "");
|
|
231
|
+
assert(url.pathname === "/x/y");
|
|
232
|
+
assert(url.search === "");
|
|
233
|
+
assert(url.hash === "");
|
|
234
|
+
assert(url.href === "/x/y");
|
|
119
235
|
assert(url.hostname === "");
|
|
120
236
|
assert(url.protocol === "");
|
|
121
237
|
|