@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.
- package/README.md +1 -0
- package/dist/core.js +900 -187
- package/dist/core.js.map +1 -1
- package/dist/core.umd.cjs +2 -2
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +13 -10
- package/src/core/types.ts +43 -10
- package/src/index.ts +3 -2
- package/src/models/aggregators/aggregated-async-iterator.ts +161 -1
- package/src/models/aggregators/aggregated-iterator.ts +146 -1
- package/src/models/aggregators/reduced-iterator.ts +148 -8
- package/src/models/aggregators/types.ts +35 -0
- package/src/models/callbacks/callable-object.ts +7 -0
- package/src/models/callbacks/publisher.ts +33 -8
- package/src/models/callbacks/switchable-callback.ts +102 -21
- package/src/models/callbacks/types.ts +32 -0
- package/src/models/exceptions/core.ts +29 -0
- package/src/models/exceptions/index.ts +105 -1
- package/src/models/iterators/smart-async-iterator.ts +145 -0
- package/src/models/iterators/smart-iterator.ts +130 -0
- package/src/models/iterators/types.ts +79 -1
- package/src/models/json/json-storage.ts +123 -0
- package/src/models/json/types.ts +1 -1
- package/src/models/promises/deferred-promise.ts +15 -0
- package/src/models/promises/smart-promise.ts +45 -0
- package/src/models/promises/timed-promise.ts +10 -0
- package/src/models/promises/types.ts +30 -0
- package/src/models/timers/clock.ts +21 -0
- package/src/models/timers/countdown.ts +30 -0
- package/src/models/timers/game-loop.ts +26 -0
- package/src/models/types.ts +1 -1
- package/src/utils/async.ts +15 -0
- package/src/utils/curve.ts +11 -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 +34 -0
- package/src/utils/string.ts +5 -0
|
@@ -24,6 +24,9 @@ import type { KeyedIteratee, KeyedReducer, KeyedTypeGuardPredicate } from "./typ
|
|
|
24
24
|
* This is particularly useful when you have group elements and
|
|
25
25
|
* need perform specific operations on the reduced elements.
|
|
26
26
|
*
|
|
27
|
+
* ---
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
27
30
|
* ```ts
|
|
28
31
|
* const results = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
29
32
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -32,6 +35,8 @@ import type { KeyedIteratee, KeyedReducer, KeyedTypeGuardPredicate } from "./typ
|
|
|
32
35
|
* console.log(results.toObject()); // { odd: 4, even: 4 }
|
|
33
36
|
* ```
|
|
34
37
|
*
|
|
38
|
+
* ---
|
|
39
|
+
*
|
|
35
40
|
* @template K The type of the key used to group the elements.
|
|
36
41
|
* @template T The type of the elements in the iterator.
|
|
37
42
|
*/
|
|
@@ -45,10 +50,15 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
45
50
|
/**
|
|
46
51
|
* Initializes a new instance of the {@link ReducedIterator} class.
|
|
47
52
|
*
|
|
53
|
+
* ---
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
48
56
|
* ```ts
|
|
49
57
|
* const results = new ReducedIterator<string, number>([["A", 1], ["B", 2], ["C", 4]]);
|
|
50
58
|
* ```
|
|
51
59
|
*
|
|
60
|
+
* ---
|
|
61
|
+
*
|
|
52
62
|
* @param iterable A reduced iterable object.
|
|
53
63
|
*/
|
|
54
64
|
public constructor(iterable: Iterable<[K, T]>);
|
|
@@ -56,6 +66,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
56
66
|
/**
|
|
57
67
|
* Initializes a new instance of the {@link ReducedIterator} class.
|
|
58
68
|
*
|
|
69
|
+
* ---
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
59
72
|
* ```ts
|
|
60
73
|
* const results = new ReducedIterator<string, number>({
|
|
61
74
|
* _index: 0,
|
|
@@ -69,6 +82,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
69
82
|
* });
|
|
70
83
|
* ```
|
|
71
84
|
*
|
|
85
|
+
* ---
|
|
86
|
+
*
|
|
72
87
|
* @param iterator An reduced iterator object.
|
|
73
88
|
*/
|
|
74
89
|
public constructor(iterator: Iterator<[K, T]>);
|
|
@@ -76,6 +91,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
76
91
|
/**
|
|
77
92
|
* Initializes a new instance of the {@link ReducedIterator} class.
|
|
78
93
|
*
|
|
94
|
+
* ---
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
79
97
|
* ```ts
|
|
80
98
|
* import { range, Random } from "@byloth/core";
|
|
81
99
|
*
|
|
@@ -88,6 +106,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
88
106
|
* });
|
|
89
107
|
* ```
|
|
90
108
|
*
|
|
109
|
+
* ---
|
|
110
|
+
*
|
|
91
111
|
* @param generatorFn A generator function that produces the reduced elements.
|
|
92
112
|
*/
|
|
93
113
|
public constructor(generatorFn: GeneratorFunction<[K, T]>);
|
|
@@ -95,10 +115,15 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
95
115
|
/**
|
|
96
116
|
* Initializes a new instance of the {@link ReducedIterator} class.
|
|
97
117
|
*
|
|
118
|
+
* ---
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
98
121
|
* ```ts
|
|
99
122
|
* const results = new ReducedIterator(reducedValues);
|
|
100
123
|
* ```
|
|
101
124
|
*
|
|
125
|
+
* ---
|
|
126
|
+
*
|
|
102
127
|
* @param argument An iterable, iterator or generator function that produces the reduced elements.
|
|
103
128
|
*/
|
|
104
129
|
public constructor(argument: Iterable<[K, T]> | Iterator<[K, T]> | GeneratorFunction<[K, T]>);
|
|
@@ -120,6 +145,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
120
145
|
*
|
|
121
146
|
* If the iterator is infinite and every element satisfies the condition, the method will never return.
|
|
122
147
|
*
|
|
148
|
+
* ---
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
123
151
|
* ```ts
|
|
124
152
|
* const results = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
125
153
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -129,6 +157,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
129
157
|
* console.log(results); // true
|
|
130
158
|
* ```
|
|
131
159
|
*
|
|
160
|
+
* ---
|
|
161
|
+
*
|
|
132
162
|
* @param predicate The condition to check for each element of the iterator.
|
|
133
163
|
*
|
|
134
164
|
* @returns `true` if all elements satisfy the condition, `false` otherwise.
|
|
@@ -156,6 +186,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
156
186
|
*
|
|
157
187
|
* If the iterator is infinite and no element satisfies the condition, the method will never return.
|
|
158
188
|
*
|
|
189
|
+
* ---
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
159
192
|
* ```ts
|
|
160
193
|
* const results = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
161
194
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -165,6 +198,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
165
198
|
* console.log(results); // true
|
|
166
199
|
* ```
|
|
167
200
|
*
|
|
201
|
+
* ---
|
|
202
|
+
*
|
|
168
203
|
* @param predicate The condition to check for each element of the iterator.
|
|
169
204
|
*
|
|
170
205
|
* @returns `true` if any element satisfies the condition, `false` otherwise.
|
|
@@ -192,6 +227,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
192
227
|
* This means that the original iterator won't be consumed until the
|
|
193
228
|
* new one is and that consuming one of them will consume the other as well.
|
|
194
229
|
*
|
|
230
|
+
* ---
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
195
233
|
* ```ts
|
|
196
234
|
* const results = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
197
235
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -201,6 +239,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
201
239
|
* console.log(results.toObject()); // { odd: 4, even: 16 }
|
|
202
240
|
* ```
|
|
203
241
|
*
|
|
242
|
+
* ---
|
|
243
|
+
*
|
|
204
244
|
* @param predicate The condition to check for each element of the iterator.
|
|
205
245
|
*
|
|
206
246
|
* @returns A new {@link ReducedIterator} containing only the elements that satisfy the condition.
|
|
@@ -220,6 +260,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
220
260
|
* This means that the original iterator won't be consumed until the
|
|
221
261
|
* new one is and that consuming one of them will consume the other as well.
|
|
222
262
|
*
|
|
263
|
+
* ---
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
223
266
|
* ```ts
|
|
224
267
|
* const results = new SmartIterator<number | string>([-3, -1, "0", "2", 3, 5, "6", "8"])
|
|
225
268
|
* .groupBy((value) => Number(value) % 2 === 0 ? "even" : "odd")
|
|
@@ -229,6 +272,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
229
272
|
* console.log(results.toObject()); // { odd: 4 }
|
|
230
273
|
* ```
|
|
231
274
|
*
|
|
275
|
+
* ---
|
|
276
|
+
*
|
|
232
277
|
* @template S
|
|
233
278
|
* The type of the elements that satisfy the condition.
|
|
234
279
|
* This allows the type-system to infer the correct type of the iterator.
|
|
@@ -266,6 +311,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
266
311
|
* This means that the original iterator won't be consumed until the
|
|
267
312
|
* new one is and that consuming one of them will consume the other as well.
|
|
268
313
|
*
|
|
314
|
+
* ---
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
269
317
|
* ```ts
|
|
270
318
|
* const results = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
271
319
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -275,6 +323,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
275
323
|
* console.log(results.toObject()); // { odd: 8, even: 32 }
|
|
276
324
|
* ```
|
|
277
325
|
*
|
|
326
|
+
* ---
|
|
327
|
+
*
|
|
278
328
|
* @template V The type of the elements after the transformation.
|
|
279
329
|
*
|
|
280
330
|
* @param iteratee The transformation function to apply to each element of the iterator.
|
|
@@ -308,6 +358,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
308
358
|
* - If an empty iterator is provided, a {@link ValueException} will be thrown.
|
|
309
359
|
* - If the iterator is infinite, the method will never return.
|
|
310
360
|
*
|
|
361
|
+
* ---
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
311
364
|
* ```ts
|
|
312
365
|
* const result = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
313
366
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -317,6 +370,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
317
370
|
* console.log(result); // 20
|
|
318
371
|
* ```
|
|
319
372
|
*
|
|
373
|
+
* ---
|
|
374
|
+
*
|
|
320
375
|
* @param reducer The reducer function to apply to the elements of the iterator.
|
|
321
376
|
*
|
|
322
377
|
* @returns The final value after reducing all the elements of the iterator.
|
|
@@ -335,6 +390,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
335
390
|
*
|
|
336
391
|
* If the iterator is infinite, the method will never return.
|
|
337
392
|
*
|
|
393
|
+
* ---
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
338
396
|
* ```ts
|
|
339
397
|
* const result = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
340
398
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -344,6 +402,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
344
402
|
* console.log(result); // { value: 20 }
|
|
345
403
|
* ```
|
|
346
404
|
*
|
|
405
|
+
* ---
|
|
406
|
+
*
|
|
347
407
|
* @template A The type of the accumulator value which will also be the type of the final result of the reduction.
|
|
348
408
|
*
|
|
349
409
|
* @param reducer The reducer function to apply to the elements of the iterator.
|
|
@@ -388,6 +448,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
388
448
|
* This means that the original iterator won't be consumed until the
|
|
389
449
|
* new one is and that consuming one of them will consume the other as well.
|
|
390
450
|
*
|
|
451
|
+
* ---
|
|
452
|
+
*
|
|
453
|
+
* @example
|
|
391
454
|
* ```ts
|
|
392
455
|
* const results = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
393
456
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -397,6 +460,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
397
460
|
* console.log(results.toObject()); // { odd: [-3, -1, 3, 5], even: [0, 2, 6, 8] }
|
|
398
461
|
* ```
|
|
399
462
|
*
|
|
463
|
+
* ---
|
|
464
|
+
*
|
|
400
465
|
* @template V The type of the elements after the transformation.
|
|
401
466
|
*
|
|
402
467
|
* @param iteratee The transformation function to apply to each element of the iterator.
|
|
@@ -437,6 +502,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
437
502
|
* Only the dropped elements will be consumed in the process.
|
|
438
503
|
* The rest of the iterator will be consumed once the new iterator is.
|
|
439
504
|
*
|
|
505
|
+
* ---
|
|
506
|
+
*
|
|
507
|
+
* @example
|
|
440
508
|
* ```ts
|
|
441
509
|
* const results = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
442
510
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -445,7 +513,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
445
513
|
*
|
|
446
514
|
* console.log(results.toObject()); // { even: [0, 2, 6, 8] }
|
|
447
515
|
* ```
|
|
448
|
-
*
|
|
516
|
+
*
|
|
517
|
+
* ---
|
|
518
|
+
*
|
|
449
519
|
* @param count The number of elements to drop.
|
|
450
520
|
*
|
|
451
521
|
* @returns A new {@link ReducedIterator} containing the remaining elements.
|
|
@@ -478,6 +548,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
478
548
|
* Only the taken elements will be consumed from the original reduced iterator.
|
|
479
549
|
* The rest of the original reduced iterator will be available for further consumption.
|
|
480
550
|
*
|
|
551
|
+
* ---
|
|
552
|
+
*
|
|
553
|
+
* @example
|
|
481
554
|
* ```ts
|
|
482
555
|
* const reduced = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
483
556
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -489,11 +562,13 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
489
562
|
* console.log(reduced.toObject()); // { even: [0, 2, 6, 8] }
|
|
490
563
|
* ```
|
|
491
564
|
*
|
|
492
|
-
*
|
|
565
|
+
* ---
|
|
566
|
+
*
|
|
567
|
+
* @param limit The number of elements to take.
|
|
493
568
|
*
|
|
494
569
|
* @returns A new {@link ReducedIterator} containing the taken elements.
|
|
495
570
|
*/
|
|
496
|
-
public take(
|
|
571
|
+
public take(limit: number): ReducedIterator<K, T>
|
|
497
572
|
{
|
|
498
573
|
const elements = this._elements.enumerate();
|
|
499
574
|
|
|
@@ -501,7 +576,7 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
501
576
|
{
|
|
502
577
|
for (const [index, [key, element]] of elements)
|
|
503
578
|
{
|
|
504
|
-
if (index >=
|
|
579
|
+
if (index >= limit) { break; }
|
|
505
580
|
yield [key, element];
|
|
506
581
|
}
|
|
507
582
|
});
|
|
@@ -521,14 +596,20 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
521
596
|
* - If no element satisfies the condition, `undefined` will be returned once the entire iterator is consumed.
|
|
522
597
|
* - If the iterator is infinite and no element satisfies the condition, the method will never return.
|
|
523
598
|
*
|
|
599
|
+
* ---
|
|
600
|
+
*
|
|
601
|
+
* @example
|
|
524
602
|
* ```ts
|
|
525
603
|
* const results = new SmartIterator<number>([-3, -3, -1, 0, 1, 2, 5, 6, 8])
|
|
526
604
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
527
605
|
* .reduce((key, accumulator, value) => accumulator + value)
|
|
528
606
|
* .find((key, value) => value > 0);
|
|
529
|
-
*
|
|
607
|
+
*
|
|
530
608
|
* console.log(results); // 16
|
|
531
|
-
*
|
|
609
|
+
* ```
|
|
610
|
+
*
|
|
611
|
+
* ---
|
|
612
|
+
*
|
|
532
613
|
* @param predicate The condition to check for each element of the iterator.
|
|
533
614
|
*
|
|
534
615
|
* @returns The first element that satisfies the condition, `undefined` otherwise.
|
|
@@ -549,13 +630,19 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
549
630
|
* - If no element satisfies the condition, `undefined` will be returned once the entire iterator is consumed.
|
|
550
631
|
* - If the iterator is infinite and no element satisfies the condition, the method will never return.
|
|
551
632
|
*
|
|
633
|
+
* ---
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
552
636
|
* ```ts
|
|
553
637
|
* const results = new SmartIterator<number | string>(["-3", -3, "-1", 0, 1, 2, "5", 6, 8])
|
|
554
638
|
* .groupBy((value) => Number(value) % 2 === 0 ? "even" : "odd")
|
|
555
639
|
* .reduce((key, accumulator, value) => accumulator + value)
|
|
556
640
|
* .find<number>((key, value) => typeof value === "number");
|
|
557
|
-
*
|
|
641
|
+
*
|
|
558
642
|
* console.log(results); // 16
|
|
643
|
+
* ```
|
|
644
|
+
*
|
|
645
|
+
* ---
|
|
559
646
|
*
|
|
560
647
|
* @template S
|
|
561
648
|
* The type of the elements that satisfy the condition.
|
|
@@ -589,6 +676,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
589
676
|
* This means that the original iterator won't be consumed until the
|
|
590
677
|
* new one is and that consuming one of them will consume the other as well.
|
|
591
678
|
*
|
|
679
|
+
* ---
|
|
680
|
+
*
|
|
681
|
+
* @example
|
|
592
682
|
* ```ts
|
|
593
683
|
* const results = new ReducedIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
594
684
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -598,6 +688,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
598
688
|
* console.log(results.toObject()); // [[0, 4], [1, 16]]
|
|
599
689
|
* ```
|
|
600
690
|
*
|
|
691
|
+
* ---
|
|
692
|
+
*
|
|
601
693
|
* @returns A new {@link ReducedIterator} object containing the enumerated elements.
|
|
602
694
|
*/
|
|
603
695
|
public enumerate(): ReducedIterator<K, [number, T]>
|
|
@@ -616,6 +708,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
616
708
|
* This means that the original iterator won't be consumed until the
|
|
617
709
|
* new one is and that consuming one of them will consume the other as well.
|
|
618
710
|
*
|
|
711
|
+
* ---
|
|
712
|
+
*
|
|
713
|
+
* @example
|
|
619
714
|
* ```ts
|
|
620
715
|
* const results = new ReducedIterator<number>([-3, -1, 0, 2, 3, 6, -3, -1, 1, 5, 6, 8, 7, 2])
|
|
621
716
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -650,6 +745,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
650
745
|
*
|
|
651
746
|
* If the iterator is infinite, the method will never return.
|
|
652
747
|
*
|
|
748
|
+
* ---
|
|
749
|
+
*
|
|
750
|
+
* @example
|
|
653
751
|
* ```ts
|
|
654
752
|
* const results = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
655
753
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -659,6 +757,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
659
757
|
* console.log(results); // 2
|
|
660
758
|
* ```
|
|
661
759
|
*
|
|
760
|
+
* ---
|
|
761
|
+
*
|
|
662
762
|
* @returns The number of elements in the iterator.
|
|
663
763
|
*/
|
|
664
764
|
public count(): number
|
|
@@ -677,17 +777,22 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
677
777
|
* This method will consume the entire iterator in the process.
|
|
678
778
|
* If the iterator is infinite, the method will never return.
|
|
679
779
|
*
|
|
780
|
+
* ---
|
|
781
|
+
*
|
|
782
|
+
* @example
|
|
680
783
|
* ```ts
|
|
681
784
|
* const reduced = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
682
785
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
683
786
|
* .reduce((key, accumulator, value) => accumulator + value);
|
|
684
|
-
*
|
|
787
|
+
*
|
|
685
788
|
* reduced.forEach((key, value, index) =>
|
|
686
789
|
* {
|
|
687
790
|
* console.log(`#${index} - ${key}: ${value}`); // "#0 - odd: 4", "#1 - even: 16"
|
|
688
791
|
* });
|
|
689
792
|
* ```
|
|
690
793
|
*
|
|
794
|
+
* ---
|
|
795
|
+
*
|
|
691
796
|
* @param iteratee The function to apply to each element of the reduced iterator.
|
|
692
797
|
*/
|
|
693
798
|
public forEach(iteratee: KeyedIteratee<K, T>): void
|
|
@@ -709,6 +814,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
709
814
|
* This means that the original iterator won't be consumed until the
|
|
710
815
|
* new one is and that consuming one of them will consume the other as well.
|
|
711
816
|
*
|
|
817
|
+
* ---
|
|
818
|
+
*
|
|
819
|
+
* @example
|
|
712
820
|
* ```ts
|
|
713
821
|
* const results = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, -6, -8])
|
|
714
822
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -718,6 +826,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
718
826
|
* console.log(results.toObject()); // { positive: 4, negative: -12 }
|
|
719
827
|
* ```
|
|
720
828
|
*
|
|
829
|
+
* ---
|
|
830
|
+
*
|
|
721
831
|
* @template J The type of the new keys used to group the elements.
|
|
722
832
|
*
|
|
723
833
|
* @param iteratee The function to determine the new key of each element of the iterator.
|
|
@@ -748,6 +858,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
748
858
|
* This means that the original iterator won't be consumed until the
|
|
749
859
|
* new one is and that consuming one of them will consume the other as well.
|
|
750
860
|
*
|
|
861
|
+
* ---
|
|
862
|
+
*
|
|
863
|
+
* @example
|
|
751
864
|
* ```ts
|
|
752
865
|
* const keys = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
753
866
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -757,6 +870,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
757
870
|
* console.log(keys.toArray()); // ["odd", "even"]
|
|
758
871
|
* ```
|
|
759
872
|
*
|
|
873
|
+
* ---
|
|
874
|
+
*
|
|
760
875
|
* @returns A new {@link SmartIterator} containing all the keys of the iterator.
|
|
761
876
|
*/
|
|
762
877
|
public keys(): SmartIterator<K>
|
|
@@ -784,6 +899,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
784
899
|
* This means that the original iterator won't be consumed until the
|
|
785
900
|
* new one is and that consuming one of them will consume the other as well.
|
|
786
901
|
*
|
|
902
|
+
* ---
|
|
903
|
+
*
|
|
904
|
+
* @example
|
|
787
905
|
* ```ts
|
|
788
906
|
* const entries = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
789
907
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -793,6 +911,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
793
911
|
* console.log(entries.toArray()); // [["odd", 4], ["even", 16]]
|
|
794
912
|
* ```
|
|
795
913
|
*
|
|
914
|
+
* ---
|
|
915
|
+
*
|
|
796
916
|
* @returns A new {@link SmartIterator} containing all the entries of the iterator.
|
|
797
917
|
*/
|
|
798
918
|
public entries(): SmartIterator<[K, T]>
|
|
@@ -811,6 +931,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
811
931
|
* This means that the original iterator won't be consumed until the
|
|
812
932
|
* new one is and that consuming one of them will consume the other as well.
|
|
813
933
|
*
|
|
934
|
+
* ---
|
|
935
|
+
*
|
|
936
|
+
* @example
|
|
814
937
|
* ```ts
|
|
815
938
|
* const values = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
816
939
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -820,6 +943,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
820
943
|
* console.log(values.toArray()); // [4, 16]
|
|
821
944
|
* ```
|
|
822
945
|
*
|
|
946
|
+
* ---
|
|
947
|
+
*
|
|
823
948
|
* @returns A new {@link SmartIterator} containing all the values of the iterator.
|
|
824
949
|
*/
|
|
825
950
|
public values(): SmartIterator<T>
|
|
@@ -841,6 +966,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
841
966
|
*
|
|
842
967
|
* If the iterator is infinite, the method will never return.
|
|
843
968
|
*
|
|
969
|
+
* ---
|
|
970
|
+
*
|
|
971
|
+
* @example
|
|
844
972
|
* ```ts
|
|
845
973
|
* const reduced = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
846
974
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -849,6 +977,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
849
977
|
* console.log(reduced.toArray()); // [4, 16]
|
|
850
978
|
* ```
|
|
851
979
|
*
|
|
980
|
+
* ---
|
|
981
|
+
*
|
|
852
982
|
* @returns The {@link Array} containing all elements of the iterator.
|
|
853
983
|
*/
|
|
854
984
|
public toArray(): T[]
|
|
@@ -862,6 +992,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
862
992
|
*
|
|
863
993
|
* If the iterator is infinite, the method will never return.
|
|
864
994
|
*
|
|
995
|
+
* ---
|
|
996
|
+
*
|
|
997
|
+
* @example
|
|
865
998
|
* ```ts
|
|
866
999
|
* const reduced = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
867
1000
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -870,6 +1003,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
870
1003
|
* console.log(reduced.toMap()); // Map(2) { "odd" => 4, "even" => 16 }
|
|
871
1004
|
* ```
|
|
872
1005
|
*
|
|
1006
|
+
* ---
|
|
1007
|
+
*
|
|
873
1008
|
* @returns The {@link Map} containing all elements of the iterator.
|
|
874
1009
|
*/
|
|
875
1010
|
public toMap(): Map<K, T>
|
|
@@ -883,6 +1018,9 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
883
1018
|
*
|
|
884
1019
|
* If the iterator is infinite, the method will never return.
|
|
885
1020
|
*
|
|
1021
|
+
* ---
|
|
1022
|
+
*
|
|
1023
|
+
* @example
|
|
886
1024
|
* ```ts
|
|
887
1025
|
* const reduced = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
888
1026
|
* .groupBy((value) => value % 2 === 0 ? "even" : "odd")
|
|
@@ -891,6 +1029,8 @@ export default class ReducedIterator<K extends PropertyKey, T>
|
|
|
891
1029
|
* console.log(reduced.toObject()); // { odd: 4, even: 16 }
|
|
892
1030
|
* ```
|
|
893
1031
|
*
|
|
1032
|
+
* ---
|
|
1033
|
+
*
|
|
894
1034
|
* @returns The {@link Object} containing all elements of the iterator.
|
|
895
1035
|
*/
|
|
896
1036
|
public toObject(): Record<K, T>
|
|
@@ -5,6 +5,9 @@ import type { MaybePromise } from "../promises/types.js";
|
|
|
5
5
|
* with the addition of a `key` parameter, compared to the JavaScript's standard ones.
|
|
6
6
|
* It can be used to transform the elements of an aggregated iterable.
|
|
7
7
|
*
|
|
8
|
+
* ---
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
8
11
|
* ```ts
|
|
9
12
|
* const iteratee: KeyedIteratee<string, number, string> = (key: string, value: number) => `${value}`;
|
|
10
13
|
* const results = new SmartIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
@@ -14,6 +17,8 @@ import type { MaybePromise } from "../promises/types.js";
|
|
|
14
17
|
* console.log(results.toObject()); // { odd: ["-3", "-1", "3", "5"], even: ["0", "2", "6", "8"] }
|
|
15
18
|
* ```
|
|
16
19
|
*
|
|
20
|
+
* ---
|
|
21
|
+
*
|
|
17
22
|
* @template K The type of the key used to aggregate elements in the iterable.
|
|
18
23
|
* @template T The type of the elements in the iterable.
|
|
19
24
|
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
@@ -25,6 +30,9 @@ export type KeyedIteratee<K extends PropertyKey, T, R = void> = (key: K, value:
|
|
|
25
30
|
* function with the addition of a `key` parameter.
|
|
26
31
|
* It can be used to transform the elements of an aggregated iterable asynchronously.
|
|
27
32
|
*
|
|
33
|
+
* ---
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
28
36
|
* ```ts
|
|
29
37
|
* const iteratee: AsyncKeyedIteratee<string, number, string> = async (key: string, value: number) => `${value}`;
|
|
30
38
|
* const results = new SmartAsyncIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
@@ -34,6 +42,8 @@ export type KeyedIteratee<K extends PropertyKey, T, R = void> = (key: K, value:
|
|
|
34
42
|
* console.log(await results.toObject()); // { odd: ["-3", "-1", "3", "5"], even: ["0", "2", "6", "8"] }
|
|
35
43
|
* ```
|
|
36
44
|
*
|
|
45
|
+
* ---
|
|
46
|
+
*
|
|
37
47
|
* @template K The type of the key used to aggregate elements in the iterable.
|
|
38
48
|
* @template T The type of the elements in the iterable.
|
|
39
49
|
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
@@ -45,6 +55,9 @@ export type AsyncKeyedIteratee<K extends PropertyKey, T, R = void> = (key: K, va
|
|
|
45
55
|
* with the addition of a `key` parameter that can be either synchronous or asynchronous.
|
|
46
56
|
* It can be used to transform the elements of an aggregated iterable.
|
|
47
57
|
*
|
|
58
|
+
* ---
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
48
61
|
* ```ts
|
|
49
62
|
* const iteratee: AsyncKeyedIteratee<string, number, string> = [async] (key: string, value: number) => `${value}`;
|
|
50
63
|
* const results = new SmartAsyncIterator<number>([-3, -1, 0, 2, 3, 5, 6, 8])
|
|
@@ -54,6 +67,8 @@ export type AsyncKeyedIteratee<K extends PropertyKey, T, R = void> = (key: K, va
|
|
|
54
67
|
* console.log(await results.toObject()); // { odd: ["-3", "-1", "3", "5"], even: ["0", "2", "6", "8"] }
|
|
55
68
|
* ```
|
|
56
69
|
*
|
|
70
|
+
* ---
|
|
71
|
+
*
|
|
57
72
|
* @template K The type of the key used to aggregate elements in the iterable.
|
|
58
73
|
* @template T The type of the elements in the iterable.
|
|
59
74
|
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
@@ -69,6 +84,9 @@ export type MaybeAsyncKeyedIteratee<K extends PropertyKey, T, R = void> =
|
|
|
69
84
|
* It can be used to filter the elements of an aggregated iterable
|
|
70
85
|
* while allowing the type-system to infer them correctly.
|
|
71
86
|
*
|
|
87
|
+
* ---
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
72
90
|
* ```ts
|
|
73
91
|
* const predicate: KeyedTypeGuardPredicate<string, number | string, string> =
|
|
74
92
|
* (key: string, value: number | string): value is string => typeof value === "string";
|
|
@@ -80,6 +98,8 @@ export type MaybeAsyncKeyedIteratee<K extends PropertyKey, T, R = void> =
|
|
|
80
98
|
* console.log(results.toObject()); // { odd: ["0", "5", "8"], even: [] }
|
|
81
99
|
* ```
|
|
82
100
|
*
|
|
101
|
+
* ---
|
|
102
|
+
*
|
|
83
103
|
* @template K The type of the key used to aggregate elements in the iterable.
|
|
84
104
|
* @template T The type of the elements in the iterable.
|
|
85
105
|
* @template R
|
|
@@ -100,6 +120,9 @@ export type KeyedTypeGuardPredicate<K extends PropertyKey, T, R extends T> =
|
|
|
100
120
|
* An utility type that represents a reducer-like function.
|
|
101
121
|
* It can be used to reduce the elements of an aggregated iterable into a single value.
|
|
102
122
|
*
|
|
123
|
+
* ---
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
103
126
|
* ```ts
|
|
104
127
|
* const sum: KeyedReducer<string, number, number> =
|
|
105
128
|
* (key: string, accumulator: number, value: number) => accumulator + value;
|
|
@@ -111,6 +134,8 @@ export type KeyedTypeGuardPredicate<K extends PropertyKey, T, R extends T> =
|
|
|
111
134
|
* console.log(results.toObject()); // { odd: 4, even: 16 }
|
|
112
135
|
* ```
|
|
113
136
|
*
|
|
137
|
+
* ---
|
|
138
|
+
*
|
|
114
139
|
* @template K The type of the key used to aggregate elements in the iterable.
|
|
115
140
|
* @template T The type of the elements in the iterable.
|
|
116
141
|
* @template A The type of the accumulator.
|
|
@@ -121,6 +146,9 @@ export type KeyedReducer<K extends PropertyKey, T, A> = (key: K, accumulator: A,
|
|
|
121
146
|
* An utility type that represents an asynchronous reducer-like function.
|
|
122
147
|
* It can be used to reduce the elements of an aggregated iterable into a single value.
|
|
123
148
|
*
|
|
149
|
+
* ---
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
124
152
|
* ```ts
|
|
125
153
|
* const sum: AsyncKeyedReducer<string, number, number> =
|
|
126
154
|
* async (key: string, accumulator: number, value: number) => accumulator + value;
|
|
@@ -132,6 +160,8 @@ export type KeyedReducer<K extends PropertyKey, T, A> = (key: K, accumulator: A,
|
|
|
132
160
|
* console.log(await results.toObject()); // { odd: 4, even: 16 }
|
|
133
161
|
* ```
|
|
134
162
|
*
|
|
163
|
+
* ---
|
|
164
|
+
*
|
|
135
165
|
* @template K The type of the key used to aggregate elements in the iterable.
|
|
136
166
|
* @template T The type of the elements in the iterable.
|
|
137
167
|
* @template A The type of the accumulator.
|
|
@@ -143,6 +173,9 @@ export type AsyncKeyedReducer<K extends PropertyKey, T, A> =
|
|
|
143
173
|
* An utility type that represents a reducer-like function that can be either synchronous or asynchronous.
|
|
144
174
|
* It can be used to reduce the elements of an aggregated iterable into a single value.
|
|
145
175
|
*
|
|
176
|
+
* ---
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
146
179
|
* ```ts
|
|
147
180
|
* const sum: MaybeAsyncKeyedReducer<string, number, number> =
|
|
148
181
|
* [async] (key: string, accumulator: number, value: number) => accumulator + value;
|
|
@@ -154,6 +187,8 @@ export type AsyncKeyedReducer<K extends PropertyKey, T, A> =
|
|
|
154
187
|
* console.log(await results.toObject()); // { odd: 4, even: 16 }
|
|
155
188
|
* ```
|
|
156
189
|
*
|
|
190
|
+
* ---
|
|
191
|
+
*
|
|
157
192
|
* @template K The type of the key used to aggregate elements in the iterable.
|
|
158
193
|
* @template T The type of the elements in the iterable.
|
|
159
194
|
* @template A The type of the accumulator.
|
|
@@ -8,6 +8,9 @@ const SmartFunction = (Function as unknown) as new<A extends unknown[] = [], R =
|
|
|
8
8
|
/**
|
|
9
9
|
* An abstract class that can be used to implement callable objects.
|
|
10
10
|
*
|
|
11
|
+
* ---
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
11
14
|
* ```ts
|
|
12
15
|
* class ActivableCallback extends CallableObject<(evt: PointerEvent) => void>
|
|
13
16
|
* {
|
|
@@ -25,6 +28,8 @@ const SmartFunction = (Function as unknown) as new<A extends unknown[] = [], R =
|
|
|
25
28
|
* window.addEventListener("pointerup", () => { callback.enabled = false; });
|
|
26
29
|
* ```
|
|
27
30
|
*
|
|
31
|
+
* ---
|
|
32
|
+
*
|
|
28
33
|
* @template T
|
|
29
34
|
* The type signature of the callback function.
|
|
30
35
|
* It must be a function. Default is `(...args: any[]) => any`.
|
|
@@ -49,6 +54,8 @@ export default abstract class CallableObject<T extends Callback<any[], any> = ()
|
|
|
49
54
|
* The method that will be called when the object is invoked.
|
|
50
55
|
* It must be implemented by the derived classes.
|
|
51
56
|
*
|
|
57
|
+
* ---
|
|
58
|
+
*
|
|
52
59
|
* @param args The arguments that have been passed to the object.
|
|
53
60
|
*
|
|
54
61
|
* @returns The return value of the method.
|