@hkdigital/lib-sveltekit 0.0.31

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.
Files changed (109) hide show
  1. package/README.md +104 -0
  2. package/dist/classes/index.d.ts +2 -0
  3. package/dist/classes/index.js +2 -0
  4. package/dist/classes/stores/SubscribersCount.d.ts +39 -0
  5. package/dist/classes/stores/SubscribersCount.js +107 -0
  6. package/dist/classes/stores/index.d.ts +1 -0
  7. package/dist/classes/stores/index.js +1 -0
  8. package/dist/classes/streams/LogTransformStream.d.ts +4 -0
  9. package/dist/classes/streams/LogTransformStream.js +19 -0
  10. package/dist/classes/streams/ServerEventsStore.d.ts +22 -0
  11. package/dist/classes/streams/ServerEventsStore.js +110 -0
  12. package/dist/classes/streams/TimeStampSource.d.ts +13 -0
  13. package/dist/classes/streams/TimeStampSource.js +26 -0
  14. package/dist/classes/streams/index.d.ts +3 -0
  15. package/dist/classes/streams/index.js +3 -0
  16. package/dist/components/area/HkArea.svelte +49 -0
  17. package/dist/components/area/HkArea.svelte.d.ts +12 -0
  18. package/dist/components/area/HkGridArea.svelte +77 -0
  19. package/dist/components/area/HkGridArea.svelte.d.ts +20 -0
  20. package/dist/components/area/index.d.ts +2 -0
  21. package/dist/components/area/index.js +2 -0
  22. package/dist/components/icon/HkIcon.svelte +86 -0
  23. package/dist/components/icon/HkIcon.svelte.d.ts +10 -0
  24. package/dist/components/icon/HkTabIcon.svelte +112 -0
  25. package/dist/components/icon/HkTabIcon.svelte.d.ts +19 -0
  26. package/dist/components/icon/index.d.ts +3 -0
  27. package/dist/components/icon/index.js +4 -0
  28. package/dist/components/icon/typedef.d.ts +13 -0
  29. package/dist/components/icon/typedef.js +16 -0
  30. package/dist/components/index.d.ts +1 -0
  31. package/dist/components/index.js +2 -0
  32. package/dist/components/layout/HkAppLayout.state.svelte.d.ts +6 -0
  33. package/dist/components/layout/HkAppLayout.state.svelte.js +25 -0
  34. package/dist/components/layout/HkAppLayout.svelte +251 -0
  35. package/dist/components/layout/HkAppLayout.svelte.d.ts +8 -0
  36. package/dist/components/layout/HkGridLayers.svelte +72 -0
  37. package/dist/components/layout/HkGridLayers.svelte.d.ts +20 -0
  38. package/dist/components/layout/index.d.ts +3 -0
  39. package/dist/components/layout/index.js +9 -0
  40. package/dist/components/tab-bar/HkTabBar.state.svelte.d.ts +54 -0
  41. package/dist/components/tab-bar/HkTabBar.state.svelte.js +149 -0
  42. package/dist/components/tab-bar/HkTabBar.svelte +74 -0
  43. package/dist/components/tab-bar/HkTabBar.svelte.d.ts +16 -0
  44. package/dist/components/tab-bar/HkTabBarSelector.state.svelte.d.ts +19 -0
  45. package/dist/components/tab-bar/HkTabBarSelector.state.svelte.js +93 -0
  46. package/dist/components/tab-bar/HkTabBarSelector.svelte +49 -0
  47. package/dist/components/tab-bar/HkTabBarSelector.svelte.d.ts +17 -0
  48. package/dist/components/tab-bar/index.d.ts +5 -0
  49. package/dist/components/tab-bar/index.js +17 -0
  50. package/dist/components/tab-bar/typedef.d.ts +6 -0
  51. package/dist/components/tab-bar/typedef.js +8 -0
  52. package/dist/constants/index.d.ts +2 -0
  53. package/dist/constants/index.js +2 -0
  54. package/dist/constants/regexp/index.d.ts +3 -0
  55. package/dist/constants/regexp/index.js +31 -0
  56. package/dist/constants/regexp/inspiratie.js__ +95 -0
  57. package/dist/constants/regexp/text.d.ts +47 -0
  58. package/dist/constants/regexp/text.js +49 -0
  59. package/dist/constants/regexp/user.d.ts +6 -0
  60. package/dist/constants/regexp/user.js +33 -0
  61. package/dist/constants/regexp/web.d.ts +1 -0
  62. package/dist/constants/regexp/web.js +3 -0
  63. package/dist/constants/time.d.ts +19 -0
  64. package/dist/constants/time.js +28 -0
  65. package/dist/css/test.css +3 -0
  66. package/dist/css/utilities.postcss +44 -0
  67. package/dist/index.d.ts +1 -0
  68. package/dist/index.js +9 -0
  69. package/dist/server/index.d.ts +1 -0
  70. package/dist/server/index.js +1 -0
  71. package/dist/server/logger.d.ts +24 -0
  72. package/dist/server/logger.js +94 -0
  73. package/dist/states/index.d.ts +1 -0
  74. package/dist/states/index.js +1 -0
  75. package/dist/states/navigation.svelte.d.ts +23 -0
  76. package/dist/states/navigation.svelte.js +55 -0
  77. package/dist/stores/index.d.ts +1 -0
  78. package/dist/stores/index.js +1 -0
  79. package/dist/stores/theme.d.ts +28 -0
  80. package/dist/stores/theme.js +80 -0
  81. package/dist/util/expect/index.d.ts +39 -0
  82. package/dist/util/expect/index.js +145 -0
  83. package/dist/util/function.js__ +150 -0
  84. package/dist/util/index.d.ts +2 -0
  85. package/dist/util/index.js +2 -0
  86. package/dist/util/singleton/index.d.ts +34 -0
  87. package/dist/util/singleton/index.js +97 -0
  88. package/dist/util/svelte/index.d.ts +2 -0
  89. package/dist/util/svelte/index.js +2 -0
  90. package/dist/util/svelte/observe/index.d.ts +10 -0
  91. package/dist/util/svelte/observe/index.js +49 -0
  92. package/dist/util/svelte/state-context/index.d.ts +17 -0
  93. package/dist/util/svelte/state-context/index.js +83 -0
  94. package/dist/util/time.js__ +330 -0
  95. package/dist/valibot/index.d.ts +3 -0
  96. package/dist/valibot/index.js +30 -0
  97. package/dist/valibot/user.d.ts +6 -0
  98. package/dist/valibot/user.js +23 -0
  99. package/dist/zod/all.d.ts +6 -0
  100. package/dist/zod/all.js +33 -0
  101. package/dist/zod/generic.d.ts +6 -0
  102. package/dist/zod/generic.js +11 -0
  103. package/dist/zod/javascript.d.ts +8 -0
  104. package/dist/zod/javascript.js +32 -0
  105. package/dist/zod/user.d.ts +9 -0
  106. package/dist/zod/user.js +16 -0
  107. package/dist/zod/web.d.ts +21 -0
  108. package/dist/zod/web.js +57 -0
  109. package/package.json +144 -0
