@byloth/core 2.0.0 → 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.
Files changed (39) hide show
  1. package/README.md +1 -0
  2. package/dist/core.js +900 -187
  3. package/dist/core.js.map +1 -1
  4. package/dist/core.umd.cjs +2 -2
  5. package/dist/core.umd.cjs.map +1 -1
  6. package/package.json +13 -10
  7. package/src/core/types.ts +43 -10
  8. package/src/index.ts +3 -2
  9. package/src/models/aggregators/aggregated-async-iterator.ts +161 -1
  10. package/src/models/aggregators/aggregated-iterator.ts +146 -1
  11. package/src/models/aggregators/reduced-iterator.ts +148 -8
  12. package/src/models/aggregators/types.ts +35 -0
  13. package/src/models/callbacks/callable-object.ts +7 -0
  14. package/src/models/callbacks/publisher.ts +33 -8
  15. package/src/models/callbacks/switchable-callback.ts +102 -21
  16. package/src/models/callbacks/types.ts +32 -0
  17. package/src/models/exceptions/core.ts +29 -0
  18. package/src/models/exceptions/index.ts +105 -1
  19. package/src/models/iterators/smart-async-iterator.ts +145 -0
  20. package/src/models/iterators/smart-iterator.ts +130 -0
  21. package/src/models/iterators/types.ts +79 -1
  22. package/src/models/json/json-storage.ts +123 -0
  23. package/src/models/json/types.ts +1 -1
  24. package/src/models/promises/deferred-promise.ts +15 -0
  25. package/src/models/promises/smart-promise.ts +45 -0
  26. package/src/models/promises/timed-promise.ts +10 -0
  27. package/src/models/promises/types.ts +30 -0
  28. package/src/models/timers/clock.ts +21 -0
  29. package/src/models/timers/countdown.ts +30 -0
  30. package/src/models/timers/game-loop.ts +26 -0
  31. package/src/models/types.ts +1 -1
  32. package/src/utils/async.ts +15 -0
  33. package/src/utils/curve.ts +11 -1
  34. package/src/utils/date.ts +36 -6
  35. package/src/utils/dom.ts +5 -0
  36. package/src/utils/iterator.ts +40 -0
  37. package/src/utils/math.ts +15 -0
  38. package/src/utils/random.ts +34 -0
  39. package/src/utils/string.ts +5 -0
@@ -1,7 +1,7 @@
1
1
  import { SmartIterator, ValueException } from "../models/index.js";
2
2
 
3
3
  /**
4
- * A utility class that provides a set of methods to generate sequences of numbers following specific curves.
4
+ * An utility class that provides a set of methods to generate sequences of numbers following specific curves.
5
5
  * It can be used to generate sequences of values that can be
6
6
  * used in animations, transitions and other different scenarios.
7
7
  *
@@ -13,6 +13,9 @@ export default class Curve
13
13
  * Generates a given number of values following a linear curve.
14
14
  * The values are equally spaced and normalized between 0 and 1.
15
15
  *
16
+ * ---
17
+ *
18
+ * @example
16
19
  * ```ts
17
20
  * for (const value of Curve.Linear(5))
18
21
  * {
@@ -20,6 +23,8 @@ export default class Curve
20
23
  * }
21
24
  * ```
22
25
  *
26
+ * ---
27
+ *
23
28
  * @param values The number of values to generate.
24
29
  *
25
30
  * @returns A {@link SmartIterator} object that generates the values following a linear curve.
@@ -38,6 +43,9 @@ export default class Curve
38
43
  * Generates a given number of values following an exponential curve.
39
44
  * The values are equally spaced and normalized between 0 and 1.
40
45
  *
46
+ * ---
47
+ *
48
+ * @example
41
49
  * ```ts
42
50
  * for (const value of Curve.Exponential(6))
43
51
  * {
@@ -45,6 +53,8 @@ export default class Curve
45
53
  * }
46
54
  * ```
47
55
  *
56
+ * ---
57
+ *
48
58
  * @param values The number of values to generate.
49
59
  * @param base
50
60
  * The base of the exponential curve. Default is `2`.
package/src/utils/date.ts CHANGED
@@ -6,6 +6,9 @@ import { RangeException, SmartIterator } from "../models/index.js";
6
6
  * It can be used as utility to express time values in a more
7
7
  * readable way or to convert time values between different units.
8
8
  *
9
+ * ---
10
+ *
11
+ * @example
9
12
  * ```ts
10
13
  * setTimeout(() => { [...] }, 5 * TimeUnit.Minute);
11
14
  * ```
@@ -59,6 +62,9 @@ export enum TimeUnit
59
62
  * An enumeration that represents the days of the week.
60
63
  * It can be used as utility to identify the days of the week when working with dates.
61
64
  *
65
+ * ---
66
+ *
67
+ * @example
62
68
  * ```ts
63
69
  * const today = new Date();
64
70
  * if (today.getUTCDay() === WeekDay.Sunday)
@@ -109,6 +115,9 @@ export enum WeekDay
109
115
  * An utility function that calculates the difference between two dates.
110
116
  * The difference can be expressed in different time units.
111
117
  *
118
+ * ---
119
+ *
120
+ * @example
112
121
  * ```ts
113
122
  * const start = new Date("2025-01-01");
114
123
  * const end = new Date("2025-01-31");
@@ -116,13 +125,15 @@ export enum WeekDay
116
125
  * dateDifference(start, end, TimeUnit.Minute); // 43200
117
126
  * ```
118
127
  *
128
+ * ---
129
+ *
119
130
  * @param start The start date.
120
131
  * @param end The end date.
121
132
  * @param unit The time unit to express the difference. `TimeUnit.Day` by default.
122
133
  *
123
134
  * @returns The difference between the two dates in the specified time unit.
124
135
  */
