@byloth/core 2.0.0-rc.9 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/core.js +4087 -621
  2. package/dist/core.js.map +1 -1
  3. package/dist/core.umd.cjs +2 -2
  4. package/dist/core.umd.cjs.map +1 -1
  5. package/package.json +17 -13
  6. package/src/core/types.ts +41 -0
  7. package/src/helpers.ts +11 -2
  8. package/src/index.ts +12 -9
  9. package/src/models/aggregators/aggregated-async-iterator.ts +920 -21
  10. package/src/models/aggregators/aggregated-iterator.ts +838 -22
  11. package/src/models/aggregators/reduced-iterator.ts +827 -11
  12. package/src/models/aggregators/types.ts +153 -10
  13. package/src/models/callbacks/callable-object.ts +42 -6
  14. package/src/models/callbacks/index.ts +2 -2
  15. package/src/models/callbacks/publisher.ts +160 -4
  16. package/src/models/callbacks/switchable-callback.ts +230 -23
  17. package/src/models/callbacks/types.ts +16 -0
  18. package/src/models/exceptions/core.ts +132 -3
  19. package/src/models/exceptions/index.ts +405 -13
  20. package/src/models/index.ts +4 -8
  21. package/src/models/iterators/smart-async-iterator.ts +827 -22
  22. package/src/models/iterators/smart-iterator.ts +755 -20
  23. package/src/models/iterators/types.ts +268 -9
  24. package/src/models/json/json-storage.ts +508 -110
  25. package/src/models/json/types.ts +10 -1
  26. package/src/models/promises/deferred-promise.ts +85 -5
  27. package/src/models/promises/index.ts +1 -3
  28. package/src/models/promises/smart-promise.ts +272 -4
  29. package/src/models/promises/timed-promise.ts +43 -1
  30. package/src/models/promises/types.ts +84 -2
  31. package/src/models/timers/clock.ts +109 -19
  32. package/src/models/timers/countdown.ts +176 -21
  33. package/src/models/timers/game-loop.ts +266 -0
  34. package/src/models/timers/index.ts +2 -1
  35. package/src/models/types.ts +6 -5
  36. package/src/utils/async.ts +43 -0
  37. package/src/utils/curve.ts +85 -0
  38. package/src/utils/date.ts +204 -10
  39. package/src/utils/dom.ts +16 -2
  40. package/src/utils/index.ts +3 -2
  41. package/src/utils/iterator.ts +200 -17
  42. package/src/utils/math.ts +55 -3
  43. package/src/utils/random.ts +139 -2
  44. package/src/utils/string.ts +11 -0
  45. package/src/models/game-loop.ts +0 -83
  46. package/src/models/promises/long-running-task.ts +0 -294
  47. package/src/models/promises/thenable.ts +0 -97
@@ -1,125 +1,517 @@
1
1
  import Exception from "./core.js";
2
2
 
3
+ /**
4
+ * A class representing a generic exception that can be thrown when a file
5
+ * operation fails, such as reading, writing, copying, moving, deleting, etc...
6
+ *
7
+ * It can also be used to catch all file-related exceptions at once.
8
+ *
9
+ * ```ts
10
+ * try { [...] }
11
+ * catch (error)
12
+ * {
13
+ * if (error instanceof FileException)
14
+ * {
15
+ * // A file-related exception occurred. Handle it...
16
+ * }
17
+ * }
18
+ * ```
19
+ */
3
20
  export class FileException extends Exception
4
21
  {
22
+ /**
23
+ * Initializes a new instance of the {@link FileException} class.
24
+ *
25
+ * ---
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * throw new FileException("An error occurred while trying to read the file.");
30
+ * ```
31
+ *
32
+ * ---
33
+ *
34
+ * @param message The message that describes the error.
35
+ * @param cause The previous caught error that caused this one, if any.
36
+ * @param name The name of the exception. Default is `"FileException"`.
37
+ */
5
38
  public constructor(message: string, cause?: unknown, name = "FileException")
6
39
  {
7
40
  super(message, cause, name);
8
41
  }
9
42
 
10
- public readonly [Symbol.toStringTag]: string = "FileException";
43
+ public override readonly [Symbol.toStringTag]: string = "FileException";
11
44
  }
45
+
46
+ /**
47
+ * A class representing an exception that can be thrown when a file already exists.
48
+ *
49
+ * ```ts
50
+ * import { existsSync } from "node:fs";
51
+ *
52
+ * if (existsSync("file.txt"))
53
+ * {
54
+ * throw new FileExistsException("The file named 'file.txt' already exists.");
55
+ * }
56
+ * ```
57
+ */
12
58
  export class FileExistsException extends FileException
