@byloth/core 2.0.1 → 2.0.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/README.md +1 -0
- package/dist/core.js +18 -8
- package/dist/core.js.map +1 -1
- package/dist/core.umd.cjs +1 -1
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +10 -8
- package/src/core/types.ts +43 -10
- package/src/index.ts +3 -2
- package/src/models/aggregators/aggregated-async-iterator.ts +5 -0
- package/src/models/aggregators/aggregated-iterator.ts +5 -0
- package/src/models/aggregators/reduced-iterator.ts +18 -5
- package/src/models/aggregators/types.ts +35 -0
- package/src/models/callbacks/callable-object.ts +7 -0
- package/src/models/callbacks/publisher.ts +12 -8
- package/src/models/callbacks/switchable-callback.ts +9 -1
- package/src/models/callbacks/types.ts +32 -0
- package/src/models/exceptions/core.ts +9 -0
- package/src/models/exceptions/index.ts +40 -1
- package/src/models/iterators/smart-async-iterator.ts +5 -0
- package/src/models/iterators/smart-iterator.ts +5 -0
- package/src/models/iterators/types.ts +79 -1
- package/src/models/json/json-storage.ts +3 -0
- package/src/models/json/types.ts +1 -1
- package/src/models/promises/deferred-promise.ts +5 -0
- package/src/models/promises/smart-promise.ts +5 -0
- package/src/models/promises/timed-promise.ts +5 -0
- package/src/models/promises/types.ts +30 -0
- package/src/models/timers/clock.ts +3 -0
- package/src/models/timers/countdown.ts +5 -0
- package/src/models/timers/game-loop.ts +3 -0
- package/src/models/types.ts +1 -1
- package/src/utils/async.ts +15 -0
- package/src/utils/curve.ts +1 -1
- package/src/utils/date.ts +36 -6
- package/src/utils/dom.ts +5 -0
- package/src/utils/iterator.ts +40 -0
- package/src/utils/math.ts +15 -0
- package/src/utils/random.ts +4 -0
- package/src/utils/string.ts +5 -0
|
@@ -6,6 +6,9 @@ import Exception from "./core.js";
|
|
|
6
6
|
*
|
|
7
7
|
* It can also be used to catch all file-related exceptions at once.
|
|
8
8
|
*
|
|
9
|
+
* ---
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
9
12
|
* ```ts
|
|
10
13
|
* try { [...] }
|
|
11
14
|
* catch (error)
|
|
@@ -46,6 +49,9 @@ export class FileException extends Exception
|
|
|
46
49
|
/**
|
|
47
50
|
* A class representing an exception that can be thrown when a file already exists.
|
|
48
51
|
*
|
|
52
|
+
* ---
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
49
55
|
* ```ts
|
|
50
56
|
* import { existsSync } from "node:fs";
|
|
51
57
|
*
|
|
@@ -84,6 +90,9 @@ export class FileExistsException extends FileException
|
|
|
84
90
|
/**
|
|
85
91
|
* A class representing an exception that can be thrown when a file isn't found.
|
|
86
92
|
*
|
|
93
|
+
* ---
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
87
96
|
* ```ts
|
|
88
97
|
* import { existsSync } from "node:fs";
|
|
89
98
|
*
|
|
@@ -123,6 +132,9 @@ export class FileNotFoundException extends FileException
|
|
|
123
132
|
* A class representing an exception that can be thrown when a key is invalid or not found.
|
|
124
133
|
* It's commonly used when working with dictionaries, maps, objects, sets, etc...
|
|
125
134
|
*
|
|
135
|
+
* ---
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
126
138
|
* ```ts
|
|
127
139
|
* const map = new Map<string, number>();
|
|
128
140
|
* if (!map.has("hash"))
|
|
@@ -161,6 +173,9 @@ export class KeyException extends Exception
|
|
|
161
173
|
* A class representing an exception that can be thrown when a network operation fails.
|
|
162
174
|
* It's commonly used when it's unable to connect to a server or when a request times out.
|
|
163
175
|
*
|
|
176
|
+
* ---
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
164
179
|
* ```ts
|
|
165
180
|
* import axios, { isAxiosError } from "axios";
|
|
166
181
|
*
|
|
@@ -205,8 +220,11 @@ export class NetworkException extends Exception
|
|
|
205
220
|
|
|
206
221
|
/**
|
|
207
222
|
* A class representing an exception that can be thrown when a permission is denied.
|
|
208
|
-
* It's commonly used when
|
|
223
|
+
* It's commonly used when an user tries to access a restricted resource or perform a forbidden action.
|
|
224
|
+
*
|
|
225
|
+
* ---
|
|
209
226
|
*
|
|
227
|
+
* @example
|
|
210
228
|
* ```ts
|
|
211
229
|
* const $user = useUserStore();
|
|
212
230
|
* if (!$user.isAdmin)
|
|
@@ -245,6 +263,9 @@ export class PermissionException extends Exception
|
|
|
245
263
|
* A class representing an exception that can be thrown when a reference is invalid or not found.
|
|
246
264
|
* It's commonly used when a variable is `null`, `undefined` or when an object doesn't exist.
|
|
247
265
|
*
|
|
266
|
+
* ---
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
248
269
|
* ```ts
|
|
249
270
|
* const $el = document.getElementById("app");
|
|
250
271
|
* if ($el === null)
|
|
@@ -283,6 +304,9 @@ export class ReferenceException extends Exception
|
|
|
283
304
|
* A class representing an exception that can be thrown when a runtime error occurs.
|
|
284
305
|
* It's commonly used when an unexpected condition is encountered during the execution of a program.
|
|
285
306
|
*
|
|
307
|
+
* ---
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
286
310
|
* ```ts
|
|
287
311
|
* let status: "enabled" | "disabled" = "enabled";
|
|
288
312
|
*
|
|
@@ -324,6 +348,9 @@ export class RuntimeException extends Exception
|
|
|
324
348
|
* isn't properly configured or when a required variable isn't set.
|
|
325
349
|
* It can also be used when the environment on which the program is running is unsupported.
|
|
326
350
|
*
|
|
351
|
+
* ---
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
327
354
|
* ```ts
|
|
328
355
|
* if (!navigator.geolocation)
|
|
329
356
|
* {
|
|
@@ -361,6 +388,9 @@ export class EnvironmentException extends RuntimeException
|
|
|
361
388
|
* A class representing an exception that can be thrown when a timeout occurs.
|
|
362
389
|
* It's commonly used when a task takes too long to complete or when a request times out.
|
|
363
390
|
*
|
|
391
|
+
* ---
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
364
394
|
* ```ts
|
|
365
395
|
* const timeoutId = setTimeout(() => { throw new TimeoutException("The request timed out."); }, 5_000);
|
|
366
396
|
* const response = await fetch("https://api.example.com/data");
|
|
@@ -398,6 +428,9 @@ export class TimeoutException extends Exception
|
|
|
398
428
|
* A class representing an exception that can be thrown when a type is invalid or not supported.
|
|
399
429
|
* It's commonly used when a function receives an unexpected type of argument.
|
|
400
430
|
*
|
|
431
|
+
* ---
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
401
434
|
* ```ts
|
|
402
435
|
* function greet(name: string): void
|
|
403
436
|
* {
|
|
@@ -438,6 +471,9 @@ export class TypeException extends Exception
|
|
|
438
471
|
* A class representing an exception that can be thrown when a value is invalid.
|
|
439
472
|
* It's commonly used when a function receives an unexpected value as an argument.
|
|
440
473
|
*
|
|
474
|
+
* ---
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
441
477
|
* ```ts
|
|
442
478
|
* function setVolume(value: number): void
|
|
443
479
|
* {
|
|
@@ -478,6 +514,9 @@ export class ValueException extends Exception
|
|
|
478
514
|
* A class representing an exception that can be thrown when a value is out of range.
|
|
479
515
|
* It's commonly used when a function receives an unexpected value as an argument.
|
|
480
516
|
*
|
|
517
|
+
* ---
|
|
518
|
+
*
|
|
519
|
+
* @example
|
|
481
520
|
* ```ts
|
|
482
521
|
* function setVolume(value: number): void
|
|
483
522
|
* {
|
|
@@ -26,6 +26,9 @@ import type {
|
|
|
26
26
|
* This allows to chain multiple transformations without
|
|
27
27
|
* the need to iterate over the elements multiple times.
|
|
28
28
|
*
|
|
29
|
+
* ---
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
29
32
|
* ```ts
|
|
30
33
|
* const result = new SmartAsyncIterator<number>(["-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5"])
|
|
31
34
|
* .map((value) => Number(value))
|
|
@@ -37,6 +40,8 @@ import type {
|
|
|
37
40
|
* console.log(await result); // 31
|
|
38
41
|
* ```
|
|
39
42
|
*
|
|
43
|
+
* ---
|
|
44
|
+
*
|
|
40
45
|
* @template T The type of elements in the iterator.
|
|
41
46
|
* @template R The type of the final result of the iterator. Default is `void`.
|
|
42
47
|
* @template N The type of the argument passed to the `next` method. Default is `undefined`.
|
|
@@ -17,6 +17,9 @@ import type { GeneratorFunction, Iteratee, TypeGuardPredicate, Reducer, Iterator
|
|
|
17
17
|
* This allows to chain multiple transformations without
|
|
18
18
|
* the need to iterate over the elements multiple times.
|
|
19
19
|
*
|
|
20
|
+
* ---
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
20
23
|
* ```ts
|
|
21
24
|
* const result = new SmartIterator<number>(["-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5"])
|
|
22
25
|
* .map(Number)
|
|
@@ -28,6 +31,8 @@ import type { GeneratorFunction, Iteratee, TypeGuardPredicate, Reducer, Iterator
|
|
|
28
31
|
* console.log(result); // 31
|
|
29
32
|
* ```
|
|
30
33
|
*
|
|
34
|
+
* ---
|
|
35
|
+
*
|
|
31
36
|
* @template T The type of elements in the iterator.
|
|
32
37
|
* @template R The type of the final result of the iterator. Default is `void`.
|
|
33
38
|
* @template N The type of the argument required by the `next` method. Default is `undefined`.
|
|
@@ -3,6 +3,9 @@ import type { MaybePromise } from "../promises/types.js";
|
|
|
3
3
|
/**
|
|
4
4
|
* An union type that represents an iterable object that can be either synchronous or asynchronous.
|
|
5
5
|
*
|
|
6
|
+
* ---
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
6
9
|
* ```ts
|
|
7
10
|
* const iterable: MaybeAsyncIterable<number> = [...];
|
|
8
11
|
* for await (const value of iterable)
|
|
@@ -11,6 +14,8 @@ import type { MaybePromise } from "../promises/types.js";
|
|
|
11
14
|
* }
|
|
12
15
|
* ```
|
|
13
16
|
*
|
|
17
|
+
* ---
|
|
18
|
+
*
|
|
14
19
|
* @template T The type of the elements in the iterable.
|
|
15
20
|
*/
|
|
16
21
|
export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> | AsyncIterable<T, R, N>;
|
|
@@ -18,6 +23,9 @@ export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> |
|
|
|
18
23
|
/**
|
|
19
24
|
* An union type that represents an iterator object that can be either synchronous or asynchronous.
|
|
20
25
|
*
|
|
26
|
+
* ---
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
21
29
|
* ```ts
|
|
22
30
|
* const iterator: MaybeAsyncIterator<number> = { ... };
|
|
23
31
|
* for await (const value of iterator)
|
|
@@ -26,6 +34,8 @@ export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> |
|
|
|
26
34
|
* }
|
|
27
35
|
* ```
|
|
28
36
|
*
|
|
37
|
+
* ---
|
|
38
|
+
*
|
|
29
39
|
* @template T The type of the elements in the iterator.
|
|
30
40
|
*/
|
|
31
41
|
export type MaybeAsyncIterator<T, R = void, N = undefined> = Iterator<T, R, N> | AsyncIterator<T, R, N>;
|
|
@@ -33,6 +43,9 @@ export type MaybeAsyncIterator<T, R = void, N = undefined> = Iterator<T, R, N> |
|
|
|
33
43
|
/**
|
|
34
44
|
* An union type that represents a generator object that can be either synchronous or asynchronous.
|
|
35
45
|
*
|
|
46
|
+
* ---
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
36
49
|
* ```ts
|
|
37
50
|
* const generator: MaybeAsyncGenerator<number> = [async] function*() { ... };
|
|
38
51
|
* for await (const value of generator)
|
|
@@ -46,6 +59,9 @@ export type MaybeAsyncGenerator<T, R = void, N = undefined> = Generator<T, R, N>
|
|
|
46
59
|
* An utility type that represents a function that returns a generator object.
|
|
47
60
|
* It differs from the native `GeneratorFunction` type by allowing to specify the types of the returned generator.
|
|
48
61
|
*
|
|
62
|
+
* ---
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
49
65
|
* ```ts
|
|
50
66
|
* const generatorFn: GeneratorFunction<number> = function*() { ... };
|
|
51
67
|
* const generator: Generator<number> = generatorFn();
|
|
@@ -56,6 +72,8 @@ export type MaybeAsyncGenerator<T, R = void, N = undefined> = Generator<T, R, N>
|
|
|
56
72
|
* }
|
|
57
73
|
* ```
|
|
58
74
|
*
|
|
75
|
+
* ---
|
|
76
|
+
*
|
|
59
77
|
* @template T The type of the elements generated by the generator.
|
|
60
78
|
* @template R The type of the return value of the generator. Default is `void`.
|
|
61
79
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -66,6 +84,9 @@ export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R
|
|
|
66
84
|
* An utility type that represents a function that returns an asynchronous generator object.
|
|
67
85
|
* It differs from the native `AsyncGeneratorFunction` type by allowing to specify the types of the returned generator.
|
|
68
86
|
*
|
|
87
|
+
* ---
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
69
90
|
* ```ts
|
|
70
91
|
* const asyncGeneratorFn: AsyncGeneratorFunction<number> = async function*() { ... };
|
|
71
92
|
* const generator: AsyncGenerator<number> = asyncGeneratorFn();
|
|
@@ -75,6 +96,8 @@ export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R
|
|
|
75
96
|
* }
|
|
76
97
|
* ```
|
|
77
98
|
*
|
|
99
|
+
* ---
|
|
100
|
+
*
|
|
78
101
|
* @template T The type of the elements generated by the generator.
|
|
79
102
|
* @template R The type of the return value of the generator. Default is `void`.
|
|
80
103
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -85,6 +108,9 @@ export type AsyncGeneratorFunction<T, R = void, N = undefined> = () => AsyncGene
|
|
|
85
108
|
* An utility type that represents a function that returns a
|
|
86
109
|
* generator object that can be either synchronous or asynchronous.
|
|
87
110
|
*
|
|
111
|
+
* ---
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
88
114
|
* ```ts
|
|
89
115
|
* const generatorFn: MaybeAsyncGeneratorFunction<number> = [async] function*() { ... };
|
|
90
116
|
* const generator: MaybeAsyncGenerator<number> = generatorFn();
|
|
@@ -94,6 +120,8 @@ export type AsyncGeneratorFunction<T, R = void, N = undefined> = () => AsyncGene
|
|
|
94
120
|
* }
|
|
95
121
|
* ```
|
|
96
122
|
*
|
|
123
|
+
* ---
|
|
124
|
+
*
|
|
97
125
|
* @template T The type of the elements generated by the generator.
|
|
98
126
|
* @template R The type of the return value of the generator. Default is `void`.
|
|
99
127
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -105,13 +133,18 @@ export type MaybeAsyncGeneratorFunction<T, R = void, N = undefined> = () => Mayb
|
|
|
105
133
|
* {@link https://en.wikipedia.org/wiki/Iteratee|iteratee} function.
|
|
106
134
|
* It can be used to transform the elements of an iterable.
|
|
107
135
|
*
|
|
136
|
+
* ---
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
108
139
|
* ```ts
|
|
109
140
|
* const iteratee: Iteratee<number, string> = (value: number) => `${value}`;
|
|
110
141
|
* const values: string[] = [1, 2, 3, 4, 5].map(iteratee);
|
|
111
|
-
*
|
|
142
|
+
*
|
|
112
143
|
* console.log(values); // ["1", "2", "3", "4", "5"]
|
|
113
144
|
* ```
|
|
114
145
|
*
|
|
146
|
+
* ---
|
|
147
|
+
*
|
|
115
148
|
* @template T The type of the elements in the iterable.
|
|
116
149
|
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
117
150
|
*/
|
|
@@ -121,6 +154,9 @@ export type Iteratee<T, R = void> = (value: T, index: number) => R;
|
|
|
121
154
|
* An utility type that represents an asynchronous {@link https://en.wikipedia.org/wiki/Iteratee|iteratee} function.
|
|
122
155
|
* It can be used to transform the elements of an iterable asynchronously.
|
|
123
156
|
*
|
|
157
|
+
* ---
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
124
160
|
* ```ts
|
|
125
161
|
* const iteratee: AsyncIteratee<number, string> = async (value: number) => `${value}`;
|
|
126
162
|
* const values: Promise<string>[] = [1, 2, 3, 4, 5].map(iteratee);
|
|
@@ -130,6 +166,8 @@ export type Iteratee<T, R = void> = (value: T, index: number) => R;
|
|
|
130
166
|
* }
|
|
131
167
|
* ```
|
|
132
168
|
*
|
|
169
|
+
* ---
|
|
170
|
+
*
|
|
133
171
|
* @template T The type of the elements in the iterable.
|
|
134
172
|
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
135
173
|
*/
|
|
@@ -140,6 +178,9 @@ export type AsyncIteratee<T, R = void> = (value: T, index: number) => Promise<R>
|
|
|
140
178
|
* function that can be either synchronous or asynchronous.
|
|
141
179
|
* It can be used to transform the elements of an iterable.
|
|
142
180
|
*
|
|
181
|
+
* ---
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
143
184
|
* ```ts
|
|
144
185
|
* const iteratee: MaybeAsyncIteratee<number, string> = [async] (value: number) => `${value}`;
|
|
145
186
|
* const values: Promise<string>[] = [1, 2, 3, 4, 5].map(iteratee);
|
|
@@ -149,6 +190,8 @@ export type AsyncIteratee<T, R = void> = (value: T, index: number) => Promise<R>
|
|
|
149
190
|
* }
|
|
150
191
|
* ```
|
|
151
192
|
*
|
|
193
|
+
* ---
|
|
194
|
+
*
|
|
152
195
|
* @template T The type of the elements in the iterable.
|
|
153
196
|
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
154
197
|
*/
|
|
@@ -161,6 +204,9 @@ export type MaybeAsyncIteratee<T, R = void> = (value: T, index: number) => Maybe
|
|
|
161
204
|
* It can be used to ensure the type of the elements of an iterable
|
|
162
205
|
* while allowing the type-system to infer them correctly.
|
|
163
206
|
*
|
|
207
|
+
* ---
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
164
210
|
* ```ts
|
|
165
211
|
* const iteratee: TypeGuardPredicate<number | string, string> = (value): value is string => typeof value === "string";
|
|
166
212
|
* const values: string[] = [1, "2", 3, "4", 5].filter(iteratee);
|
|
@@ -170,6 +216,8 @@ export type MaybeAsyncIteratee<T, R = void> = (value: T, index: number) => Maybe
|
|
|
170
216
|
* }
|
|
171
217
|
* ```
|
|
172
218
|
*
|
|
219
|
+
* ---
|
|
220
|
+
*
|
|
173
221
|
* @template T The type of the elements in the iterable.
|
|
174
222
|
* @template R
|
|
175
223
|
* The type of the elements that pass the type guard.
|
|
@@ -186,6 +234,9 @@ export type TypeGuardPredicate<T, R extends T> = (value: T, index: number) => va
|
|
|
186
234
|
* An utility type that represents a reducer function.
|
|
187
235
|
* It can be used to reduce the elements of an iterable into a single value.
|
|
188
236
|
*
|
|
237
|
+
* ---
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
189
240
|
* ```ts
|
|
190
241
|
* const sum: Reducer<number, number> = (accumulator, value) => accumulator + value;
|
|
191
242
|
* const total: number = [1, 2, 3, 4, 5].reduce(sum);
|
|
@@ -193,6 +244,8 @@ export type TypeGuardPredicate<T, R extends T> = (value: T, index: number) => va
|
|
|
193
244
|
* console.log(total); // 15
|
|
194
245
|
* ```
|
|
195
246
|
*
|
|
247
|
+
* ---
|
|
248
|
+
*
|
|
196
249
|
* @template T The type of the elements in the iterable.
|
|
197
250
|
* @template A The type of the accumulator.
|
|
198
251
|
*/
|
|
@@ -202,6 +255,9 @@ export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
|
|
|
202
255
|
* An utility type that represents an asynchronous reducer function.
|
|
203
256
|
* It can be used to reduce the elements of an iterable into a single value.
|
|
204
257
|
*
|
|
258
|
+
* ---
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
205
261
|
* ```ts
|
|
206
262
|
* const sum: AsyncReducer<number, number> = async (accumulator, value) => accumulator + value;
|
|
207
263
|
* const result = await new SmartAsyncIterator<number>([1, 2, 3, 4, 5]).reduce(sum);
|
|
@@ -209,6 +265,8 @@ export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
|
|
|
209
265
|
* console.log(result); // 15
|
|
210
266
|
* ```
|
|
211
267
|
*
|
|
268
|
+
* ---
|
|
269
|
+
*
|
|
212
270
|
* @template T The type of the elements in the iterable.
|
|
213
271
|
* @template A The type of the accumulator.
|
|
214
272
|
*/
|
|
@@ -218,6 +276,9 @@ export type AsyncReducer<T, A> = (accumulator: A, value: T, index: number) => Pr
|
|
|
218
276
|
* An utility type that represents a reducer function that can be either synchronous or asynchronous.
|
|
219
277
|
* It can be used to reduce the elements of an iterable into a single value.
|
|
220
278
|
*
|
|
279
|
+
* ---
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
221
282
|
* ```ts
|
|
222
283
|
* const sum: MaybeAsyncReducer<number, number> = [async] (accumulator, value) => accumulator + value;
|
|
223
284
|
* const result = await new SmartAsyncIterator<number>([1, 2, 3, 4, 5]).reduce(sum);
|
|
@@ -225,6 +286,8 @@ export type AsyncReducer<T, A> = (accumulator: A, value: T, index: number) => Pr
|
|
|
225
286
|
* console.log(result); // 15
|
|
226
287
|
* ```
|
|
227
288
|
*
|
|
289
|
+
* ---
|
|
290
|
+
*
|
|
228
291
|
* @template T The type of the elements in the iterable.
|
|
229
292
|
* @template A The type of the accumulator.
|
|
230
293
|
*/
|
|
@@ -234,6 +297,9 @@ export type MaybeAsyncReducer<T, A> = (accumulator: A, value: T, index: number)
|
|
|
234
297
|
* An union type that represents either an iterable or an iterator object.
|
|
235
298
|
* More in general, it represents an object that can be looped over in one way or another.
|
|
236
299
|
*
|
|
300
|
+
* ---
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
237
303
|
* ```ts
|
|
238
304
|
* const elements: IteratorLike<number> = { ... };
|
|
239
305
|
* const iterator: SmartIterator<number> = new SmartIterator(elements);
|
|
@@ -243,6 +309,8 @@ export type MaybeAsyncReducer<T, A> = (accumulator: A, value: T, index: number)
|
|
|
243
309
|
* }
|
|
244
310
|
* ```
|
|
245
311
|
*
|
|
312
|
+
* ---
|
|
313
|
+
*
|
|
246
314
|
* @template T The type of the elements in the iterable.
|
|
247
315
|
* @template R The type of the return value of the iterator. Default is `void`.
|
|
248
316
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -253,6 +321,9 @@ export type IteratorLike<T, R = void, N = undefined> = Iterable<T, R, N> | Itera
|
|
|
253
321
|
* An union type that represents either an iterable or an iterator object that can be asynchronous.
|
|
254
322
|
* More in general, it represents an object that can be looped over in one way or another.
|
|
255
323
|
*
|
|
324
|
+
* ---
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
256
327
|
* ```ts
|
|
257
328
|
* const elements: AsyncIteratorLike<number> = { ... };
|
|
258
329
|
* const iterator: SmartAsyncIterator<number> = new SmartAsyncIterator(elements);
|
|
@@ -262,6 +333,8 @@ export type IteratorLike<T, R = void, N = undefined> = Iterable<T, R, N> | Itera
|
|
|
262
333
|
* }
|
|
263
334
|
* ```
|
|
264
335
|
*
|
|
336
|
+
* ---
|
|
337
|
+
*
|
|
265
338
|
* @template T The type of the elements in the iterable.
|
|
266
339
|
* @template R The type of the return value of the iterator. Default is `void`.
|
|
267
340
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -273,6 +346,9 @@ export type AsyncIteratorLike<T, R = void, N = undefined> = AsyncIterable<T, R,
|
|
|
273
346
|
* object that can be either synchronous or asynchronous.
|
|
274
347
|
* More in general, it represents an object that can be looped over in one way or another.
|
|
275
348
|
*
|
|
349
|
+
* ---
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
276
352
|
* ```ts
|
|
277
353
|
* const elements: MaybeAsyncIteratorLike<number> = { ... };
|
|
278
354
|
* const iterator: SmartAsyncIterator<number> = new SmartAsyncIterator(elements);
|
|
@@ -282,6 +358,8 @@ export type AsyncIteratorLike<T, R = void, N = undefined> = AsyncIterable<T, R,
|
|
|
282
358
|
* }
|
|
283
359
|
* ```
|
|
284
360
|
*
|
|
361
|
+
* ---
|
|
362
|
+
*
|
|
285
363
|
* @template T The type of the elements in the iterable.
|
|
286
364
|
* @template R The type of the return value of the iterator. Default is `void`.
|
|
287
365
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -10,6 +10,9 @@ import type { JSONValue } from "./types.js";
|
|
|
10
10
|
* It allows to handle either the volatile {@link sessionStorage} or the persistent
|
|
11
11
|
* {@link localStorage} at the same time, depending on what's your required use case.
|
|
12
12
|
*
|
|
13
|
+
* ---
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
13
16
|
* ```ts
|
|
14
17
|
* const jsonStorage = new JSONStorage();
|
|
15
18
|
*
|
package/src/models/json/types.ts
CHANGED
|
@@ -6,7 +6,7 @@ export type JSONArray = JSONValue[];
|
|
|
6
6
|
/**
|
|
7
7
|
* A type that represents a JSON object.
|
|
8
8
|
*/
|
|
9
|
-
export
|
|
9
|
+
export type JSONObject<T extends object = object> = { [K in keyof T]: JSONValue };
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* A type that represents all the possible values of a JSON value.
|
|
@@ -12,6 +12,9 @@ import SmartPromise from "./smart-promise.js";
|
|
|
12
12
|
* This is a change in the approach to promises: instead of defining how the promise will be resolved (or rejected),
|
|
13
13
|
* you define how to handle the resolution (or rejection) when it occurs.
|
|
14
14
|
*
|
|
15
|
+
* ---
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
15
18
|
* ```ts
|
|
16
19
|
* const promise = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
|
|
17
20
|
*
|
|
@@ -19,6 +22,8 @@ import SmartPromise from "./smart-promise.js";
|
|
|
19
22
|
* promise.resolve("Hello, World!");
|
|
20
23
|
* ```
|
|
21
24
|
*
|
|
25
|
+
* ---
|
|
26
|
+
*
|
|
22
27
|
* @template T The type of value the promise expects to initially be resolved with. Default is `void`.
|
|
23
28
|
* @template F
|
|
24
29
|
* The type of value returned by the `onFulfilled` callback.
|
|
@@ -7,6 +7,9 @@ import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "./types
|
|
|
7
7
|
* The state can be either `pending`, `fulfilled` or `rejected` and is accessible through
|
|
8
8
|
* the {@link SmartPromise.isPending}, {@link SmartPromise.isFulfilled} & {@link SmartPromise.isRejected} properties.
|
|
9
9
|
*
|
|
10
|
+
* ---
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
10
13
|
* ```ts
|
|
11
14
|
* const promise = new SmartPromise<string>((resolve, reject) =>
|
|
12
15
|
* {
|
|
@@ -22,6 +25,8 @@ import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "./types
|
|
|
22
25
|
* console.log(promise.isFulfilled); // true
|
|
23
26
|
* ```
|
|
24
27
|
*
|
|
28
|
+
* ---
|
|
29
|
+
*
|
|
25
30
|
* @template T The type of value the promise will eventually resolve to. Default is `void`.
|
|
26
31
|
*/
|
|
27
32
|
export default class SmartPromise<T = void> implements Promise<T>
|
|
@@ -9,6 +9,9 @@ import type { MaybePromise, PromiseExecutor } from "./types.js";
|
|
|
9
9
|
*
|
|
10
10
|
* If the operation takes longer than the specified time, the promise is rejected with a {@link TimeoutException}.
|
|
11
11
|
*
|
|
12
|
+
* ---
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
12
15
|
* ```ts
|
|
13
16
|
* const promise = new TimedPromise<string>((resolve, reject) =>
|
|
14
17
|
* {
|
|
@@ -21,6 +24,8 @@ import type { MaybePromise, PromiseExecutor } from "./types.js";
|
|
|
21
24
|
* .catch((error) => console.error(error)); // TimeoutException: The operation has timed out.
|
|
22
25
|
* ```
|
|
23
26
|
*
|
|
27
|
+
* ---
|
|
28
|
+
*
|
|
24
29
|
* @template T The type of value the promise will eventually resolve to. Default is `void`.
|
|
25
30
|
*/
|
|
26
31
|
export default class TimedPromise<T = void> extends SmartPromise<T>
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
* An union type that represents a value that can be either a value or a promise of that value.
|
|
3
3
|
* This is useful when you want to handle both synchronous and asynchronous values in the same way.
|
|
4
4
|
*
|
|
5
|
+
* ---
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
5
8
|
* ```ts
|
|
6
9
|
* async function splitWords(value: MaybePromise<string>): Promise<string[]>
|
|
7
10
|
* {
|
|
@@ -9,6 +12,8 @@
|
|
|
9
12
|
* }
|
|
10
13
|
* ```
|
|
11
14
|
*
|
|
15
|
+
* ---
|
|
16
|
+
*
|
|
12
17
|
* @template T The type of the value.
|
|
13
18
|
*/
|
|
14
19
|
export type MaybePromise<T> = T | PromiseLike<T>;
|
|
@@ -17,6 +22,9 @@ export type MaybePromise<T> = T | PromiseLike<T>;
|
|
|
17
22
|
* An utility type that represents the callback that is executed when a promise is fulfilled.
|
|
18
23
|
* It's compatible with the `onFulfilled` parameter of the `then` method of the native {@link Promise} object.
|
|
19
24
|
*
|
|
25
|
+
* ---
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
20
28
|
* ```ts
|
|
21
29
|
* const onFulfilled: FulfilledHandler<string, string[]> = (value) => value.split(" ");
|
|
22
30
|
*
|
|
@@ -24,6 +32,8 @@ export type MaybePromise<T> = T | PromiseLike<T>;
|
|
|
24
32
|
* .then(onFulfilled);
|
|
25
33
|
* ```
|
|
26
34
|
*
|
|
35
|
+
* ---
|
|
36
|
+
*
|
|
27
37
|
* @template T The type of value accepted by the function. Default is `void`.
|
|
28
38
|
* @template R The type of value returned by the function. Default is `T`.
|
|
29
39
|
*/
|
|
@@ -33,6 +43,9 @@ export type FulfilledHandler<T = void, R = T> = (value: T) => MaybePromise<R>;
|
|
|
33
43
|
* An utility type that represents the callback that is executed when a promise is rejected.
|
|
34
44
|
* It's compatible with the `onRejected` parameter of the `then`/`catch` methods of the native {@link Promise} object.
|
|
35
45
|
*
|
|
46
|
+
* ---
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
36
49
|
* ```ts
|
|
37
50
|
* const onRejected: RejectedHandler<unknown, string> = (reason) => String(reason);
|
|
38
51
|
*
|
|
@@ -40,6 +53,8 @@ export type FulfilledHandler<T = void, R = T> = (value: T) => MaybePromise<R>;
|
|
|
40
53
|
* .catch(onRejected);
|
|
41
54
|
* ```
|
|
42
55
|
*
|
|
56
|
+
* ---
|
|
57
|
+
*
|
|
43
58
|
* @template E The type of value accepted by the function. Default is `unknown`.
|
|
44
59
|
* @template R The type of value returned by the function. Default is `never`.
|
|
45
60
|
*/
|
|
@@ -49,12 +64,17 @@ export type RejectedHandler<E = unknown, R = never> = (reason: E) => MaybePromis
|
|
|
49
64
|
* An utility type that represents a function that can be used to resolve a promise.
|
|
50
65
|
* It's compatible with the `resolve` parameter of the native {@link Promise} executor.
|
|
51
66
|
*
|
|
67
|
+
* ---
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
52
70
|
* ```ts
|
|
53
71
|
* let _resolve: PromiseResolver<string> = (result) => console.log(result);
|
|
54
72
|
*
|
|
55
73
|
* await new Promise<string>((resolve) => { _resolve = resolve; });
|
|
56
74
|
* ```
|
|
57
75
|
*
|
|
76
|
+
* ---
|
|
77
|
+
*
|
|
58
78
|
* @template T The type of the value accepted by the function. Default is `void`.
|
|
59
79
|
*/
|
|
60
80
|
export type PromiseResolver<T = void> = (result: MaybePromise<T>) => void;
|
|
@@ -63,12 +83,17 @@ export type PromiseResolver<T = void> = (result: MaybePromise<T>) => void;
|
|
|
63
83
|
* An utility type that represents a function that can be used to reject a promise.
|
|
64
84
|
* It's compatible with the `reject` parameter of the native {@link Promise} executor.
|
|
65
85
|
*
|
|
86
|
+
* ---
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
66
89
|
* ```ts
|
|
67
90
|
* let _reject: PromiseRejecter<string> = (reason) => console.error(reason);
|
|
68
91
|
*
|
|
69
92
|
* await new Promise<string>((_, reject) => { _reject = reject; });
|
|
70
93
|
* ```
|
|
71
94
|
*
|
|
95
|
+
* ---
|
|
96
|
+
*
|
|
72
97
|
* @template E The type of the value accepted by the function. Default is `unknown`.
|
|
73
98
|
*/
|
|
74
99
|
export type PromiseRejecter<E = unknown> = (reason?: MaybePromise<E>) => void;
|
|
@@ -77,6 +102,9 @@ export type PromiseRejecter<E = unknown> = (reason?: MaybePromise<E>) => void;
|
|
|
77
102
|
* An utility type that represents the function that will be executed by the promise.
|
|
78
103
|
* It's compatible with the `executor` parameter of the native {@link Promise} object.
|
|
79
104
|
*
|
|
105
|
+
* ---
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
80
108
|
* ```ts
|
|
81
109
|
* const executor: PromiseExecutor<string> = (resolve, reject) =>
|
|
82
110
|
* {
|
|
@@ -86,6 +114,8 @@ export type PromiseRejecter<E = unknown> = (reason?: MaybePromise<E>) => void;
|
|
|
86
114
|
* await new Promise<string>(executor);
|
|
87
115
|
* ```
|
|
88
116
|
*
|
|
117
|
+
* ---
|
|
118
|
+
*
|
|
89
119
|
* @template T The type of value accepted by the `resolve` function. Default is `void`.
|
|
90
120
|
* @template E The type of value accepted by the `reject` function. Default is `unknown`.
|
|
91
121
|
*/
|
|
@@ -22,6 +22,9 @@ interface ClockEventMap
|
|
|
22
22
|
* It can be started, stopped and, when running, it ticks at a specific frame rate.
|
|
23
23
|
* It's possible to subscribe to these events to receive notifications when they occur.
|
|
24
24
|
*
|
|
25
|
+
* ---
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
25
28
|
* ```ts
|
|
26
29
|
* const clock = new Clock();
|
|
27
30
|
*
|
|
@@ -24,6 +24,9 @@ interface CountdownEventMap
|
|
|
24
24
|
* It can be started, stopped, when running it ticks at a specific frame rate and it expires when the time's up.
|
|
25
25
|
* It's possible to subscribe to these events to receive notifications when they occur.
|
|
26
26
|
*
|
|
27
|
+
* ---
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
27
30
|
* ```ts
|
|
28
31
|
* const countdown = new Countdown(10_000);
|
|
29
32
|
*
|
|
@@ -119,6 +122,8 @@ export default class Countdown extends GameLoop
|
|
|
119
122
|
* The internal method actually responsible for stopping the
|
|
120
123
|
* countdown and resolving or rejecting the {@link Countdown._deferrer} promise.
|
|
121
124
|
*
|
|
125
|
+
* ---
|
|
126
|
+
*
|
|
122
127
|
* @param reason
|
|
123
128
|
* The reason why the countdown has stopped.
|
|
124
129
|
*
|
|
@@ -27,6 +27,9 @@ interface GameLoopEventMap
|
|
|
27
27
|
* elapsed time since the start of the game loop.
|
|
28
28
|
* It's also possible to subscribe to the `start` & `stop` events to receive notifications when they occur.
|
|
29
29
|
*
|
|
30
|
+
* ---
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
30
33
|
* ```ts
|
|
31
34
|
* const loop = new GameLoop((elapsedTime: number) =>
|
|
32
35
|
* {
|
package/src/models/types.ts
CHANGED