125
- export function dateDifference(start: string | Date, end: string | Date, unit = TimeUnit.Day): number
136
+ export function dateDifference(start: number | string | Date, end: number | string | Date, unit = TimeUnit.Day): number
126
137
  {
127
138
  let _round: (value: number) => number;
128
139
 
@@ -139,6 +150,9 @@ export function dateDifference(start: string | Date, end: string | Date, unit =
139
150
  * An utility function that generates an iterator over a range of dates.
140
151
  * The step between the dates can be expressed in different time units.
141
152
  *
153
+ * ---
154
+ *
155
+ * @example
142
156
  * ```ts
143
157
  * const start = new Date("2025-01-01");
144
158
  * const end = new Date("2025-01-31");
@@ -149,6 +163,8 @@ export function dateDifference(start: string | Date, end: string | Date, unit =
149
163
  * }
150
164
  * ```
151
165
  *
166
+ * ---
167
+ *
152
168
  * @param start The start date (included).
153
169
  * @param end
154
170
  * The end date (excluded).
@@ -159,15 +175,19 @@ export function dateDifference(start: string | Date, end: string | Date, unit =
159
175
  *
160
176
  * @returns A {@link SmartIterator} object that generates the dates in the range.
161
177
  */
162
- export function dateRange(start: string | Date, end: string | Date, step = TimeUnit.Day): SmartIterator<Date>
178
+ export function dateRange(start: number | string | Date, end: number | string | Date, step = TimeUnit.Day)
179
+ : SmartIterator<Date>
163
180
  {
181
+ start = new Date(start);
182
+ end = new Date(end);
183
+
164
184
  if (start >= end) { throw new RangeException("The end date must be greater than the start date."); }
165
185
 
166
186
  return new SmartIterator<Date>(function* ()
167
187
  {
168
- const endTime = new Date(end).getTime();
188
+ const endTime = end.getTime();
169
189
 
170
- let unixTime: number = new Date(start).getTime();
190
+ let unixTime: number = start.getTime();
171
191
  while (unixTime < endTime)
172
192
  {
173
193
  yield new Date(unixTime);
@@ -181,12 +201,17 @@ export function dateRange(start: string | Date, end: string | Date, step = TimeU
181
201
  * An utility function that rounds a date to the nearest time unit.
182
202
  * The rounding can be expressed in different time units.
183
203
  *
204
+ * ---
205
+ *
206
+ * @example
184
207
  * ```ts
185
208
  * const date = new Date("2025-01-01T12:34:56.789Z");
186
209
  *
187
210
  * dateRound(date, TimeUnit.Hour); // 2025-01-01T12:00:00.000Z
188
211
  * ```
189
212
  *
213
+ * ---
214
+ *
190
215
  * @param date The date to round.
191
216
  * @param unit
192
217
  * The time unit to express the rounding. `TimeUnit.Day` by default.
@@ -196,7 +221,7 @@ export function dateRange(start: string | Date, end: string | Date, step = TimeU
196
221
  *
197
222
  * @returns The rounded date.
198
223
  */
199
- export function dateRound(date: string | Date, unit = TimeUnit.Day): Date
224
+ export function dateRound(date: number | string | Date, unit = TimeUnit.Day): Date
200
225
  {
201
226
  if (unit <= TimeUnit.Millisecond)
202
227
  {
@@ -221,18 +246,23 @@ export function dateRound(date: string | Date, unit = TimeUnit.Day): Date
221
246
  * An utility function that gets the week of a date.
222
247
  * The first day of the week can be optionally specified.
223
248
  *
249
+ * ---
250
+ *
251
+ * @example
224
252
  * ```ts
225
253
  * const date = new Date("2025-01-01");
226
254
  *
227
255
  * getWeek(date, WeekDay.Monday); // 2024-12-30
228
256
  * ```
229
257
  *
258
+ * ---
259
+ *
230
260
  * @param date The date to get the week of.
231
261
  * @param firstDay The first day of the week. `WeekDay.Sunday` by default.
232
262
  *
233
263
  * @returns The first day of the week of the specified date.
234
264
  */
235
- export function getWeek(date: string | Date, firstDay = WeekDay.Sunday): Date
265
+ export function getWeek(date: number | string | Date, firstDay = WeekDay.Sunday): Date
236
266
  {
237
267
  date = new Date(date);
238
268
 
package/src/utils/dom.ts CHANGED
@@ -2,10 +2,15 @@
2
2
  * Appends a script element to the document body.
3
3
  * It can be used to load external scripts dynamically.
4
4
  *
5
+ * ---
6
+ *
7
+ * @example
5
8
  * ```ts
6
9
  * await loadScript("https://analytics.service/script.js?id=0123456789");
7
10
  * ```
8
11
  *
12
+ * ---
13
+ *
9
14
  * @param scriptUrl The URL of the script to load.
10
15
  * @param scriptType The type of the script to load. Default is `"text/javascript"`.
11
16
  *
@@ -10,6 +10,9 @@ import { RangeException, SmartIterator } from "../models/index.js";
10
10
  * This means that the original iterator won't be consumed until the
11
11
  * new one is and that consuming one of them will consume the other as well.
12
12
  *
13
+ * ---
14
+ *
15
+ * @example
13
16
  * ```ts
14
17
  * for (const value of chain([1, 2, 3], [4, 5, 6], [7, 8, 9]))
15
18
  * {
@@ -17,6 +20,8 @@ import { RangeException, SmartIterator } from "../models/index.js";
17
20
  * }
18
21
  * ```
19
22
  *
23
+ * ---
24
+ *
20
25
  * @template T The type of elements in the iterables.
21
26
  *
22
27
  * @param iterables The list of iterables to chain.
@@ -41,10 +46,15 @@ export function chain<T>(...iterables: readonly Iterable<T>[]): SmartIterator<T>
41
46
  * - If the iterable isn't an `Array`, it will be consumed entirely in the process.
42
47
  * - If the iterable is an infinite generator, the function will never return.
43
48
  *
49
+ * ---
50
+ *
51
+ * @example
44
52
  * ```ts
45
53
  * count([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); // 10
46
54
  * ```
47
55
  *
56
+ * ---
57
+ *
48
58
  * @template T The type of elements in the iterable.
49
59
  *
50
60
  * @param elements The iterable to count.
@@ -72,6 +82,9 @@ export function count<T>(elements: Iterable<T>): number
72
82
  * This means that the original iterator won't be consumed until the
73
83
  * new one is and that consuming one of them will consume the other as well.
74
84
  *
85
+ * ---
86
+ *
87
+ * @example
75
88
  * ```ts
76
89
  * for (const [index, value] of enumerate(["A", "M", "N", "Z"]))
77
90
  * {
@@ -79,6 +92,8 @@ export function count<T>(elements: Iterable<T>): number
79
92
  * }
80
93
  * ```
81
94
  *
95
+ * ---
96
+ *
82
97
  * @template T The type of elements in the iterable.
83
98
  *
84
99
  * @param elements The iterable to enumerate.
@@ -105,6 +120,9 @@ export function enumerate<T>(elements: Iterable<T>): SmartIterator<[number, T]>
105
120
  *
106
121
  * The default step between the numbers is `1`.
107
122
  *
123
+ * ---
124
+ *
125
+ * @example
108
126
  * ```ts
109
127
  * for (const number of range(5))
110
128
  * {
@@ -112,6 +130,8 @@ export function enumerate<T>(elements: Iterable<T>): SmartIterator<[number, T]>
112
130
  * }
113
131
  * ```
114
132
  *
133
+ * ---
134
+ *
115
135
  * @param end
116
136
  * The end value (excluded).
117
137
  *
@@ -127,6 +147,9 @@ export function range(end: number): SmartIterator<number>;
127
147
  *
128
148
  * The step between the numbers can be specified with a custom value. Default is `1`.
129
149
  *
150
+ * ---
151
+ *
152
+ * @example
130
153
  * ```ts
131
154
  * for (const number of range(2, 7))
132
155
  * {
@@ -134,6 +157,8 @@ export function range(end: number): SmartIterator<number>;
134
157
  * }
135
158
  * ```
136
159
  *
160
+ * ---
161
+ *
137
162
  * @param start
138
163
  * The start value (included).
139
164
  *
@@ -192,10 +217,15 @@ export function range(start: number, end?: number, step = 1): SmartIterator<numb
192
217
  * - If the iterable isn't an `Array`, it will be consumed entirely in the process.
193
218
  * - If the iterable is an infinite generator, the function will never return.
194
219
  *
220
+ * ---
221
+ *
222
+ * @example
195
223
  * ```ts
196
224
  * shuffle([1, 2, 3, 4, 5]); // [3, 1, 5, 2, 4]
197
225
  * ```
198
226
  *
227
+ * ---
228
+ *
199
229
  * @template T The type of elements in the iterable.
200
230
  *
201
231
  * @param iterable The iterable to shuffle.
@@ -219,6 +249,9 @@ export function shuffle<T>(iterable: Iterable<T>): T[]
219
249
  /**
220
250
  * An utility function that filters the elements of an iterable ensuring that they are all unique.
221
251
  *
252
+ * ---
253
+ *
254
+ * @example
222
255
  * ```ts
223
256
  * for (const value of unique([1, 1, 2, 3, 2, 3, 4, 5, 5, 4]))
224
257
  * {
@@ -226,6 +259,8 @@ export function shuffle<T>(iterable: Iterable<T>): T[]
226
259
  * }
227
260
  * ```
228
261
  *
262
+ * ---
263
+ *
229
264
  * @template T The type of elements in the iterable.
230
265
  *
231
266
  * @param elements The iterable to filter.
@@ -254,6 +289,9 @@ export function unique<T>(elements: Iterable<T>): SmartIterator<T>
254
289
  *
255
290
  * The function will stop when one of the two iterables is exhausted.
256
291
  *
292
+ * ---
293
+ *
294
+ * @example
257
295
  * ```ts
258
296
  * for (const [number, char] of zip([1, 2, 3, 4], ["A", "M", "N" "Z"]))
259
297
  * {
@@ -261,6 +299,8 @@ export function unique<T>(elements: Iterable<T>): SmartIterator<T>
261
299
  * }
262
300
  * ```
263
301
  *
302
+ * ---
303
+ *
264
304
  * @template T The type of elements in the first iterable.
265
305
  * @template U The type of elements in the second iterable.
266
306
  *
package/src/utils/math.ts CHANGED
@@ -5,11 +5,16 @@ import { zip } from "./iterator.js";
5
5
  * Computes the average of a given list of values.
6
6
  * The values can be weighted using an additional list of weights.
7
7
  *
8
+ * ---
9
+ *
10
+ * @example
8
11
  * ```ts
9
12
  * average([1, 2, 3, 4, 5]); // 3
10
13
  * average([6, 8.5, 4], [3, 2, 1]); // 6.5
11
14
  * ```
12
15
  *
16
+ * ---
17
+ *
13
18
  * @template T The type of the values in the list. It must be or extend a `number` object.
14
19
  *
15
20
  * @param values
@@ -73,11 +78,16 @@ export function average<T extends number>(values: Iterable<T>, weights?: Iterabl
73
78
  * {@link http://www.cse.yorku.ca/~oz/hash.html#djb2|djb2} algorithm.
74
79
  * However, the hash is garanteed to be a 32-bit signed integer.
75
80
  *
81
+ * ---
82
+ *
83
+ * @example
76
84
  * ```ts
77
85
  * hash("Hello, world!"); // -1880044555
78
86
  * hash("How are you?"); // 1761539132
79
87
  * ```
80
88
  *
89
+ * ---
90
+ *
81
91
  * @param value The string to hash.
82
92
  *
83
93
  * @returns The hash of the specified string.
@@ -99,10 +109,15 @@ export function hash(value: string): number
99
109
  /**
100
110
  * Sums all the values of a given list.
101
111
  *
112
+ * ---
113
+ *
114
+ * @example
102
115
  * ```ts
103
116
  * sum([1, 2, 3, 4, 5]); // 15
104
117
  * ```
105
118
  *
119
+ * ---
120
+ *
106
121
  * @template T The type of the values in the list. It must be or extend a `number` object.
107
122
  *
108
123
  * @param values The list of values to sum.
@@ -12,6 +12,9 @@ export default class Random
12
12
  /**
13
13
  * Generates a random boolean value.
14
14
  *
15
+ * ---
16
+ *
17
+ * @example
15
18
  * ```ts
16
19
  * if (Random.Boolean())
17
20
  * {
@@ -19,6 +22,8 @@ export default class Random
19
22
  * }
20
23
  * ```
21
24
  *
25
+ * ---
26
+ *
22
27
  * @param ratio
23
28
  * The probability of generating `true`.
24
29
  *
@@ -34,10 +39,15 @@ export default class Random
34
39
  /**
35
40
  * Generates a random integer value between `0` (included) and `max` (excluded).
36
41
  *
42
+ * ---
43
+ *
44
+ * @example
37
45
  * ```ts
38
46
  * Random.Integer(5); // 0, 1, 2, 3, 4
39
47
  * ```
40
48
  *
49
+ * ---
50
+ *
41
51
  * @param max The maximum value (excluded).
42
52
  *
43
53
  * @returns A random integer value.
@@ -47,10 +57,15 @@ export default class Random
47
57
  /**
48
58
  * Generates a random integer value between `min` (included) and `max` (excluded).
49
59
  *
60
+ * ---
61
+ *
62
+ * @example
50
63
  * ```ts
51
64
  * Random.Integer(2, 7); // 2, 3, 4, 5, 6
52
65
  * ```
53
66
  *
67
+ * ---
68
+ *
54
69
  * @param min The minimum value (included).
55
70
  * @param max The maximum value (excluded).
56
71
  *
@@ -67,10 +82,15 @@ export default class Random
67
82
  /**
68
83
  * Generates a random decimal value between `0` (included) and `1` (excluded).
69
84
  *
85
+ * ---
86
+ *
87
+ * @example
70
88
  * ```ts
71
89
  * Random.Decimal(); // 0.123456789
72
90
  * ```
73
91
  *
92
+ * ---
93
+ *
74
94
  * @returns A random decimal value.
75
95
  */
76
96
  public static Decimal(): number;
@@ -78,10 +98,15 @@ export default class Random
78
98
  /**
79
99
  * Generates a random decimal value between `0` (included) and `max` (excluded).
80
100
  *
101
+ * ---
102
+ *
103
+ * @example
81
104
  * ```ts
82
105
  * Random.Decimal(5); // 2.3456789
83
106
  * ```
84
107
  *
108
+ * ---
109
+ *
85
110
  * @param max The maximum value (excluded).
86
111
  *
87
112
  * @returns A random decimal value.
@@ -91,10 +116,15 @@ export default class Random
91
116
  /**
92
117
  * Generates a random decimal value between `min` (included) and `max` (excluded).
93
118
  *
119
+ * ---
120
+ *
121
+ * @example
94
122
  * ```ts
95
123
  * Random.Decimal(2, 7); // 4.56789
96
124
  * ```
97
125
  *
126
+ * ---
127
+ *
98
128
  * @param min The minimum value (included).
99
129
  * @param max The maximum value (excluded).
100
130
  *
@@ -112,6 +142,8 @@ export default class Random
112
142
  /**
113
143
  * Picks a random valid index from a given array of elements.
114
144
  *
145
+ * ---
146
+ *
115
147
  * @template T The type of the elements in the array.
116
148
  *
117
149
  * @param elements
@@ -131,6 +163,8 @@ export default class Random
131
163
  /**
132
164
  * Picks a random element from a given array of elements.
133
165
  *
166
+ * ---
167
+ *
134
168
  * @template T The type of the elements in the array.
135
169
  *
136
170
  * @param elements
@@ -1,10 +1,15 @@
1
1
  /**
2
2
  * Capitalize the first letter of a string.
3
3
  *
4
+ * ---
5
+ *
6
+ * @example
4
7
  * ```ts
5
8
  * capitalize('hello'); // 'Hello'
6
9
  * ```
7
10
  *
11
+ * ---
12
+ *
8
13
  * @param value The string to capitalize.
9
14
  *
10
15
  * @returns The capitalized string.