@@ -0,0 +1,330 @@
1
+ /**
2
+ * time.js
3
+ *
4
+ * @description
5
+ * This file contains time related functionality
6
+ *
7
+ * @example
8
+ *
9
+ * import { delay, now } from './time.js';
10
+ *
11
+ * async function test()
12
+ * {
13
+ * console.log( `Current timestamp [${now()}]` );
14
+ *
15
+ * await delay( 1000 );
16
+ *
17
+ * console.log( `Current timestamp [${now()}]` );
18
+ * }
19
+ */
20
+
21
+ /**
22
+ * FIXME: use Intl.DateTimeFormat instead of custom conversions
23
+ *
24
+ * e.g.
25
+ * return new Intl.DateTimeFormat('nl-NL', {
26
+ * month: "long",
27
+ * timeZone: 'Europe/Amsterdam'
28
+ * }).format(d);
29
+ */
30
+
31
+ /* ------------------------------------------------------------------ Imports */
32
+
33
+ // import { expectPositiveNumber } from './expect.js';
34
+ // import { HkPromise } from './promises.js';
35
+
36
+ /* ---------------------------------------------------------------- Internals */
37
+
38
+ /* ------------------------------------------------------------------ Exports */
39
+
40
+ /**
41
+ * Returns a promise that resolves after a specified timeout
42
+ * - If the returned promise is rejected, the timeout is cancelled
43
+ *
44
+ * @param {number} delayOrMinDelayMs
45
+ * Number of milliseconds to wait before promise resolves
46
+ *
47
+ * @param {number} [maxDelayMs=delayOrMinDelayMs]
48
+ * Maximum number of milliseconds to wait before the returned promise
49
+ * resolves. If this parameter is set, the delay will be chosen randomly
50
+ * between the values [delayOrMinDelayMs, maxDelayMs]
51
+ *
52
+ * @returns {Promise} promise that resolves after a specified timeout
53
+ */
54
+ export function delay(delayOrMinDelayMs, maxDelayMs) {
55
+ if (typeof delayOrMinDelayMs !== 'number') {
56
+ throw new Error('Invalid or missing parameter [delayOrMinDelayMs]');
57
+ }
58
+
59
+ if (maxDelayMs) {
60
+ //
61
+ // maxDelayMs was set -> generate random delay
62
+ //
63
+ if (maxDelayMs > delayOrMinDelayMs) {
64
+ delayOrMinDelayMs = Math.floor(
65
+ delayOrMinDelayMs + Math.random() * (maxDelayMs - delayOrMinDelayMs)
66
+ );
67
+ }
68
+ }
69
+
70
+ const promise = new HkPromise();
71
+
72
+ let timer = setTimeout(() => {
73
+ timer = null;
74
+ promise.resolve();
75
+ }, delayOrMinDelayMs);
76
+
77
+ // Register catch method to cancel timer when promise is rejected
78
+ promise.catch(() => {
79
+ if (timer) {
80
+ clearTimeout(timer);
81
+ timer = null;
82
+ }
83
+ });
84
+
85
+ return promise;
86
+ }
87
+
88
+ // -----------------------------------------------------------------------------
89
+
90
+ /**
91
+ * Get the number of milliseconds since the specified time stamp of the default
92
+ * reference time stamp TIME_2020_01_01
93
+ *
94
+ * @param {number} [sinceMs=TIME_2020_01_01]
95
+ *
96
+ * @returns {number} number of milliseconds since the specified time
97
+ */
98
+ export function sinceMs(sinceMs = TIME_2020_01_01) {
99
+ return Date.now() - sinceMs;
100
+ }
101
+
102
+ // -----------------------------------------------------------------------------
103
+
104
+ /**
105
+ * Get a string that represents the time in a readable
106
+ * string format: [DD:][HH:]MM:SS.mmm
107
+ *
108
+ * @param {number} timeMs [description]
109
+ *
110
+ * @returns {string} time in human readable format
111
+ */
112
+ export function timeToString(timeMs) {
113
+ const days = Math.floor(timeMs / DAY_MS);
114
+
115
+ let restMs = timeMs - days * DAY_MS;
116
+
117
+ const hours = Math.floor(restMs / HOUR_MS);
118
+
119
+ restMs = restMs - hours * HOUR_MS;
120
+
121
+ const minutes = Math.floor(restMs / MINUTE_MS);
122
+
123
+ restMs = restMs - minutes * MINUTE_MS;
124
+
125
+ const seconds = Math.floor(restMs / SECOND_MS);
126
+
127
+ restMs = restMs - seconds * SECOND_MS;
128
+
129
+ let str = '';
130
+
131
+ if (days) {
132
+ str += `${days.toString().padStart(2, '0')}:`;
133
+ str += `${hours.toString().padStart(2, '0')}:`;
134
+ } else if (hours) {
135
+ str += `${hours.toString().padStart(2, '0')}:`;
136
+ }
137
+
138
+ str += `${minutes.toString().padStart(2, '0')}:`;
139
+ str += `${seconds.toString().padStart(2, '0')}.`;
140
+ str += `${restMs.toString().padEnd(3, '0')}`;
141
+
142
+ return str;
143
+ }
144
+
145
+ // -----------------------------------------------------------------------------
146
+
147
+ /**
148
+ * Returns a Date object
149
+ * - The input can be a Date object or a numeric timestamp
150
+ *
151
+ * @param {Date|number} dateOrTimestamp
152
+ *
153
+ * @returns {Date} date object
154
+ */
155
+ export function toDate(dateOrTimestamp) {
156
+ if (dateOrTimestamp instanceof Date) {
157
+ return dateOrTimestamp;
158
+ }
159
+
160
+ if (typeof dateOrTimestamp === 'number') {
161
+ return new Date(dateOrTimestamp);
162
+ }
163
+
164
+ throw new Error('Missing or invalid parameter [dateOrTimestamp]');
165
+ }
166
+
167
+ // -----------------------------------------------------------------------------
168
+
169
+ /**
170
+ * Get the ISO 8601 week number of the specified date
171
+ *
172
+ * @see https://stackoverflow.com
173
+ * /questions/6117814/get-week-of-year-in-javascript-like-in-php
174
+ *
175
+ * @param {Date|number} dateOrTimestamp
176
+ *
177
+ * @returns {number} week number
178
+ */
179
+ export function getWeekNumber(dateOrTimestamp) {
180
+ const date = toDate(dateOrTimestamp);
181
+
182
+ //
183
+ // Create a copy of this date object
184
+ //
185
+ const target = new Date(date.valueOf());
186
+
187
+ //
188
+ // ISO week date weeks start on Monday, so correct the day number
189
+ //
190
+ const dayNumber = (date.getDay() + 6) % 7;
191
+
192
+ //
193
+ // ISO 8601 states that week 1 is the week with the first Thursday
194
+ // of that year.
195
+ //
196
+ // Set the target date to the Thursday in the target week
197
+ //
198
+ target.setDate(target.getDate() - dayNumber + 3);
199
+
200
+ //
201
+ // Store the millisecond value of the target date
202
+ //
203
+ const firstThursday = target.valueOf();
204
+
205
+ // Set the target to the first Thursday of the year
206
+ // First, set the target to January 1st
207
+ target.setMonth(0, 1);
208
+
209
+ //
210
+ // Not a Thursday? Correct the date to the next Thursday
211
+ //
212
+ if (target.getDay() !== 4) {
213
+ target.setMonth(0, 1 + ((4 - target.getDay() + 7) % 7));
214
+ }
215
+
216
+ //
217
+ // The week number is the number of weeks between the first Thursday
218
+ // of the year and the Thursday in the target week
219
+ // (604800000 = 7 * 24 * 3600 * 1000)
220
+ //
221
+ return 1 + Math.ceil((firstThursday - target) / 604800000);
222
+ }
223
+
224
+ // -----------------------------------------------------------------------------
225
+
226
+ /**
227
+ * Get the name of the month
228
+ * - Returns the English name of the month
229
+ *
230
+ * - Use the output as label in combination with the functions
231
+ * text() and translate() for international month names
232
+ *
233
+ * e.g.
234
+ *
235
+ * setTranslations()
236
+ * ...
237
+ *
238
+ * text( getMonthName( new Date() ) );
239
+ *
240
+ * --
241
+ *
242
+ * @param {Date|number} dateOrTimestamp
243
+ *
244
+ * @returns {string} name of the month (English)
245
+ */
246
+ export function getMonthName(dateOrTimestamp) {
247
+ return MONTH_NAME_LABELS_EN[toDate(dateOrTimestamp).getMonth()];
248
+ }
249
+
250
+ // -----------------------------------------------------------------------------
251
+
252
+ /**
253
+ * Get the name of the day
254
+ * - Returns the English name of the day
255
+ *
256
+ * - Use the output as label in combination with the functions
257
+ * text() and translate() for international day names
258
+ *
259
+ * e.g.
260
+ *
261
+ * setTranslations()
262
+ * ...
263
+ *
264
+ * text( getDayName( new Date() ) );
265
+ *
266
+ * --
267
+ *
268
+ * @param {Date|number} dateOrTimestamp
269
+ *
270
+ * @returns {string} name of the day (English)
271
+ */
272
+ export function getDayName(dateOrTimestamp) {
273
+ return DAY_NAME_LABELS_EN[toDate(dateOrTimestamp).getDay()];
274
+ }
275
+
276
+ // -----------------------------------------------------------------------------
277
+
278
+ /**
279
+ * Return the timestamp of the start of the day
280
+ * - Midnight
281
+ *
282
+ * @param {Date|number} dateOrTimestamp
283
+ *
284
+ * @returns {number} timestamp of start of the day (00:00:00:0000)
285
+ */
286
+ export function getTimeAtStartOfDay(dateOrTimestamp) {
287
+ let d;
288
+
289
+ if (dateOrTimestamp) {
290
+ d = toDate(dateOrTimestamp);
291
+ } else {
292
+ // today, now
293
+ d = new Date();
294
+ }
295
+
296
+ d.setHours(0);
297
+ d.setMinutes(0);
298
+ d.setSeconds(0);
299
+ d.setMilliseconds(0);
300
+
301
+ return d.getTime();
302
+ }
303
+
304
+ // -----------------------------------------------------------------------------
305
+
306
+ /**
307
+ * Return the timestamp of the end of the day
308
+ * - Midnight - 1 millisecond
309
+ *
310
+ * @param {Date|number} dateOrTimestamp
311
+ *
312
+ * @returns {number} timestamp of start of the day
313
+ */
314
+ export function getTimeAtEndOfDay(dateOrTimestamp) {
315
+ let d;
316
+
317
+ if (dateOrTimestamp) {
318
+ d = toDate(dateOrTimestamp);
319
+ } else {
320
+ // today, now
321
+ d = new Date();
322
+ }
323
+
324
+ d.setHours(23);
325
+ d.setMinutes(59);
326
+ d.setSeconds(59);
327
+ d.setMilliseconds(999);
328
+
329
+ return d.getTime();
330
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./user.js";
2
+ export default v;
3
+ import * as v from 'valibot';
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The following namespace style import can be used to acces the named exports
3
+ * and als the JSdoc typedefs
4
+ *
5
+ * @example
6
+ *
7
+ * import * as Zods from '<path-to>/zod/all.js';
8
+ *
9
+ * JSdoc:
10
+ * `@param {Zods.Name} name
11
+ * `@param {Zods.Timeout} t
12
+ *
13
+ * Assert style:
14
+ * Zods.String.parse(name);
15
+ *
16
+ * Parse style:
17
+ * const name = Zods.Name.parse('Jens ');
18
+ *
19
+ * Use default export 'z':
20
+ * // eslint-disable-next-line no-unused-vars
21
+ * import z, * as Zods from '<path-to>/zod/all.js';
22
+ *
23
+ * z.string().parse(name);
24
+ */
25
+
26
+ import * as v from 'valibot';
27
+
28
+ export * from './user.js';
29
+
30
+ export default v;
@@ -0,0 +1,6 @@
1
+ export const Name: v.SchemaWithPipe<[v.StringSchema<undefined>, v.TrimAction, v.RegexAction<string, undefined>]>;
2
+ export const Fullname: v.SchemaWithPipe<[v.StringSchema<undefined>, v.TrimAction, v.RegexAction<string, undefined>]>;
3
+ export const Username: v.SchemaWithPipe<[v.StringSchema<undefined>, v.TrimAction, v.RegexAction<string, undefined>]>;
4
+ export const Surname: v.SchemaWithPipe<[v.StringSchema<undefined>, v.TrimAction, v.RegexAction<string, undefined>]>;
5
+ export const PhoneNumber: v.SchemaWithPipe<[v.StringSchema<undefined>, v.TrimAction, v.RegexAction<string, undefined>]>;
6
+ import * as v from 'valibot';
@@ -0,0 +1,23 @@
1
+ // > Imports
2
+
3
+ import * as v from 'valibot';
4
+
5
+ import {
6
+ RE_FULLNAME,
7
+ RE_NAME,
8
+ RE_USERNAME,
9
+ RE_SURNAME,
10
+ RE_PHONENUMBER
11
+ } from '../constants/regexp/index.js';
12
+
13
+ // > Exports
14
+
15
+ export const Name = v.pipe(v.string(), v.trim(), v.regex(RE_NAME));
16
+
17
+ export const Fullname = v.pipe(v.string(), v.trim(), v.regex(RE_FULLNAME));
18
+
19
+ export const Username = v.pipe(v.string(), v.trim(), v.regex(RE_USERNAME));
20
+
21
+ export const Surname = v.pipe(v.string(), v.trim(), v.regex(RE_SURNAME));
22
+
23
+ export const PhoneNumber = v.pipe(v.string(), v.trim(), v.regex(RE_PHONENUMBER));
@@ -0,0 +1,6 @@
1
+ export * from "./generic.js";
2
+ export * from "./javascript.js";
3
+ export * from "./user.js";
4
+ export * from "./web.js";
5
+ export default z;
6
+ import { z } from 'zod';
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The following namespace style import can be used to acces the named exports
3
+ * and als the JSdoc typedefs
4
+ *
5
+ * @example
6
+ *
7
+ * import * as Zods from '<path-to>/zod/all.js';
8
+ *
9
+ * JSdoc:
10
+ * `@param {Zods.Name} name
11
+ * `@param {Zods.Timeout} t
12
+ *
13
+ * Assert style:
14
+ * Zods.String.parse(name);
15
+ *
16
+ * Parse style:
17
+ * const name = Zods.Name.parse('Jens ');
18
+ *
19
+ * Use default export 'z':
20
+ * // eslint-disable-next-line no-unused-vars
21
+ * import z, * as Zods from '<path-to>/zod/all.js';
22
+ *
23
+ * z.string().parse(name);
24
+ */
25
+
26
+ import { z } from 'zod';
27
+
28
+ export * from './generic.js';
29
+ export * from './javascript.js';
30
+ export * from './user.js';
31
+ export * from './web.js';
32
+
33
+ export default z;
@@ -0,0 +1,6 @@
1
+ export type Label = string;
2
+ export const Label: z.ZodString;
3
+ export type LabelOrSymbol = string | Symbol;
4
+ /** @typedef {string} Label */
5
+ export const LabelOrSymbol: z.ZodUnion<[z.ZodString, z.ZodSymbol]>;
6
+ import { z } from 'zod';
@@ -0,0 +1,11 @@
1
+ // > Imports
2
+
3
+ import { z } from 'zod';
4
+
5
+ // > Exports
6
+
7
+ export const Label = z.string().min(1).max(80);
8
+ /** @typedef {string} Label */
9
+
10
+ export const LabelOrSymbol = z.union([z.string().min(1).max(80), z.symbol()]);
11
+ /** @typedef {string|Symbol} LabelOrSymbol */
@@ -0,0 +1,8 @@
1
+ export type Timeout = number | NodeJS.Timeout;
2
+ export const Timeout: z.ZodNumber;
3
+ export type TimeoutOrUndefined = number | NodeJS.Timeout | undefined;
4
+ /** @typedef {number|NodeJS.Timeout} Timeout */
5
+ export const TimeoutOrUndefined: z.ZodUnion<[z.ZodNumber, z.ZodUndefined]>;
6
+ declare const _default: {};
7
+ export default _default;
8
+ import { z } from 'zod';
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The following namespace style import can be used to acces the named exports
3
+ * and als the JSdoc typedefs
4
+ *
5
+ * import z, * as Zods from '<path-to>/all.js';
6
+ *
7
+ * @example
8
+ *
9
+ * JSdoc:
10
+ * `@param {Type.Name} name
11
+ *
12
+ * Assert style:
13
+ * Type.String.parse(name);
14
+ *
15
+ * Parse style:
16
+ * const name = Type.Name.parse('Jens ');
17
+ */
18
+
19
+ // > Imports
20
+
21
+ import { z } from 'zod';
22
+
23
+ // > Exports
24
+
25
+ export const Timeout = z.number();
26
+ /** @typedef {number|NodeJS.Timeout} Timeout */
27
+
28
+ export const TimeoutOrUndefined = z.union([z.number(), z.undefined()]);
29
+ /** @typedef {number|NodeJS.Timeout|undefined} TimeoutOrUndefined */
30
+
31
+ // Export default is required for exporting typedefs
32
+ export default {};
@@ -0,0 +1,9 @@
1
+ export type Name = z.infer<typeof Name>;
2
+ export const Name: z.ZodString;
3
+ export type Fullname = z.infer<typeof Fullname>;
4
+ /** @typedef { z.infer<typeof Name> } Name */
5
+ export const Fullname: z.ZodString;
6
+ export type Username = z.infer<typeof Username>;
7
+ /** @typedef { z.infer<typeof Fullname> } Fullname */
8
+ export const Username: z.ZodString;
9
+ import { z } from 'zod';
@@ -0,0 +1,16 @@
1
+ // > Imports
2
+
3
+ import { z } from 'zod';
4
+
5
+ import { RE_FULLNAME, RE_NAME, RE_USERNAME } from '../constants/regexp/index.js';
6
+
7
+ // > Exports
8
+
9
+ export const Name = z.string().trim().regex(RE_NAME);
10
+ /** @typedef { z.infer<typeof Name> } Name */
11
+
12
+ export const Fullname = z.string().trim().regex(RE_FULLNAME);
13
+ /** @typedef { z.infer<typeof Fullname> } Fullname */
14
+
15
+ export const Username = z.string().trim().regex(RE_USERNAME);
16
+ /** @typedef { z.infer<typeof Username> } Username */
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Parse Url
3
+ * - Allows relative urls (only the pathname and a search part of an url)
4
+ * - Require the use of the HTTP(s) protocol in absolute URL's
5
+ *
6
+ * @param {object} _
7
+ * @param {boolean} [_.allowRelative=true]
8
+ * @param {boolean} [_.requireHttp=true]
9
+ *
10
+ * @returns {{ parse:function }}
11
+ */
12
+ export function UrlOrPath({ allowRelative, requireHttp }?: {
13
+ allowRelative?: boolean;
14
+ requireHttp?: boolean;
15
+ }): {
16
+ parse: Function;
17
+ };
18
+ export type AbsOrRelUrl = string;
19
+ export const AbsOrRelUrl: {
20
+ parse: Function;
21
+ };
@@ -0,0 +1,57 @@
1
+ // > Imports
2
+
3
+ import { z } from 'zod';
4
+
5
+ // > Exports
6
+
7
+ /**
8
+ * Parse Url
9
+ * - Allows relative urls (only the pathname and a search part of an url)
10
+ * - Require the use of the HTTP(s) protocol in absolute URL's
11
+ *
12
+ * @param {object} _
13
+ * @param {boolean} [_.allowRelative=true]
14
+ * @param {boolean} [_.requireHttp=true]
15
+ *
16
+ * @returns {{ parse:function }}
17
+ */
18
+ export function UrlOrPath({ allowRelative = true, requireHttp = true } = {}) {
19
+ // > URL parser
20
+ return z.string().refine((value) => {
21
+ let url;
22
+
23
+ try {
24
+ if (!allowRelative) {
25
+ // > Ensure that URL is absolute
26
+ url = new URL(value);
27
+
28
+ if (requireHttp) {
29
+ // > Ensure that protocol is http(s)
30
+ if (url.protocol.startsWith('http')) {
31
+ return null;
32
+ }
33
+ }
34
+
35
+ return (
36
+ `${url.origin}${url.pathname}` +
37
+ `${url.search.length <= 1 ? '' : url.search}`
38
+ );
39
+ } else {
40
+ // > Parse relative URL
41
+ // @note removes ../../ parts
42
+
43
+ url = new URL(value, 'http://localhost');
44
+
45
+ return (
46
+ `${url.pathname}` + `${url.search.length <= 1 ? '' : url.search}`
47
+ );
48
+ }
49
+ // eslint-disable-next-line no-unused-vars
50
+ } catch (e) {
51
+ return null;
52
+ }
53
+ });
54
+ }
55
+
56
+ export const AbsOrRelUrl = UrlOrPath();
57
+ /** @typedef {string} AbsOrRelUrl */