@byloth/core 2.0.1 → 2.1.0
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.cjs +6 -0
- package/dist/core.cjs.map +1 -0
- package/dist/{core.js → core.esm.js} +1098 -779
- package/dist/core.esm.js.map +1 -0
- package/dist/core.global.js +6 -0
- package/dist/core.global.js.map +1 -0
- package/dist/core.umd.cjs +3 -3
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +15 -11
- package/src/core/types.ts +43 -10
- package/src/index.ts +9 -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 +16 -12
- package/src/models/callbacks/switchable-callback.ts +9 -1
- package/src/models/callbacks/types.ts +35 -0
- package/src/models/collections/index.ts +4 -0
- package/src/models/collections/map-view.ts +206 -0
- package/src/models/collections/set-view.ts +204 -0
- package/src/models/collections/types.ts +25 -0
- package/src/models/exceptions/core.ts +10 -1
- package/src/models/exceptions/index.ts +40 -1
- package/src/models/index.ts +1 -0
- 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 +4 -1
- 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 +3 -8
- 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 +43 -3
- package/src/utils/math.ts +15 -0
- package/src/utils/random.ts +4 -0
- package/src/utils/string.ts +5 -0
- package/dist/core.js.map +0 -1
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)
|
|
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 =
|
|
188
|
+
const endTime = end.getTime();
|
|
169
189
|
|
|
170
|
-
let unixTime: number =
|
|
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
|
*
|
package/src/utils/iterator.ts
CHANGED
|
@@ -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.
|
|
@@ -38,13 +43,18 @@ export function chain<T>(...iterables: readonly Iterable<T>[]): SmartIterator<T>
|
|
|
38
43
|
* An utility function that counts the number of elements in an iterable.
|
|
39
44
|
*
|
|
40
45
|
* Also note that:
|
|
41
|
-
* - If the iterable isn't an
|
|
46
|
+
* - If the iterable isn't an {@link 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
|
*
|
|
@@ -188,14 +213,19 @@ export function range(start: number, end?: number, step = 1): SmartIterator<numb
|
|
|
188
213
|
* algorithm to shuffle the elements.
|
|
189
214
|
*
|
|
190
215
|
* Also note that:
|
|
191
|
-
* - If the iterable is an
|
|
192
|
-
* - If the iterable isn't an
|
|
216
|
+
* - If the iterable is an {@link Array}, it won't be modified since the shuffling isn't done in-place.
|
|
217
|
+
* - If the iterable isn't an {@link 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.
|
package/src/utils/random.ts
CHANGED
|
@@ -142,6 +142,8 @@ export default class Random
|
|
|
142
142
|
/**
|
|
143
143
|
* Picks a random valid index from a given array of elements.
|
|
144
144
|
*
|
|
145
|
+
* ---
|
|
146
|
+
*
|
|
145
147
|
* @template T The type of the elements in the array.
|
|
146
148
|
*
|
|
147
149
|
* @param elements
|
|
@@ -161,6 +163,8 @@ export default class Random
|
|
|
161
163
|
/**
|
|
162
164
|
* Picks a random element from a given array of elements.
|
|
163
165
|
*
|
|
166
|
+
* ---
|
|
167
|
+
*
|
|
164
168
|
* @template T The type of the elements in the array.
|
|
165
169
|
*
|
|
166
170
|
* @param elements
|
package/src/utils/string.ts
CHANGED