13
59
  {
60
+ /**
61
+ * Initializes a new instance of the {@link FileExistsException} class.
62
+ *
63
+ * ---
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * throw new FileExistsException("The file named 'data.json' already exists on the server.");
68
+ * ```
69
+ *
70
+ * ---
71
+ *
72
+ * @param message The message that describes the error.
73
+ * @param cause The previous caught error that caused this one, if any.
74
+ * @param name The name of the exception. Default is `"FileExistsException"`.
75
+ */
14
76
  public constructor(message: string, cause?: unknown, name = "FileExistsException")
15
77
  {
16
78
  super(message, cause, name);
17
79
  }
18
80
 
19
- public readonly [Symbol.toStringTag]: string = "FileExistsException";
81
+ public override readonly [Symbol.toStringTag]: string = "FileExistsException";
20
82
  }
83
+
84
+ /**
85
+ * A class representing an exception that can be thrown when a file isn't found.
86
+ *
87
+ * ```ts
88
+ * import { existsSync } from "node:fs";
89
+ *
90
+ * if (!existsSync("file.txt"))
91
+ * {
92
+ * throw new FileNotFoundException("The file named 'file.txt' wasn't found.");
93
+ * }
94
+ * ```
95
+ */
21
96
  export class FileNotFoundException extends FileException
22
97
  {
98
+ /**
99
+ * Initializes a new instance of the {@link FileNotFoundException} class.
100
+ *
101
+ * ---
102
+ *
103
+ * @example
104
+ * ```ts
105
+ * throw new FileNotFoundException("The file named 'data.json' wasn't found on the server.");
106
+ * ```
107
+ *
108
+ * ---
109
+ *
110
+ * @param message The message that describes the error.
111
+ * @param cause The previous caught error that caused this one, if any.
112
+ * @param name The name of the exception. Default is `"FileNotFoundException"`.
113
+ */
23
114
  public constructor(message: string, cause?: unknown, name = "FileNotFoundException")
24
115
  {
25
116
  super(message, cause, name);
26
117
  }
27
118
 
28
- public readonly [Symbol.toStringTag]: string = "FileNotFoundException";
119
+ public override readonly [Symbol.toStringTag]: string = "FileNotFoundException";
29
120
  }
30
121
 
122
+ /**
123
+ * A class representing an exception that can be thrown when a key is invalid or not found.
124
+ * It's commonly used when working with dictionaries, maps, objects, sets, etc...
125
+ *
126
+ * ```ts
127
+ * const map = new Map<string, number>();
128
+ * if (!map.has("hash"))
129
+ * {
130
+ * throw new KeyException("The key 'hash' wasn't found in the collection.");
131
+ * }
132
+ * ```
133
+ */
31
134
  export class KeyException extends Exception
32
135
  {
136
+ /**
137
+ * Initializes a new instance of the {@link KeyException} class.
138
+ *
139
+ * ---
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * throw new KeyException("The 'id' key wasn't found in the dictionary.");
144
+ * ```
145
+ *
146
+ * ---
147
+ *
148
+ * @param message The message that describes the error.
149
+ * @param cause The previous caught error that caused this one, if any.
150
+ * @param name The name of the exception. Default is `"KeyException"`.
151
+ */
33
152
  public constructor(message: string, cause?: unknown, name = "KeyException")
34
153
  {
35
154
  super(message, cause, name);
36
155
  }
37
156
 
38
- public readonly [Symbol.toStringTag]: string = "KeyException";
157
+ public override readonly [Symbol.toStringTag]: string = "KeyException";
39
158
  }
159
+
160
+ /**
161
+ * A class representing an exception that can be thrown when a network operation fails.
162
+ * It's commonly used when it's unable to connect to a server or when a request times out.
163
+ *
164
+ * ```ts
165
+ * import axios, { isAxiosError } from "axios";
166
+ *
167
+ * try { await axios.get("https://api.example.com/data"); }
168
+ * catch (error)
169
+ * {
170
+ * if (isAxiosError(error) && !error.response)
171
+ * {
172
+ * throw new NetworkException(
173
+ * "Unable to establish a connection to the server. " +
174
+ * "Please, check your internet connection and try again."
175
+ * );
176
+ * }
177
+ * }
178
+ * ```
179
+ */
40
180
  export class NetworkException extends Exception
