@zthun/webigail-url 5.0.3 → 5.0.6

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
@@ -1,721 +1,655 @@
1
- import { keyBy, last, sortBy, trim, trimEnd, trimStart } from 'lodash-es';
2
- import URLParse from 'url-parse';
3
-
4
- /* cspell:disable */ /**
5
- * Application mime types.
6
- */ var ZMimeTypeApplication = /*#__PURE__*/ function(ZMimeTypeApplication) {
7
- /**
8
- * JSON data.
9
- */ ZMimeTypeApplication["JSON"] = "application/json";
10
- /**
11
- * The unknown type.
12
- *
13
- * Used for raw byte data.
14
- */ ZMimeTypeApplication["OctetStream"] = "application/octet-stream";
15
- /**
16
- * Compressed zip stream.
17
- */ ZMimeTypeApplication["Zip"] = "application/zip";
18
- return ZMimeTypeApplication;
1
+ import { keyBy, last, sortBy, trim, trimEnd, trimStart } from "lodash-es";
2
+ import URLParse from "url-parse";
3
+ //#region src/mime/mime-type-application.mts
4
+ /**
5
+ * Application mime types.
6
+ */ var ZMimeTypeApplication = /* @__PURE__ */ function(ZMimeTypeApplication) {
7
+ /**
8
+ * JSON data.
9
+ */ ZMimeTypeApplication["JSON"] = "application/json";
10
+ /**
11
+ * The unknown type.
12
+ *
13
+ * Used for raw byte data.
14
+ */ ZMimeTypeApplication["OctetStream"] = "application/octet-stream";
15
+ /**
16
+ * Compressed zip stream.
17
+ */ ZMimeTypeApplication["Zip"] = "application/zip";
18
+ return ZMimeTypeApplication;
19
19
  }({});
20
-
21
- /* cspell:disable */ /**
22
- * Mime types for images.
23
- */ var ZMimeTypeImage = /*#__PURE__*/ function(ZMimeTypeImage) {
24
- /**
25
- * Good choice for lossless animation sequences (GIF is less performant). AVIF and
26
- * WebP have better performance but less broad browser support.
27
- */ ZMimeTypeImage["APNG"] = "image/apng";
28
- /**
29
- * Good choice for both images and animated images due to high performance and
30
- * royalty free image format. It offers much better compression than PNG or JPEG
31
- * with support for higher color depths, animated frames, transparency etc.
32
- *
33
- * Note that when using AVIF, you should include fallbacks to formats with
34
- * better browser support (i.e. using the <picture> element).
35
- */ ZMimeTypeImage["AVIF"] = "image/avif";
36
- /**
37
- * Good choice for simple images and animations. Prefer PNG for lossless and
38
- * indexed still images, and consider WebP, AVIF or APNG for animation sequences.
39
- */ ZMimeTypeImage["GIF"] = "image/gif";
40
- /**
41
- * Good choice for lossy compression of still images (currently the most popular). Prefer
42
- * PNG when more precise reproduction of the image is required, or WebP/AVIF if both better
43
- * reproduction and higher compression are required.
44
- */ ZMimeTypeImage["JPEG"] = "image/jpeg";
45
- /**
46
- * PNG is preferred over JPEG for more precise reproduction of source images, or when
47
- * transparency is needed. WebP/AVIF provide even better compression and reproduction,
48
- * but browser support is more limited.
49
- */ ZMimeTypeImage["PNG"] = "image/png";
50
- /**
51
- * Vector image format; ideal for user interface elements, icons, diagrams, etc., that
52
- * must be drawn accurately at different sizes.
53
- */ ZMimeTypeImage["SVG"] = "image/svg+xml";
54
- /**
55
- * Excellent choice for both images and animated images. WebP offers much better compression
56
- * than PNG or JPEG with support for higher color depths, animated frames, transparency etc.
57
- * AVIF offers slightly better compression, but is not quite as well-supported in browsers
58
- * and does not support progressive rendering.
59
- */ ZMimeTypeImage["WebP"] = "image/webp";
60
- return ZMimeTypeImage;
20
+ //#endregion
21
+ //#region src/mime/mime-type-image.mts
22
+ /**
23
+ * Mime types for images.
24
+ */ var ZMimeTypeImage = /* @__PURE__ */ function(ZMimeTypeImage) {
25
+ /**
26
+ * Good choice for lossless animation sequences (GIF is less performant). AVIF and
27
+ * WebP have better performance but less broad browser support.
28
+ */ ZMimeTypeImage["APNG"] = "image/apng";
29
+ /**
30
+ * Good choice for both images and animated images due to high performance and
31
+ * royalty free image format. It offers much better compression than PNG or JPEG
32
+ * with support for higher color depths, animated frames, transparency etc.
33
+ *
34
+ * Note that when using AVIF, you should include fallbacks to formats with
35
+ * better browser support (i.e. using the <picture> element).
36
+ */ ZMimeTypeImage["AVIF"] = "image/avif";
37
+ /**
38
+ * Good choice for simple images and animations. Prefer PNG for lossless and
39
+ * indexed still images, and consider WebP, AVIF or APNG for animation sequences.
40
+ */ ZMimeTypeImage["GIF"] = "image/gif";
41
+ /**
42
+ * Good choice for lossy compression of still images (currently the most popular). Prefer
43
+ * PNG when more precise reproduction of the image is required, or WebP/AVIF if both better
44
+ * reproduction and higher compression are required.
45
+ */ ZMimeTypeImage["JPEG"] = "image/jpeg";
46
+ /**
47
+ * PNG is preferred over JPEG for more precise reproduction of source images, or when
48
+ * transparency is needed. WebP/AVIF provide even better compression and reproduction,
49
+ * but browser support is more limited.
50
+ */ ZMimeTypeImage["PNG"] = "image/png";
51
+ /**
52
+ * Vector image format; ideal for user interface elements, icons, diagrams, etc., that
53
+ * must be drawn accurately at different sizes.
54
+ */ ZMimeTypeImage["SVG"] = "image/svg+xml";
55
+ /**
56
+ * Excellent choice for both images and animated images. WebP offers much better compression
57
+ * than PNG or JPEG with support for higher color depths, animated frames, transparency etc.
58
+ * AVIF offers slightly better compression, but is not quite as well-supported in browsers
59
+ * and does not support progressive rendering.
60
+ */ ZMimeTypeImage["WebP"] = "image/webp";
61
+ return ZMimeTypeImage;
61
62
  }({});
62
-
63
- /* cspell:disable */ /**
64
- * Mime types for text based data.
65
- */ var ZMimeTypeText = /*#__PURE__*/ function(ZMimeTypeText) {
66
- /**
67
- * Style files used in html documents must be sent as
68
- * css.
69
- */ ZMimeTypeText["CSS"] = "text/css";
70
- /**
71
- * Comma separated values.
72
- */ ZMimeTypeText["CSV"] = "text/csv";
73
- /**
74
- * JavaScript.
75
- */ ZMimeTypeText["EcmaScript"] = "text/ecmascript";
76
- /**
77
- * Hyper Text Markup Language. Markup language for
78
- * web pages.
79
- */ ZMimeTypeText["HTML"] = "text/html";
80
- /**
81
- * JavaScript.
82
- */ ZMimeTypeText["JavaScript"] = "text/javascript";
83
- /**
84
- * Plain (basic) text.
85
- */ ZMimeTypeText["Plain"] = "text/plain";
86
- /**
87
- * Xtreme Markup Language.
88
- *
89
- * Superset of HTML. Bulky, but can
90
- * represent anything.
91
- */ ZMimeTypeText["XML"] = "text/xml";
92
- return ZMimeTypeText;
63
+ //#endregion
64
+ //#region src/mime/mime-type-text.mts
65
+ /**
66
+ * Mime types for text based data.
67
+ */ var ZMimeTypeText = /* @__PURE__ */ function(ZMimeTypeText) {
68
+ /**
69
+ * Style files used in html documents must be sent as
70
+ * css.
71
+ */ ZMimeTypeText["CSS"] = "text/css";
72
+ /**
73
+ * Comma separated values.
74
+ */ ZMimeTypeText["CSV"] = "text/csv";
75
+ /**
76
+ * JavaScript.
77
+ */ ZMimeTypeText["EcmaScript"] = "text/ecmascript";
78
+ /**
79
+ * Hyper Text Markup Language. Markup language for
80
+ * web pages.
81
+ */ ZMimeTypeText["HTML"] = "text/html";
82
+ /**
83
+ * JavaScript.
84
+ */ ZMimeTypeText["JavaScript"] = "text/javascript";
85
+ /**
86
+ * Plain (basic) text.
87
+ */ ZMimeTypeText["Plain"] = "text/plain";
88
+ /**
89
+ * Xtreme Markup Language.
90
+ *
91
+ * Superset of HTML. Bulky, but can
92
+ * represent anything.
93
+ */ ZMimeTypeText["XML"] = "text/xml";
94
+ return ZMimeTypeText;
93
95
  }({});
94
-
96
+ //#endregion
97
+ //#region src/mime/mime-type.mts
95
98
  /**
96
- * A mapping of supported mime types.
97
- */ const ZSupportedMimeTypes = Object.freeze({
98
- ...{
99
- "": "text/plain;charset=ASCII"
100
- },
101
- ...keyBy(Object.values(ZMimeTypeApplication)),
102
- ...keyBy(Object.values(ZMimeTypeText)),
103
- ...keyBy(Object.values(ZMimeTypeImage))
99
+ * A mapping of supported mime types.
100
+ */ var ZSupportedMimeTypes = Object.freeze({
101
+ "": "text/plain;charset=ASCII",
102
+ ...keyBy(Object.values(ZMimeTypeApplication)),
103
+ ...keyBy(Object.values(ZMimeTypeText)),
104
+ ...keyBy(Object.values(ZMimeTypeImage))
104
105
  });
105
-
106
- function _define_property$1(obj, key, value) {
107
- if (key in obj) {
108
- Object.defineProperty(obj, key, {
109
- value: value,
110
- enumerable: true,
111
- configurable: true,
112
- writable: true
113
- });
114
- } else {
115
- obj[key] = value;
116
- }
117
- return obj;
118
- }
106
+ //#endregion
107
+ //#region src/data/data-url.mts
119
108
  /**
120
- * Represents an object that is helpful in building a data url with support
121
- * for data encoding.
122
- */ class ZDataUrlBuilder {
123
- /**
124
- * Parses an existing data url and sets all properties.
125
- *
126
- * @param url -
127
- * The url to parse.
128
- *
129
- * @returns
130
- * This object.
131
- */ parse(url) {
132
- this._data = {
133
- mimeType: "",
134
- encoding: "utf8",
135
- buffer: new Uint8Array([])
136
- };
137
- if (!url.startsWith("data:")) {
138
- return this;
139
- }
140
- url = url.substring(5);
141
- const parts = url.split(",");
142
- if (parts.length < 2) {
143
- return this;
144
- }
145
- const [mimeType, ...bodyParts] = parts;
146
- let [type, ...params] = mimeType.split(";");
147
- const isBase64 = last(params) === "base64";
148
- this._data.encoding = isBase64 ? "base64" : "utf8";
149
- if (isBase64) {
150
- params.pop();
151
- }
152
- if (!Object.hasOwnProperty.call(ZSupportedMimeTypes, type)) {
153
- type = ZMimeTypeApplication.OctetStream;
154
- params = [];
155
- }
156
- type = [
157
- type,
158
- ...params
159
- ].join(";");
160
- this._data.mimeType = type;
161
- // Commas can be in the body. Type this into chrome and you can
162
- // see that chrome actually parses it: data:text/plain,cat,,,
163
- // We will support this here, but we're going to properly encode it.
164
- let body = bodyParts.join("%2C");
165
- if (isBase64) {
166
- try {
167
- body = atob(body);
168
- } catch {
169
- body = "";
170
- }
171
- } else {
172
- body = decodeURIComponent(body);
173
- }
174
- this._data.buffer = new TextEncoder().encode(body);
175
- return this;
176
- }
177
- /**
178
- * Sets the mime type.
179
- *
180
- * @param type -
181
- * The mime type.
182
- *
183
- * @returns
184
- * This object.
185
- */ mimeType(type) {
186
- this._data.mimeType = type;
187
- return this;
188
- }
189
- /**
190
- * Sets the data buffer.
191
- *
192
- * @param data -
193
- * The data to set. If you pass a raw string, then the input encoding
194
- * is expected to be utf8.
195
- * @returns
196
- * This object.
197
- */ buffer(data) {
198
- this._data.buffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
199
- return this;
200
- }
201
- /**
202
- * Sets the output encoding.
203
- *
204
- * If you output encode the data as utf8, then it will be properly
205
- * escaped.
206
- *
207
- * @param encoding -
208
- * The output encoding.
209
- *
210
- * @returns
211
- * This object.
212
- */ encode(encoding) {
213
- this._data.encoding = encoding;
214
- return this;
215
- }
216
- /**
217
- * Builds the url string and returns it.
218
- *
219
- * @returns The url string.
220
- */ build() {
221
- const protocol = "data";
222
- const modifier = this._data.encoding === "base64" ? ";base64" : "";
223
- let raw = new TextDecoder("utf8").decode(this._data.buffer);
224
- if (this._data.encoding === "utf8") {
225
- raw = encodeURIComponent(raw);
226
- // Note ! should be encoded as %21, but for uri's..it's not because
227
- // it's actually valid, but some servers don't accept ! as a character
228
- // and it must be encoded. Just fix it here.
229
- raw = raw.split("!").join("%21");
230
- }
231
- if (this._data.encoding === "base64") {
232
- raw = btoa(raw);
233
- }
234
- return `${protocol}:${this._data.mimeType}${modifier},${raw}`;
235
- }
236
- /**
237
- * Gets the current information about the url being built.
238
- *
239
- * @returns The current information about the url being built.
240
- */ info() {
241
- const other = {
242
- ...this._data
243
- };
244
- other.buffer = this._data.buffer.slice();
245
- return other;
246
- }
247
- /**
248
- * Initializes a new instance of this object.
249
- */ constructor(){
250
- /**
251
- * The representation of the data url object.
252
- */ _define_property$1(this, "_data", void 0);
253
- this._data = {
254
- mimeType: "",
255
- encoding: "utf8",
256
- buffer: new Uint8Array([])
257
- };
258
- }
259
- }
260
-
261
- function _define_property(obj, key, value) {
262
- if (key in obj) {
263
- Object.defineProperty(obj, key, {
264
- value: value,
265
- enumerable: true,
266
- configurable: true,
267
- writable: true
268
- });
269
- } else {
270
- obj[key] = value;
271
- }
272
- return obj;
273
- }
109
+ * Represents an object that is helpful in building a data url with support
110
+ * for data encoding.
111
+ */ var ZDataUrlBuilder = class {
112
+ /**
113
+ * The representation of the data url object.
114
+ */ _data;
115
+ /**
116
+ * Initializes a new instance of this object.
117
+ */ constructor() {
118
+ this._data = {
119
+ mimeType: "",
120
+ encoding: "utf8",
121
+ buffer: new Uint8Array([])
122
+ };
123
+ }
124
+ /**
125
+ * Parses an existing data url and sets all properties.
126
+ *
127
+ * @param url -
128
+ * The url to parse.
129
+ *
130
+ * @returns
131
+ * This object.
132
+ */ parse(url) {
133
+ this._data = {
134
+ mimeType: "",
135
+ encoding: "utf8",
136
+ buffer: new Uint8Array([])
137
+ };
138
+ if (!url.startsWith("data:")) return this;
139
+ url = url.substring(5);
140
+ const parts = url.split(",");
141
+ if (parts.length < 2) return this;
142
+ const [mimeType, ...bodyParts] = parts;
143
+ let [type, ...params] = mimeType.split(";");
144
+ const isBase64 = last(params) === "base64";
145
+ this._data.encoding = isBase64 ? "base64" : "utf8";
146
+ if (isBase64) params.pop();
147
+ if (!Object.hasOwnProperty.call(ZSupportedMimeTypes, type)) {
148
+ type = ZMimeTypeApplication.OctetStream;
149
+ params = [];
150
+ }
151
+ type = [type, ...params].join(";");
152
+ this._data.mimeType = type;
153
+ let body = bodyParts.join("%2C");
154
+ if (isBase64) try {
155
+ body = atob(body);
156
+ } catch {
157
+ body = "";
158
+ }
159
+ else body = decodeURIComponent(body);
160
+ this._data.buffer = new TextEncoder().encode(body);
161
+ return this;
162
+ }
163
+ /**
164
+ * Sets the mime type.
165
+ *
166
+ * @param type -
167
+ * The mime type.
168
+ *
169
+ * @returns
170
+ * This object.
171
+ */ mimeType(type) {
172
+ this._data.mimeType = type;
173
+ return this;
174
+ }
175
+ /**
176
+ * Sets the data buffer.
177
+ *
178
+ * @param data -
179
+ * The data to set. If you pass a raw string, then the input encoding
180
+ * is expected to be utf8.
181
+ * @returns
182
+ * This object.
183
+ */ buffer(data) {
184
+ this._data.buffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
185
+ return this;
186
+ }
187
+ /**
188
+ * Sets the output encoding.
189
+ *
190
+ * If you output encode the data as utf8, then it will be properly
191
+ * escaped.
192
+ *
193
+ * @param encoding -
194
+ * The output encoding.
195
+ *
196
+ * @returns
197
+ * This object.
198
+ */ encode(encoding) {
199
+ this._data.encoding = encoding;
200
+ return this;
201
+ }
202
+ /**
203
+ * Builds the url string and returns it.
204
+ *
205
+ * @returns The url string.
206
+ */ build() {
207
+ const protocol = "data";
208
+ const modifier = this._data.encoding === "base64" ? ";base64" : "";
209
+ let raw = new TextDecoder("utf8").decode(this._data.buffer);
210
+ if (this._data.encoding === "utf8") {
211
+ raw = encodeURIComponent(raw);
212
+ raw = raw.split("!").join("%21");
213
+ }
214
+ if (this._data.encoding === "base64") raw = btoa(raw);
215
+ return `${protocol}:${this._data.mimeType}${modifier},${raw}`;
216
+ }
217
+ /**
218
+ * Gets the current information about the url being built.
219
+ *
220
+ * @returns The current information about the url being built.
221
+ */ info() {
222
+ const other = { ...this._data };
223
+ other.buffer = this._data.buffer.slice();
224
+ return other;
225
+ }
226
+ };
227
+ //#endregion
228
+ //#region src/url/url.mts
274
229
  /**
275
- * The api to target for YouTube.
276
- */ var ZYouTubeApi = /*#__PURE__*/ function(ZYouTubeApi) {
277
- /**
278
- * An embedded video link.
279
- */ ZYouTubeApi["Embed"] = "embed";
280
- /**
281
- * A watch video link.
282
- */ ZYouTubeApi["Watch"] = "watch";
283
- return ZYouTubeApi;
230
+ * The api to target for YouTube.
231
+ */ var ZYouTubeApi = /* @__PURE__ */ function(ZYouTubeApi) {
232
+ /**
233
+ * An embedded video link.
234
+ */ ZYouTubeApi["Embed"] = "embed";
235
+ /**
236
+ * A watch video link.
237
+ */ ZYouTubeApi["Watch"] = "watch";
238
+ return ZYouTubeApi;
284
239
  }({});
285
240
  /**
286
- * Represents an object that is helpful in building a url.
287
- */ class ZUrlBuilder {
288
- /**
289
- * Gets whether the given protocols default port is port.
290
- *
291
- * The main purpose of this method is to determine if a url requires
292
- * a port section. Therefore, there are some special behaviors which may
293
- * not be obvious:
294
- *
295
- * 1. If the port is falsy then this method returns true.
296
- * 2. If the port is NaN, then this method returns true.
297
- * 3. If the port is a string that evaluates to 0, then this method returns true.
298
- * 4. If port is less than 0, then this method returns true.
299
- *
300
- * @param protocol -
301
- * The protocol to check.
302
- * @param port -
303
- * The port to compare.
304
- *
305
- * @returns
306
- * True if the default port for protocol is port.
307
- */ static defaults(protocol, port) {
308
- const numericPort = +port;
309
- if (isNaN(numericPort) || numericPort < 1) {
310
- return true;
311
- }
312
- return ZUrlBuilder.ProtocolPorts[protocol] === +port;
313
- }
314
- /**
315
- * Fills the information from the current location data.
316
- *
317
- * @param loc -
318
- * The optional location object to populate with.
319
- *
320
- * @returns
321
- * This object.
322
- */ location(loc) {
323
- this.protocol(loc.protocol).hostname(loc.hostname).hash(loc.hash).path(loc.pathname).port(+loc.port);
324
- let search = loc.search;
325
- if (search.startsWith("?")) {
326
- search = search.slice(1);
327
- const pairs = search.split("&");
328
- pairs.map((pair)=>pair.split("=")).forEach((matrix)=>this.param(matrix[0], matrix[1]));
329
- }
330
- return this;
331
- }
332
- /**
333
- * Fills the information for an api path call.
334
- *
335
- * This is a combination of location, hash with an empty string, and an
336
- * append of the basePath.
337
- *
338
- * @param loc -
339
- * The optional location object to populate with.
340
- * @param basePath -
341
- * The basePath for the api. Generally, it's best to
342
- * just keep this as api and have your server accept
343
- * this path.
344
- *
345
- * @returns
346
- * This object.
347
- */ api(loc, basePath = "api") {
348
- return this.location(loc).hash("").path(basePath);
349
- }
350
- /**
351
- * Parses an existing url and sets all properties.
352
- *
353
- * If you give this a path without the protocol and hostname, then it will
354
- * automatically append the protocol and host of the browser window if it
355
- * exists.
356
- *
357
- * This method sets all the properties so if you need to modify the url before
358
- * parsing it, it is best to always call this method first.
359
- *
360
- * @param url -
361
- * The url to parse.
362
- *
363
- * @returns
364
- * This object.
365
- */ parse(url) {
366
- const current = new URLParse(url, true);
367
- this.protocol(current.protocol).username(current.username).password(current.password).hostname(current.hostname).hash(current.hash).path(current.pathname).port(current.port ? +current.port : undefined);
368
- Object.keys(current.query).forEach((key)=>this.param(key, current.query[key]));
369
- return this;
370
- }
371
- /**
372
- * Sets the url for a user gravatar.
373
- *
374
- * @param hash -
375
- * The md5 email hash.
376
- * @param size -
377
- * The dimensional size of the gravatar image.
378
- *
379
- * @returns
380
- * This object.
381
- */ gravatar(hash, size) {
382
- let current = this.parse(ZUrlBuilder.UrlGravatar);
383
- current = hash ? current.append(hash) : current;
384
- current = size ? current.param("s", String(size)) : current;
385
- return current;
386
- }
387
- /**
388
- * Sets the url target to something on YouTube.
389
- *
390
- * @param api -
391
- * The supported api.
392
- * @param id -
393
- * The id of the video. If api is set,
394
- * then this parameter is required.
395
- *
396
- * @returns
397
- * The url for YouTube. If you set the api and
398
- * id, then the url will be targeted to a specific video,
399
- * otherwise, the base domain url of YouTube will be returned.
400
- */ youTube(api, id) {
401
- let current = this.parse(ZUrlBuilder.UrlYouTube);
402
- current = api ? current.path(`${api}/${id}`) : current;
403
- // The watch api is a little bizarre that they don't actually
404
- // use the same format as their other apis. So we will handle this here.
405
- if (api === "watch") {
406
- current = current.path(api).param("v", id);
407
- }
408
- return current;
409
- }
410
- /**
411
- * Sets the protocol.
412
- *
413
- * @param protocol -
414
- * The protocol.
415
- *
416
- * @returns
417
- * This object.
418
- */ protocol(protocol) {
419
- this._url.protocol = protocol;
420
- return this;
421
- }
422
- /**
423
- * Sets the user name.
424
- *
425
- * Used for things like ssh and ftp.
426
- *
427
- * @param user -
428
- * The username
429
- *
430
- * @returns
431
- * This object.
432
- */ username(user) {
433
- this._url.username = user;
434
- return this;
435
- }
436
- /**
437
- * Sets the password.
438
- *
439
- * This is only valid if the username is set.
440
- *
441
- * @param pwd -
442
- * The user password.
443
- *
444
- * @returns
445
- * This object.
446
- */ password(pwd) {
447
- this._url.password = pwd;
448
- return this;
449
- }
450
- /**
451
- * Sets the host name.
452
- *
453
- * This sets the entire hostname as the root domain. This
454
- * will blow away any subdomains added, so it's best to
455
- * call this method first.
456
- *
457
- * @param host -
458
- * The hostname.
459
- *
460
- * @returns
461
- * This object.
462
- */ hostname(host) {
463
- this._url.hostname = host;
464
- return this;
465
- }
466
- /**
467
- * Adds a subdomain in front of the hostname.
468
- *
469
- * If a hostname was never set, then domain becomes the hostname.
470
- *
471
- * @param domain -
472
- * The domain to append.
473
- *
474
- * @returns
475
- * This object.
476
- */ subdomain(domain) {
477
- this._url.hostname = this._url.hostname ? `${domain}.${this._url.hostname}` : domain;
478
- return this;
479
- }
480
- /**
481
- * Removes a subdomain from the current domain.
482
- *
483
- * @returns
484
- * This object.
485
- */ popSubdomain() {
486
- const parts = this._url.hostname.split(".");
487
- parts.splice(0, 1);
488
- this._url.hostname = parts.join(".");
489
- return this;
490
- }
491
- /**
492
- * Sets the port.
493
- *
494
- * @param port -
495
- * The port.
496
- *
497
- * @returns
498
- * This object.
499
- */ port(port) {
500
- this._url.port = port;
501
- return this;
502
- }
503
- /**
504
- * Removes all existing path segments and restarts the path.
505
- *
506
- * @param path -
507
- * The starting path.
508
- *
509
- * @returns
510
- * This object.
511
- */ path(path) {
512
- this._url.path = [
513
- path
514
- ];
515
- return this;
516
- }
517
- /**
518
- * Appends a path segment.
519
- *
520
- * @param path -
521
- * The segment to append.
522
- *
523
- * @returns
524
- * This object.
525
- */ append(path) {
526
- this._url.path.push(path);
527
- return this;
528
- }
529
- /**
530
- * Sets the hash section.
531
- *
532
- * @param hash -
533
- * The hash section.
534
- *
535
- * @returns
536
- * This object.
537
- */ hash(hash) {
538
- this._url.hash = hash;
539
- return this;
540
- }
541
- /**
542
- * Adds a search parameter.
543
- *
544
- * This version assumes that value is not null.
545
- *
546
- * @param key -
547
- * The parameter key.
548
- * @param val -
549
- * The parameter value.
550
- *
551
- * @returns
552
- * This object.
553
- */ param(key, val) {
554
- this._url.params.push({
555
- key,
556
- val
557
- });
558
- return this;
559
- }
560
- /**
561
- * Removes all duplicate params and adds one with the key to the value.
562
- *
563
- * @param key -
564
- * The parameter key that can have only one.
565
- * @param val -
566
- * The parameter value. If this is falsy, then
567
- * the key is deleted.
568
- */ onlyParam(key, val) {
569
- this._url.params = this._url.params.filter((p)=>p.key !== key);
570
- return val ? this.param(key, val) : this;
571
- }
572
- /**
573
- * Adds a page param.
574
- *
575
- * @param page -
576
- * The page param to add. If this null, undefined,
577
- * or less than 1, then any page param is deleted.
578
- *
579
- * @returns
580
- * This object.
581
- */ page(page) {
582
- return this.onlyParam("page", page == null || page < 1 ? undefined : String(page));
583
- }
584
- /**
585
- * Adds a size param.
586
- *
587
- * @param size -
588
- * The size param to add. If this is null, undefined,
589
- * or less than 0, then any size param is deleted.
590
- *
591
- * @returns
592
- * This object.
593
- */ size(size) {
594
- return this.onlyParam("size", size == null || size < 0 ? undefined : String(size));
595
- }
596
- /**
597
- * Adds a search param.
598
- *
599
- * @param search -
600
- * The search param to add. If this is null, undefined,
601
- * or empty, then any search param is deleted.
602
- *
603
- * @returns
604
- * This object.
605
- */ search(search) {
606
- return this.onlyParam("search", search || undefined);
607
- }
608
- /**
609
- * Adds a filter param.
610
- *
611
- * @param filter -
612
- * The filter param to add. If this is null, undefined,
613
- * or empty, then any filter param is deleted.
614
- *
615
- * @returns
616
- * This object.
617
- */ filter(filter) {
618
- return this.onlyParam("filter", filter || undefined);
619
- }
620
- /**
621
- * Adds a sort param.
622
- *
623
- * @param sort -
624
- * The sort param to add. If this is null, undefined,
625
- * or empty, then any filter param is deleted.
626
- *
627
- * @returns
628
- * This object.
629
- */ sort(sort) {
630
- return this.onlyParam("sort", sort || undefined);
631
- }
632
- /**
633
- * Builds the url string and returns it.
634
- *
635
- * @returns
636
- * The url string.
637
- */ build() {
638
- const search = sortBy(this._url.params, (p)=>p.key).map((param)=>`${param.key}=${encodeURIComponent(param.val)}`).join("&");
639
- const user = trim(this._url.username);
640
- const password = trim(this._url.password);
641
- let protocol = trim(this._url.protocol);
642
- let host = trim(this._url.hostname);
643
- let port = String(this._url.port);
644
- let hash = trim(this._url.hash);
645
- let path = this._url.path.map((segment)=>trim(segment, "/")).join("/");
646
- let credentials = "";
647
- protocol = trimEnd(protocol, "/:");
648
- host = trim(host, "/");
649
- hash = trimStart(hash, "#");
650
- path = trim(path, "/");
651
- if (ZUrlBuilder.defaults(protocol, port)) {
652
- port = "";
653
- } else {
654
- port = `:${port}`;
655
- }
656
- if (user) {
657
- credentials = password ? `${user}:${password}@` : `${user}@`;
658
- }
659
- let url = `${protocol}://${credentials}${host}${port}`;
660
- if (path) {
661
- url = `${url}/${path}`;
662
- }
663
- if (search) {
664
- url = `${url}/?${search}`;
665
- }
666
- if (hash) {
667
- url = `${url}#${hash}`;
668
- }
669
- return url;
670
- }
671
- /**
672
- * Gets the current information about the uri being built.
673
- *
674
- * @returns The current uri information.
675
- */ info() {
676
- return JSON.parse(JSON.stringify(this._url));
677
- }
678
- /**
679
- * Initializes a new instance of this object.
680
- *
681
- * @param protocol -
682
- * The protocol to use.
683
- * @param hostname -
684
- * The hostname to connect with.
685
- */ constructor(protocol = "http", hostname = "localhost"){
686
- /**
687
- * The representation of the url object.
688
- */ _define_property(this, "_url", void 0);
689
- this._url = {
690
- protocol,
691
- hostname,
692
- path: [
693
- "/"
694
- ],
695
- hash: "",
696
- params: []
697
- };
698
- }
699
- }
700
- /**
701
- * The url to the gravatar api.
702
- */ _define_property(ZUrlBuilder, "UrlGravatar", "https://s.gravatar.com/avatar");
703
- /**
704
- * The url to youtube.
705
- *
706
- * This is mostly used to embed videos.
707
- */ _define_property(ZUrlBuilder, "UrlYouTube", "https://www.youtube.com");
708
- /**
709
- * A mapping between protocol and default port.
710
- */ _define_property(ZUrlBuilder, "ProtocolPorts", {
711
- http: 80,
712
- https: 443,
713
- ftp: 21,
714
- sftp: 22,
715
- ssh: 22,
716
- smtp: 25,
717
- smb: 445
718
- });
719
-
241
+ * Represents an object that is helpful in building a url.
242
+ */ var ZUrlBuilder = class ZUrlBuilder {
243
+ /**
244
+ * The url to the gravatar api.
245
+ */ static UrlGravatar = "https://s.gravatar.com/avatar";
246
+ /**
247
+ * The url to youtube.
248
+ *
249
+ * This is mostly used to embed videos.
250
+ */ static UrlYouTube = "https://www.youtube.com";
251
+ /**
252
+ * A mapping between protocol and default port.
253
+ */ static ProtocolPorts = {
254
+ http: 80,
255
+ https: 443,
256
+ ftp: 21,
257
+ sftp: 22,
258
+ ssh: 22,
259
+ smtp: 25,
260
+ smb: 445
261
+ };
262
+ /**
263
+ * Gets whether the given protocols default port is port.
264
+ *
265
+ * The main purpose of this method is to determine if a url requires
266
+ * a port section. Therefore, there are some special behaviors which may
267
+ * not be obvious:
268
+ *
269
+ * 1. If the port is falsy then this method returns true.
270
+ * 2. If the port is NaN, then this method returns true.
271
+ * 3. If the port is a string that evaluates to 0, then this method returns true.
272
+ * 4. If port is less than 0, then this method returns true.
273
+ *
274
+ * @param protocol -
275
+ * The protocol to check.
276
+ * @param port -
277
+ * The port to compare.
278
+ *
279
+ * @returns
280
+ * True if the default port for protocol is port.
281
+ */ static defaults(protocol, port) {
282
+ const numericPort = +port;
283
+ if (isNaN(numericPort) || numericPort < 1) return true;
284
+ return ZUrlBuilder.ProtocolPorts[protocol] === +port;
285
+ }
286
+ /**
287
+ * The representation of the url object.
288
+ */ _url;
289
+ /**
290
+ * Initializes a new instance of this object.
291
+ *
292
+ * @param protocol -
293
+ * The protocol to use.
294
+ * @param hostname -
295
+ * The hostname to connect with.
296
+ */ constructor(protocol = "http", hostname = "localhost") {
297
+ this._url = {
298
+ protocol,
299
+ hostname,
300
+ path: ["/"],
301
+ hash: "",
302
+ params: []
303
+ };
304
+ }
305
+ /**
306
+ * Fills the information from the current location data.
307
+ *
308
+ * @param loc -
309
+ * The optional location object to populate with.
310
+ *
311
+ * @returns
312
+ * This object.
313
+ */ location(loc) {
314
+ this.protocol(loc.protocol).hostname(loc.hostname).hash(loc.hash).path(loc.pathname).port(+loc.port);
315
+ let search = loc.search;
316
+ if (search.startsWith("?")) {
317
+ search = search.slice(1);
318
+ search.split("&").map((pair) => pair.split("=")).forEach((matrix) => this.param(matrix[0], matrix[1]));
319
+ }
320
+ return this;
321
+ }
322
+ /**
323
+ * Fills the information for an api path call.
324
+ *
325
+ * This is a combination of location, hash with an empty string, and an
326
+ * append of the basePath.
327
+ *
328
+ * @param loc -
329
+ * The optional location object to populate with.
330
+ * @param basePath -
331
+ * The basePath for the api. Generally, it's best to
332
+ * just keep this as api and have your server accept
333
+ * this path.
334
+ *
335
+ * @returns
336
+ * This object.
337
+ */ api(loc, basePath = "api") {
338
+ return this.location(loc).hash("").path(basePath);
339
+ }
340
+ /**
341
+ * Parses an existing url and sets all properties.
342
+ *
343
+ * If you give this a path without the protocol and hostname, then it will
344
+ * automatically append the protocol and host of the browser window if it
345
+ * exists.
346
+ *
347
+ * This method sets all the properties so if you need to modify the url before
348
+ * parsing it, it is best to always call this method first.
349
+ *
350
+ * @param url -
351
+ * The url to parse.
352
+ *
353
+ * @returns
354
+ * This object.
355
+ */ parse(url) {
356
+ const current = new URLParse(url, true);
357
+ this.protocol(current.protocol).username(current.username).password(current.password).hostname(current.hostname).hash(current.hash).path(current.pathname).port(current.port ? +current.port : void 0);
358
+ Object.keys(current.query).forEach((key) => this.param(key, current.query[key]));
359
+ return this;
360
+ }
361
+ /**
362
+ * Sets the url for a user gravatar.
363
+ *
364
+ * @param hash -
365
+ * The md5 email hash.
366
+ * @param size -
367
+ * The dimensional size of the gravatar image.
368
+ *
369
+ * @returns
370
+ * This object.
371
+ */ gravatar(hash, size) {
372
+ let current = this.parse(ZUrlBuilder.UrlGravatar);
373
+ current = hash ? current.append(hash) : current;
374
+ current = size ? current.param("s", String(size)) : current;
375
+ return current;
376
+ }
377
+ /**
378
+ * Sets the url target to something on YouTube.
379
+ *
380
+ * @param api -
381
+ * The supported api.
382
+ * @param id -
383
+ * The id of the video. If api is set,
384
+ * then this parameter is required.
385
+ *
386
+ * @returns
387
+ * The url for YouTube. If you set the api and
388
+ * id, then the url will be targeted to a specific video,
389
+ * otherwise, the base domain url of YouTube will be returned.
390
+ */ youTube(api, id) {
391
+ let current = this.parse(ZUrlBuilder.UrlYouTube);
392
+ current = api ? current.path(`${api}/${id}`) : current;
393
+ if (api === "watch") current = current.path(api).param("v", id);
394
+ return current;
395
+ }
396
+ /**
397
+ * Sets the protocol.
398
+ *
399
+ * @param protocol -
400
+ * The protocol.
401
+ *
402
+ * @returns
403
+ * This object.
404
+ */ protocol(protocol) {
405
+ this._url.protocol = protocol;
406
+ return this;
407
+ }
408
+ /**
409
+ * Sets the user name.
410
+ *
411
+ * Used for things like ssh and ftp.
412
+ *
413
+ * @param user -
414
+ * The username
415
+ *
416
+ * @returns
417
+ * This object.
418
+ */ username(user) {
419
+ this._url.username = user;
420
+ return this;
421
+ }
422
+ /**
423
+ * Sets the password.
424
+ *
425
+ * This is only valid if the username is set.
426
+ *
427
+ * @param pwd -
428
+ * The user password.
429
+ *
430
+ * @returns
431
+ * This object.
432
+ */ password(pwd) {
433
+ this._url.password = pwd;
434
+ return this;
435
+ }
436
+ /**
437
+ * Sets the host name.
438
+ *
439
+ * This sets the entire hostname as the root domain. This
440
+ * will blow away any subdomains added, so it's best to
441
+ * call this method first.
442
+ *
443
+ * @param host -
444
+ * The hostname.
445
+ *
446
+ * @returns
447
+ * This object.
448
+ */ hostname(host) {
449
+ this._url.hostname = host;
450
+ return this;
451
+ }
452
+ /**
453
+ * Adds a subdomain in front of the hostname.
454
+ *
455
+ * If a hostname was never set, then domain becomes the hostname.
456
+ *
457
+ * @param domain -
458
+ * The domain to append.
459
+ *
460
+ * @returns
461
+ * This object.
462
+ */ subdomain(domain) {
463
+ this._url.hostname = this._url.hostname ? `${domain}.${this._url.hostname}` : domain;
464
+ return this;
465
+ }
466
+ /**
467
+ * Removes a subdomain from the current domain.
468
+ *
469
+ * @returns
470
+ * This object.
471
+ */ popSubdomain() {
472
+ const parts = this._url.hostname.split(".");
473
+ parts.splice(0, 1);
474
+ this._url.hostname = parts.join(".");
475
+ return this;
476
+ }
477
+ /**
478
+ * Sets the port.
479
+ *
480
+ * @param port -
481
+ * The port.
482
+ *
483
+ * @returns
484
+ * This object.
485
+ */ port(port) {
486
+ this._url.port = port;
487
+ return this;
488
+ }
489
+ /**
490
+ * Removes all existing path segments and restarts the path.
491
+ *
492
+ * @param path -
493
+ * The starting path.
494
+ *
495
+ * @returns
496
+ * This object.
497
+ */ path(path) {
498
+ this._url.path = [path];
499
+ return this;
500
+ }
501
+ /**
502
+ * Appends a path segment.
503
+ *
504
+ * @param path -
505
+ * The segment to append.
506
+ *
507
+ * @returns
508
+ * This object.
509
+ */ append(path) {
510
+ this._url.path.push(path);
511
+ return this;
512
+ }
513
+ /**
514
+ * Sets the hash section.
515
+ *
516
+ * @param hash -
517
+ * The hash section.
518
+ *
519
+ * @returns
520
+ * This object.
521
+ */ hash(hash) {
522
+ this._url.hash = hash;
523
+ return this;
524
+ }
525
+ /**
526
+ * Adds a search parameter.
527
+ *
528
+ * This version assumes that value is not null.
529
+ *
530
+ * @param key -
531
+ * The parameter key.
532
+ * @param val -
533
+ * The parameter value.
534
+ *
535
+ * @returns
536
+ * This object.
537
+ */ param(key, val) {
538
+ this._url.params.push({
539
+ key,
540
+ val
541
+ });
542
+ return this;
543
+ }
544
+ /**
545
+ * Removes all duplicate params and adds one with the key to the value.
546
+ *
547
+ * @param key -
548
+ * The parameter key that can have only one.
549
+ * @param val -
550
+ * The parameter value. If this is falsy, then
551
+ * the key is deleted.
552
+ */ onlyParam(key, val) {
553
+ this._url.params = this._url.params.filter((p) => p.key !== key);
554
+ return val ? this.param(key, val) : this;
555
+ }
556
+ /**
557
+ * Adds a page param.
558
+ *
559
+ * @param page -
560
+ * The page param to add. If this null, undefined,
561
+ * or less than 1, then any page param is deleted.
562
+ *
563
+ * @returns
564
+ * This object.
565
+ */ page(page) {
566
+ return this.onlyParam("page", page == null || page < 1 ? void 0 : String(page));
567
+ }
568
+ /**
569
+ * Adds a size param.
570
+ *
571
+ * @param size -
572
+ * The size param to add. If this is null, undefined,
573
+ * or less than 0, then any size param is deleted.
574
+ *
575
+ * @returns
576
+ * This object.
577
+ */ size(size) {
578
+ return this.onlyParam("size", size == null || size < 0 ? void 0 : String(size));
579
+ }
580
+ /**
581
+ * Adds a search param.
582
+ *
583
+ * @param search -
584
+ * The search param to add. If this is null, undefined,
585
+ * or empty, then any search param is deleted.
586
+ *
587
+ * @returns
588
+ * This object.
589
+ */ search(search) {
590
+ return this.onlyParam("search", search || void 0);
591
+ }
592
+ /**
593
+ * Adds a filter param.
594
+ *
595
+ * @param filter -
596
+ * The filter param to add. If this is null, undefined,
597
+ * or empty, then any filter param is deleted.
598
+ *
599
+ * @returns
600
+ * This object.
601
+ */ filter(filter) {
602
+ return this.onlyParam("filter", filter || void 0);
603
+ }
604
+ /**
605
+ * Adds a sort param.
606
+ *
607
+ * @param sort -
608
+ * The sort param to add. If this is null, undefined,
609
+ * or empty, then any filter param is deleted.
610
+ *
611
+ * @returns
612
+ * This object.
613
+ */ sort(sort) {
614
+ return this.onlyParam("sort", sort || void 0);
615
+ }
616
+ /**
617
+ * Builds the url string and returns it.
618
+ *
619
+ * @returns
620
+ * The url string.
621
+ */ build() {
622
+ const search = sortBy(this._url.params, (p) => p.key).map((param) => `${param.key}=${encodeURIComponent(param.val)}`).join("&");
623
+ const user = trim(this._url.username);
624
+ const password = trim(this._url.password);
625
+ let protocol = trim(this._url.protocol);
626
+ let host = trim(this._url.hostname);
627
+ let port = String(this._url.port);
628
+ let hash = trim(this._url.hash);
629
+ let path = this._url.path.map((segment) => trim(segment, "/")).join("/");
630
+ let credentials = "";
631
+ protocol = trimEnd(protocol, "/:");
632
+ host = trim(host, "/");
633
+ hash = trimStart(hash, "#");
634
+ path = trim(path, "/");
635
+ if (ZUrlBuilder.defaults(protocol, port)) port = "";
636
+ else port = `:${port}`;
637
+ if (user) credentials = password ? `${user}:${password}@` : `${user}@`;
638
+ let url = `${protocol}://${credentials}${host}${port}`;
639
+ if (path) url = `${url}/${path}`;
640
+ if (search) url = `${url}/?${search}`;
641
+ if (hash) url = `${url}#${hash}`;
642
+ return url;
643
+ }
644
+ /**
645
+ * Gets the current information about the uri being built.
646
+ *
647
+ * @returns The current uri information.
648
+ */ info() {
649
+ return JSON.parse(JSON.stringify(this._url));
650
+ }
651
+ };
652
+ //#endregion
720
653
  export { ZDataUrlBuilder, ZMimeTypeApplication, ZMimeTypeImage, ZMimeTypeText, ZSupportedMimeTypes, ZUrlBuilder, ZYouTubeApi };
721
- //# sourceMappingURL=index.js.map
654
+
655
+ //# sourceMappingURL=index.js.map