quasiurl 1.1.0 → 1.1.2

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 CHANGED
@@ -13,14 +13,12 @@ var QuasiURL = class {
13
13
  return `${this.origin}${this.pathname}${this.search}${this.hash}`;
14
14
  }
15
15
  set href(value) {
16
- let head = value.match(/^((\w+:)?\/\/([^/:]+)(:(\d+))?)(\/.*|$)/);
17
- let tail = value.match(/(\?[^#]+)?(#.+)?$/);
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] ?? "";
16
+ let origin = value.match(/^(\w+:)?\/\/[^/:]+(:\d+)?/)?.[0] ?? "";
17
+ let tail = value.slice(origin.length).match(/^([^?#]+)?(\?[^#]+)?(#.+)?$/) ?? [];
18
+ this.origin = origin;
19
+ this.pathname = tail[1] ?? "";
20
+ this.search = tail[2] ?? "";
21
+ this.hash = tail[3] ?? "";
24
22
  }
25
23
  get protocol() {
26
24
  return this._protocol;
package/index.ts CHANGED
@@ -17,18 +17,14 @@ export class QuasiURL {
17
17
  return `${this.origin}${this.pathname}${this.search}${this.hash}`;
18
18
  }
19
19
  set href(value: string) {
20
- let head = value.match(/^((\w+:)?\/\/([^/:]+)(:(\d+))?)(\/.*|$)/);
21
- let tail = value.match(/(\?[^#]+)?(#.+)?$/);
20
+ let origin = value.match(/^(\w+:)?\/\/[^/:]+(:\d+)?/)?.[0] ?? "";
21
+ let tail =
22
+ value.slice(origin.length).match(/^([^?#]+)?(\?[^#]+)?(#.+)?$/) ?? [];
22
23
 
23
- this.protocol = head?.[2] ?? "";
24
- this.hostname = head?.[3] ?? "";
25
- this.port = head?.[5] ?? "";
26
- this.pathname = value
27
- .replace(/^(\w+:)?\/\/[^/:]+(:\d+)?/, "")
28
- .replace(/\?.*$/, "")
29
- .replace(/#.*$/, "");
30
- this.search = tail?.[1] ?? "";
31
- this.hash = tail?.[2] ?? "";
24
+ this.origin = origin;
25
+ this.pathname = tail[1] ?? "";
26
+ this.search = tail[2] ?? "";
27
+ this.hash = tail[3] ?? "";
32
28
  }
33
29
  get protocol() {
34
30
  return this._protocol;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quasiurl",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "URL for templating",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/dist/tests.js DELETED
@@ -1,279 +0,0 @@
1
- // index.ts
2
- var QuasiURL = class {
3
- _protocol = "";
4
- _hostname = "";
5
- _port = "";
6
- _pathname = "";
7
- _search = "";
8
- _hash = "";
9
- constructor(url2) {
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(/(\?[^#]+)?(#.+)?$/);
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;
80
- }
81
- toString() {
82
- return this.href;
83
- }
84
- };
85
-
86
- // tests.ts
87
- var k = 0;
88
- function assert(predicate) {
89
- let n = `00${++k}`.slice(-3);
90
- if (predicate) console.log(n, "passed");
91
- else {
92
- console.error(n, "failed");
93
- console.error();
94
- console.error("[!] failed");
95
- process.exit(1);
96
- }
97
- }
98
- function createQuasiURL(url2) {
99
- console.log(`
100
- ${JSON.stringify(url2)}`);
101
- return new QuasiURL(url2);
102
- }
103
- var url = createQuasiURL("/x");
104
- assert(url.origin === "");
105
- assert(url.pathname === "/x");
106
- assert(url.search === "");
107
- assert(url.hash === "");
108
- assert(url.href === "/x");
109
- assert(url.hostname === "");
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 === "");
148
- url = createQuasiURL("/sections/:id");
149
- assert(url.origin === "");
150
- assert(url.pathname === "/sections/:id");
151
- assert(url.search === "");
152
- assert(url.hash === "");
153
- assert(url.href === "/sections/:id");
154
- assert(url.hostname === "");
155
- assert(url.protocol === "");
156
- url = createQuasiURL("/x{/:name}");
157
- assert(url.origin === "");
158
- assert(url.pathname === "/x{/:name}");
159
- assert(url.search === "");
160
- assert(url.hash === "");
161
- assert(url.href === "/x{/:name}");
162
- assert(url.hostname === "");
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:");
182
- url = createQuasiURL("https://a.aa/x{/:name}");
183
- assert(url.origin === "https://a.aa");
184
- assert(url.pathname === "/x{/:name}");
185
- assert(url.search === "");
186
- assert(url.hash === "");
187
- assert(url.href === "https://a.aa/x{/:name}");
188
- assert(url.hostname === "a.aa");
189
- assert(url.protocol === "https:");
190
- url = createQuasiURL("/x?a=1&b=nnn#start");
191
- assert(url.origin === "");
192
- assert(url.pathname === "/x");
193
- assert(url.search === "?a=1&b=nnn");
194
- assert(url.hash === "#start");
195
- assert(url.href === "/x?a=1&b=nnn#start");
196
- assert(url.hostname === "");
197
- assert(url.protocol === "");
198
- url = createQuasiURL("https://a.aa/chapter/1");
199
- assert(url.origin === "https://a.aa");
200
- assert(url.pathname === "/chapter/1");
201
- assert(url.search === "");
202
- assert(url.hash === "");
203
- assert(url.href === "https://a.aa/chapter/1");
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");
228
- assert(url.protocol === "https:");
229
- url = createQuasiURL("https://a.aa");
230
- assert(url.origin === "https://a.aa");
231
- assert(url.pathname === "/");
232
- assert(url.search === "");
233
- assert(url.hash === "");
234
- assert(url.href === "https://a.aa/");
235
- assert(url.hostname === "a.aa");
236
- assert(url.protocol === "https:");
237
- url = createQuasiURL("");
238
- assert(url.origin === "");
239
- assert(url.pathname === "/");
240
- assert(url.search === "");
241
- assert(url.hash === "");
242
- assert(url.href === "/");
243
- assert(url.hostname === "");
244
- assert(url.protocol === "");
245
- url = createQuasiURL("?x=321&y=567#start");
246
- assert(url.origin === "");
247
- assert(url.pathname === "/");
248
- assert(url.search === "?x=321&y=567");
249
- assert(url.hash === "#start");
250
- assert(url.href === "/?x=321&y=567#start");
251
- assert(url.hostname === "");
252
- assert(url.protocol === "");
253
- url = createQuasiURL("x/y?a=1");
254
- assert(url.origin === "");
255
- assert(url.pathname === "/x/y");
256
- assert(url.search === "?a=1");
257
- assert(url.hash === "");
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");
277
- assert(url.hostname === "");
278
- assert(url.protocol === "");
279
- console.log("\npassed");
package/tests.ts DELETED
@@ -1,238 +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
- 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
-
77
- url = createQuasiURL("/sections/:id");
78
-
79
- assert(url.origin === "");
80
- assert(url.pathname === "/sections/:id");
81
- assert(url.search === "");
82
- assert(url.hash === "");
83
- assert(url.href === "/sections/:id");
84
- assert(url.hostname === "");
85
- assert(url.protocol === "");
86
-
87
- url = createQuasiURL("/x{/:name}");
88
-
89
- assert(url.origin === "");
90
- assert(url.pathname === "/x{/:name}");
91
- assert(url.search === "");
92
- assert(url.hash === "");
93
- assert(url.href === "/x{/:name}");
94
- assert(url.hostname === "");
95
- assert(url.protocol === "");
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
-
119
- url = createQuasiURL("https://a.aa/x{/:name}");
120
-
121
- assert(url.origin === "https://a.aa");
122
- assert(url.pathname === "/x{/:name}");
123
- assert(url.search === "");
124
- assert(url.hash === "");
125
- assert(url.href === "https://a.aa/x{/:name}");
126
- assert(url.hostname === "a.aa");
127
- assert(url.protocol === "https:");
128
-
129
- url = createQuasiURL("/x?a=1&b=nnn#start");
130
-
131
- assert(url.origin === "");
132
- assert(url.pathname === "/x");
133
- assert(url.search === "?a=1&b=nnn");
134
- assert(url.hash === "#start");
135
- assert(url.href === "/x?a=1&b=nnn#start");
136
- assert(url.hostname === "");
137
- assert(url.protocol === "");
138
-
139
- url = createQuasiURL("https://a.aa/chapter/1");
140
-
141
- assert(url.origin === "https://a.aa");
142
- assert(url.pathname === "/chapter/1");
143
- assert(url.search === "");
144
- assert(url.hash === "");
145
- assert(url.href === "https://a.aa/chapter/1");
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");
174
- assert(url.protocol === "https:");
175
-
176
- url = createQuasiURL("https://a.aa");
177
-
178
- assert(url.origin === "https://a.aa");
179
- assert(url.pathname === "/");
180
- assert(url.search === "");
181
- assert(url.hash === "");
182
- assert(url.href === "https://a.aa/");
183
- assert(url.hostname === "a.aa");
184
- assert(url.protocol === "https:");
185
-
186
- url = createQuasiURL("");
187
-
188
- assert(url.origin === "");
189
- assert(url.pathname === "/");
190
- assert(url.search === "");
191
- assert(url.hash === "");
192
- assert(url.href === "/");
193
- assert(url.hostname === "");
194
- assert(url.protocol === "");
195
-
196
- url = createQuasiURL("?x=321&y=567#start");
197
-
198
- assert(url.origin === "");
199
- assert(url.pathname === "/");
200
- assert(url.search === "?x=321&y=567");
201
- assert(url.hash === "#start");
202
- assert(url.href === "/?x=321&y=567#start");
203
- assert(url.hostname === "");
204
- assert(url.protocol === "");
205
-
206
- url = createQuasiURL("x/y?a=1");
207
-
208
- assert(url.origin === "");
209
- assert(url.pathname === "/x/y");
210
- assert(url.search === "?a=1");
211
- assert(url.hash === "");
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");
235
- assert(url.hostname === "");
236
- assert(url.protocol === "");
237
-
238
- console.log("\npassed");