41
181
  {
182
+ /**
183
+ * Initializes a new instance of the {@link NetworkException} class.
184
+ *
185
+ * ---
186
+ *
187
+ * @example
188
+ * ```ts
189
+ * throw new NetworkException("Couldn't connect to the server. Please, try again later.");
190
+ * ```
191
+ *
192
+ * ---
193
+ *
194
+ * @param message The message that describes the error.
195
+ * @param cause The previous caught error that caused this one, if any.
196
+ * @param name The name of the exception. Default is `"NetworkException"`.
197
+ */
42
198
  public constructor(message: string, cause?: unknown, name = "NetworkException")
43
199
  {
44
200
  super(message, cause, name);
45
201
  }
46
202
 
47
- public readonly [Symbol.toStringTag]: string = "NetworkException";
203
+ public override readonly [Symbol.toStringTag]: string = "NetworkException";
48
204
  }
205
+
206
+ /**
207
+ * A class representing an exception that can be thrown when a permission is denied.
208
+ * It's commonly used when a user tries to access a restricted resource or perform a forbidden action.
209
+ *
210
+ * ```ts
211
+ * const $user = useUserStore();
212
+ * if (!$user.isAdmin)
213
+ * {
214
+ * throw new PermissionException("You don't have permission to perform this action.");
215
+ * }
216
+ * ```
217
+ */
49
218
  export class PermissionException extends Exception
50
219
  {
220
+ /**
221
+ * Initializes a new instance of the {@link PermissionException} class.
222
+ *
223
+ * ---
224
+ *
225
+ * @example
226
+ * ```ts
227
+ * throw new PermissionException("You don't have permission to access this resource.");
228
+ * ```
229
+ *
230
+ * ---
231
+ *
232
+ * @param message The message that describes the error.
233
+ * @param cause The previous caught error that caused this one, if any.
234
+ * @param name The name of the exception. Default is `"PermissionException"`.
235
+ */
51
236
  public constructor(message: string, cause?: unknown, name = "PermissionException")
52
237
  {
53
238
  super(message, cause, name);
54
239
  }
55
240
 
56
- public readonly [Symbol.toStringTag]: string = "PermissionException";
241
+ public override readonly [Symbol.toStringTag]: string = "PermissionException";
57
242
  }
243
+
244
+ /**
245
+ * A class representing an exception that can be thrown when a reference is invalid or not found.
246
+ * It's commonly used when a variable is `null`, `undefined` or when an object doesn't exist.
247
+ *
248
+ * ```ts
249
+ * const $el = document.getElementById("app");
250
+ * if ($el === null)
251
+ * {
252
+ * throw new ReferenceException("The element with the ID 'app' wasn't found in the document.");
253
+ * }
254
+ * ```
255
+ */
58
256
  export class ReferenceException extends Exception
59
257
  {
258
+ /**
259
+ * Initializes a new instance of the {@link ReferenceException} class.
260
+ *
261
+ * ---
262
+ *
263
+ * @example
264
+ * ```ts
265
+ * throw new ReferenceException("The 'canvas' element wasn't found in the document.");
266
+ * ```
267
+ *
268
+ * ---
269
+ *
270
+ * @param message The message that describes the error.
271
+ * @param cause The previous caught error that caused this one, if any.
272
+ * @param name The name of the exception. Default is `"ReferenceException"`.
273
+ */
60
274
  public constructor(message: string, cause?: unknown, name = "ReferenceException")
61
275
  {
62
276
  super(message, cause, name);
63
277
  }
64
278
 
65
- public readonly [Symbol.toStringTag]: string = "ReferenceException";
279
+ public override readonly [Symbol.toStringTag]: string = "ReferenceException";
66
280
  }
67
281
 
282
+ /**
283
+ * A class representing an exception that can be thrown when a runtime error occurs.
284
+ * It's commonly used when an unexpected condition is encountered during the execution of a program.
285
+ *
286
+ * ```ts
287
+ * let status: "enabled" | "disabled" = "enabled";
288
+ *
289
+ * function enable(): void
290
+ * {
291
+ * if (status === "enabled") { throw new RuntimeException("The feature is already enabled."); }
292
+ * status = "enabled";
293
+ * }
294
+ * ```
295
+ */
68
296
  export class RuntimeException extends Exception
69
297
  {
298
+ /**
299
+ * Initializes a new instance of the {@link RuntimeException} class.
300
+ *
301
+ * ---
302
+ *
303
+ * @example
304
+ * ```ts
305
+ * throw new RuntimeException("The received input seems to be malformed or corrupted.");
306
+ * ```
307
+ *
308
+ * ---
309
+ *
310
+ * @param message The message that describes the error.
311
+ * @param cause The previous caught error that caused this one, if any.
312
+ * @param name The name of the exception. Default is `"RuntimeException"`.
313
+ */
70
314
  public constructor(message: string, cause?: unknown, name = "RuntimeException")
71
315
  {
72
316
  super(message, cause, name);
73
317
  }
74
318
 
75
- public readonly [Symbol.toStringTag]: string = "RuntimeException";
319
+ public override readonly [Symbol.toStringTag]: string = "RuntimeException";
76
320
  }
321
+
322
+ /**
323
+ * A class representing an exception that can be thrown when an environment
324
+ * isn't properly configured or when a required variable isn't set.
325
+ * It can also be used when the environment on which the program is running is unsupported.
326
+ *
327
+ * ```ts
328
+ * if (!navigator.geolocation)
329
+ * {
330
+ * throw new EnvironmentException("The Geolocation API isn't supported in this environment.");
331
+ * }
332
+ * ```
333
+ */
77
334
  export class EnvironmentException extends RuntimeException
78
335
  {
336
+ /**
337
+ * Initializes a new instance of the {@link EnvironmentException} class.
338
+ *
339
+ * ---
340
+ *
341
+ * @example
342
+ * ```ts
343
+ * throw new EnvironmentException("The required environment variable 'API_KEY' isn't set.");
344
+ * ```
345
+ *
346
+ * ---
347
+ *
348
+ * @param message The message that describes the error.
349
+ * @param cause The previous caught error that caused this one, if any.
350
+ * @param name The name of the exception. Default is `"EnvironmentException"`.
351
+ */
79
352
  public constructor(message: string, cause?: unknown, name = "EnvironmentException")
80
353
  {
81
354
  super(message, cause, name);
82
355
  }
83
356
 
84
- public readonly [Symbol.toStringTag]: string = "EnvironmentException";
357
+ public override readonly [Symbol.toStringTag]: string = "EnvironmentException";
85
358
  }
86
359
 
360
+ /**
361
+ * A class representing an exception that can be thrown when a timeout occurs.
362
+ * It's commonly used when a task takes too long to complete or when a request times out.
363
+ *
364
+ * ```ts
365
+ * const timeoutId = setTimeout(() => { throw new TimeoutException("The request timed out."); }, 5_000);
366
+ * const response = await fetch("https://api.example.com/data");
367
+ *
368
+ * clearTimeout(timeoutId);
369
+ * ```
370
+ */
87
371
  export class TimeoutException extends Exception
88
372
  {
373
+ /**
374
+ * Initializes a new instance of the {@link TimeoutException} class.
375
+ *
376
+ * ---
377
+ *
378
+ * @example
379
+ * ```ts
380
+ * throw new TimeoutException("The task took too long to complete.");
381
+ * ```
382
+ *
383
+ * ---
384
+ *
385
+ * @param message The message that describes the error.
386
+ * @param cause The previous caught error that caused this one, if any.
387
+ * @param name The name of the exception. Default is `"TimeoutException"`.
388
+ */
89
389
  public constructor(message: string, cause?: unknown, name = "TimeoutException")
90
390
  {
91
391
  super(message, cause, name);
92
392
  }
93
393
 
94
- public readonly [Symbol.toStringTag]: string = "TimeoutException";
394
+ public override readonly [Symbol.toStringTag]: string = "TimeoutException";
95
395
  }
396
+
397
+ /**
398
+ * A class representing an exception that can be thrown when a type is invalid or not supported.
399
+ * It's commonly used when a function receives an unexpected type of argument.
400
+ *
401
+ * ```ts
402
+ * function greet(name: string): void
403
+ * {
404
+ * if (typeof name !== "string")
405
+ * {
406
+ * throw new TypeException("The 'name' argument must be a valid string.");
407
+ * }
408
+ * }
409
+ * ```
410
+ */
96
411
  export class TypeException extends Exception
97
412
  {
413
+ /**
414
+ * Initializes a new instance of the {@link TypeException} class.
415
+ *
416
+ * ---
417
+ *
418
+ * @example
419
+ * ```ts
420
+ * throw new TypeException("The 'username' argument must be a valid string.");
421
+ * ```
422
+ *
423
+ * ---
424
+ *
425
+ * @param message The message that describes the error.
426
+ * @param cause The previous caught error that caused this one, if any.
427
+ * @param name The name of the exception. Default is `"TypeException"`.
428
+ */
98
429
  public constructor(message: string, cause?: unknown, name = "TypeException")
99
430
  {
100
431
  super(message, cause, name);
101
432
  }
102
433
 
103
- public readonly [Symbol.toStringTag]: string = "TypeException";
434
+ public override readonly [Symbol.toStringTag]: string = "TypeException";
104
435
  }
105
436
 
437
+ /**
438
+ * A class representing an exception that can be thrown when a value is invalid.
439
+ * It's commonly used when a function receives an unexpected value as an argument.
440
+ *
441
+ * ```ts
442
+ * function setVolume(value: number): void
443
+ * {
444
+ * if (value < 0)
445
+ * {
446
+ * throw new ValueException("The 'value' argument must be greater than or equal to 0.");
447
+ * }
448
+ * }
449
+ * ```
450
+ */
106
451
  export class ValueException extends Exception
107
452
  {
453
+ /**
454
+ * Initializes a new instance of the {@link ValueException} class.
455
+ *
456
+ * ---
457
+ *
458
+ * @example
459
+ * ```ts
460
+ * throw new ValueException("The 'grade' argument cannot be negative.");
461
+ * ```
462
+ *
463
+ * ---
464
+ *
465
+ * @param message The message that describes the error.
466
+ * @param cause The previous caught error that caused this one, if any.
467
+ * @param name The name of the exception. Default is `"ValueException"`.
468
+ */
108
469
  public constructor(message: string, cause?: unknown, name = "ValueException")
109
470
  {
110
471
  super(message, cause, name);
111
472
  }
112
473
 
113
- public readonly [Symbol.toStringTag]: string = "ValueException";
474
+ public override readonly [Symbol.toStringTag]: string = "ValueException";
114
475
  }
476
+
477
+ /**
478
+ * A class representing an exception that can be thrown when a value is out of range.
479
+ * It's commonly used when a function receives an unexpected value as an argument.
480
+ *
481
+ * ```ts
482
+ * function setVolume(value: number): void
483
+ * {
484
+ * if ((value < 0) || (value > 100))
485
+ * {
486
+ * throw new RangeException("The 'value' argument must be between 0 and 100.");
487
+ * }
488
+ * }
489
+ * ```
490
+ */
115
491
  export class RangeException extends ValueException
116
492
  {
493
+ /**
494
+ * Initializes a new instance of the {@link RangeException} class.
495
+ *
496
+ * ---
497
+ *
498
+ * @example
499
+ * ```ts
500
+ * throw new RangeException("The 'percentage' argument must be between 0 and 100.");
501
+ * ```
502
+ *
503
+ * ---
504
+ *
505
+ * @param message The message that describes the error.
506
+ * @param cause The previous caught error that caused this one, if any.
507
+ * @param name The name of the exception. Default is `"RangeException"`.
508
+ */
117
509
  public constructor(message: string, cause?: unknown, name = "RangeException")
118
510
  {
119
511
  super(message, cause, name);
120
512
  }
121
513
 
122
- public readonly [Symbol.toStringTag]: string = "RangeException";
514
+ public override readonly [Symbol.toStringTag]: string = "RangeException";
123
515
  }
124
516
 
125
517
  export { Exception };
@@ -5,11 +5,12 @@ export {
5
5
 
6
6
  } from "./aggregators/index.js";
7
7
 
8
- export { CallableObject, Publisher, SmartFunction, SwitchableCallback } from "./callbacks/index.js";
8
+ export { CallableObject, Publisher, SwitchableCallback } from "./callbacks/index.js";
9
9
  export {
10
10
  Exception,
11
11
  FatalErrorException,
12
12
  NotImplementedException,
13
+ EnvironmentException,
13
14
  FileException,
14
15
  FileExistsException,
15
16
  FileNotFoundException,
@@ -25,12 +26,7 @@ export {
25
26
 
26
27
  } from "./exceptions/index.js";
27
28
 
28
- import GameLoop from "./game-loop.js";
29
-
30
29
  export { SmartIterator, SmartAsyncIterator } from "./iterators/index.js";
31
30
  export { JSONStorage } from "./json/index.js";
32
- export { DeferredPromise, LongRunningTask, SmartPromise, Thenable, TimedPromise } from "./promises/index.js";
33
-
34
- export { Clock, Countdown } from "./timers/index.js";
35
-
36
- export { GameLoop };
31
+ export { DeferredPromise, SmartPromise, TimedPromise } from "./promises/index.js";
32
+ export { Clock, Countdown, GameLoop } from "./timers/index.js";