@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
|
@@ -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`.
|
|
@@ -51,10 +56,15 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
51
56
|
/**
|
|
52
57
|
* Initializes a new instance of the {@link SmartAsyncIterator} class.
|
|
53
58
|
*
|
|
59
|
+
* ---
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
54
62
|
* ```ts
|
|
55
63
|
* const iterator = new SmartAsyncIterator<string>(["A", "B", "C"]);
|
|
56
64
|
* ```
|
|
57
65
|
*
|
|
66
|
+
* ---
|
|
67
|
+
*
|
|
58
68
|
* @param iterable The iterable object to wrap.
|
|
59
69
|
*/
|
|
60
70
|
public constructor(iterable: Iterable<T>);
|
|
@@ -62,10 +72,15 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
62
72
|
/**
|
|
63
73
|
* Initializes a new instance of the {@link SmartAsyncIterator} class.
|
|
64
74
|
*
|
|
75
|
+
* ---
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
65
78
|
* ```ts
|
|
66
79
|
* const iterator = new SmartAsyncIterator<number>([1, 2, 3, 4, 5]);
|
|
67
80
|
* ```
|
|
68
81
|
*
|
|
82
|
+
* ---
|
|
83
|
+
*
|
|
69
84
|
* @param iterable The asynchronous iterable object to wrap.
|
|
70
85
|
*/
|
|
71
86
|
public constructor(iterable: AsyncIterable<T>);
|
|
@@ -73,6 +88,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
73
88
|
/**
|
|
74
89
|
* Initializes a new instance of the {@link SmartAsyncIterator} class.
|
|
75
90
|
*
|
|
91
|
+
* ---
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
76
94
|
* ```ts
|
|
77
95
|
* const iterator = new SmartAsyncIterator<number, void, number>({
|
|
78
96
|
* _sum: 0, _count: 0,
|
|
@@ -87,6 +105,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
87
105
|
* })
|
|
88
106
|
* ```
|
|
89
107
|
*
|
|
108
|
+
* ---
|
|
109
|
+
*
|
|
90
110
|
* @param iterator The iterator object to wrap.
|
|
91
111
|
*/
|
|
92
112
|
public constructor(iterator: Iterator<T, R, N>);
|
|
@@ -94,6 +114,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
94
114
|
/**
|
|
95
115
|
* Initializes a new instance of the {@link SmartAsyncIterator} class.
|
|
96
116
|
*
|
|
117
|
+
* ---
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
97
120
|
* ```ts
|
|
98
121
|
* const iterator = new SmartAsyncIterator<number, void, number>({
|
|
99
122
|
* _sum: 0, _count: 0,
|
|
@@ -108,6 +131,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
108
131
|
* })
|
|
109
132
|
* ```
|
|
110
133
|
*
|
|
134
|
+
* ---
|
|
135
|
+
*
|
|
111
136
|
* @param iterator The asynchronous iterator object to wrap.
|
|
112
137
|
*/
|
|
113
138
|
public constructor(iterator: AsyncIterator<T, R, N>);
|
|
@@ -115,6 +140,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
115
140
|
/**
|
|
116
141
|
* Initializes a new instance of the {@link SmartAsyncIterator} class.
|
|
117
142
|
*
|
|
143
|
+
* ---
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
118
146
|
* ```ts
|
|
119
147
|
* const iterator = new SmartAsyncIterator<number>(function* ()
|
|
120
148
|
* {
|
|
@@ -122,6 +150,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
122
150
|
* });
|
|
123
151
|
* ```
|
|
124
152
|
*
|
|
153
|
+
* ---
|
|
154
|
+
*
|
|
125
155
|
* @param generatorFn The generator function to wrap.
|
|
126
156
|
*/
|
|
127
157
|
public constructor(generatorFn: GeneratorFunction<T, R, N>);
|
|
@@ -129,6 +159,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
129
159
|
/**
|
|
130
160
|
* Initializes a new instance of the {@link SmartAsyncIterator} class.
|
|
131
161
|
*
|
|
162
|
+
* ---
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
132
165
|
* ```ts
|
|
133
166
|
* const iterator = new SmartAsyncIterator<number>(async function* ()
|
|
134
167
|
* {
|
|
@@ -136,6 +169,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
136
169
|
* });
|
|
137
170
|
* ```
|
|
138
171
|
*
|
|
172
|
+
* ---
|
|
173
|
+
*
|
|
139
174
|
* @param generatorFn The asynchronous generator function to wrap.
|
|
140
175
|
*/
|
|
141
176
|
public constructor(generatorFn: AsyncGeneratorFunction<T, R, N>);
|
|
@@ -143,10 +178,15 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
143
178
|
/**
|
|
144
179
|
* Initializes a new instance of the {@link SmartAsyncIterator} class.
|
|
145
180
|
*
|
|
181
|
+
* ---
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
146
184
|
* ```ts
|
|
147
185
|
* const iterator = new SmartAsyncIterator(values);
|
|
148
186
|
* ```
|
|
149
187
|
*
|
|
188
|
+
* ---
|
|
189
|
+
*
|
|
150
190
|
* @param argument The synchronous or asynchronous iterable, iterator or generator function to wrap.
|
|
151
191
|
*/
|
|
152
192
|
public constructor(argument: MaybeAsyncIteratorLike<T, R, N> | MaybeAsyncGeneratorFunction<T, R, N>);
|
|
@@ -226,6 +266,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
226
266
|
*
|
|
227
267
|
* If the iterator is infinite and every element satisfies the condition, the method will never return.
|
|
228
268
|
*
|
|
269
|
+
* ---
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
229
272
|
* ```ts
|
|
230
273
|
* const iterator = new SmartAsyncIterator<number>([-2, -1, 0, 1, 2]);
|
|
231
274
|
* const result = await iterator.every(async (value) => value < 0);
|
|
@@ -233,6 +276,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
233
276
|
* console.log(result); // false
|
|
234
277
|
* ```
|
|
235
278
|
*
|
|
279
|
+
* ---
|
|
280
|
+
*
|
|
236
281
|
* @param predicate The condition to check for each element of the iterator.
|
|
237
282
|
*
|
|
238
283
|
* @returns
|
|
@@ -266,6 +311,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
266
311
|
*
|
|
267
312
|
* If the iterator is infinite and no element satisfies the condition, the method will never return.
|
|
268
313
|
*
|
|
314
|
+
* ---
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
269
317
|
* ```ts
|
|
270
318
|
* const iterator = new SmartAsyncIterator<number>([-2, -1, 0, 1, 2]);
|
|
271
319
|
* const result = await iterator.some(async (value) => value > 0);
|
|
@@ -273,6 +321,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
273
321
|
* console.log(result); // true
|
|
274
322
|
* ```
|
|
275
323
|
*
|
|
324
|
+
* ---
|
|
325
|
+
*
|
|
276
326
|
* @param predicate The condition to check for each element of the iterator.
|
|
277
327
|
*
|
|
278
328
|
* @returns
|
|
@@ -306,6 +356,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
306
356
|
* This means that the original iterator won't be consumed until the
|
|
307
357
|
* new one is and that consuming one of them will consume the other as well.
|
|
308
358
|
*
|
|
359
|
+
* ---
|
|
360
|
+
*
|
|
361
|
+
* @example
|
|
309
362
|
* ```ts
|
|
310
363
|
* const iterator = new SmartAsyncIterator<number>([-2, -1, 0, 1, 2]);
|
|
311
364
|
* const result = iterator.filter(async (value) => value < 0);
|
|
@@ -313,6 +366,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
313
366
|
* console.log(await result.toArray()); // [-2, -1]
|
|
314
367
|
* ```
|
|
315
368
|
*
|
|
369
|
+
* ---
|
|
370
|
+
*
|
|
316
371
|
* @param predicate The condition to check for each element of the iterator.
|
|
317
372
|
*
|
|
318
373
|
* @returns A new {@link SmartAsyncIterator} containing only the elements that satisfy the condition.
|
|
@@ -332,6 +387,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
332
387
|
* This means that the original iterator won't be consumed until the
|
|
333
388
|
* new one is and that consuming one of them will consume the other as well.
|
|
334
389
|
*
|
|
390
|
+
* ---
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
335
393
|
* ```ts
|
|
336
394
|
* const iterator = new SmartAsyncIterator<number | string>([-2, "-1", "0", 1, "2"]);
|
|
337
395
|
* const result = iterator.filter<number>(async (value) => typeof value === "number");
|
|
@@ -339,6 +397,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
339
397
|
* console.log(await result.toArray()); // [-2, 1]
|
|
340
398
|
* ```
|
|
341
399
|
*
|
|
400
|
+
* ---
|
|
401
|
+
*
|
|
342
402
|
* @template S
|
|
343
403
|
* The type of the elements that satisfy the condition.
|
|
344
404
|
* This allows the type-system to infer the correct type of the new iterator.
|
|
@@ -381,6 +441,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
381
441
|
* This means that the original iterator won't be consumed until the
|
|
382
442
|
* new one is and that consuming one of them will consume the other as well.
|
|
383
443
|
*
|
|
444
|
+
* ---
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
384
447
|
* ```ts
|
|
385
448
|
* const iterator = new SmartAsyncIterator<number>([-2, -1, 0, 1, 2]);
|
|
386
449
|
* const result = iterator.map(async (value) => Math.abs(value));
|
|
@@ -388,6 +451,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
388
451
|
* console.log(await result.toArray()); // [2, 1, 0, 1, 2]
|
|
389
452
|
* ```
|
|
390
453
|
*
|
|
454
|
+
* ---
|
|
455
|
+
*
|
|
391
456
|
* @template V The type of the elements after the transformation.
|
|
392
457
|
*
|
|
393
458
|
* @param iteratee The transformation function to apply to each element of the iterator.
|
|
@@ -427,6 +492,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
427
492
|
* - If an empty iterator is provided, a {@link ValueException} will be thrown.
|
|
428
493
|
* - If the iterator is infinite, the method will never return.
|
|
429
494
|
*
|
|
495
|
+
* ---
|
|
496
|
+
*
|
|
497
|
+
* @example
|
|
430
498
|
* ```ts
|
|
431
499
|
* const iterator = new SmartAsyncIterator<number>([1, 2, 3, 4, 5]);
|
|
432
500
|
* const result = await iterator.reduce(async (acc, value) => acc + value);
|
|
@@ -434,6 +502,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
434
502
|
* console.log(result); // 15
|
|
435
503
|
* ```
|
|
436
504
|
*
|
|
505
|
+
* ---
|
|
506
|
+
*
|
|
437
507
|
* @param reducer The reducer function to apply to each element of the iterator.
|
|
438
508
|
*
|
|
439
509
|
* @returns A {@link Promise} that will resolve to the final result of the reduction.
|
|
@@ -452,6 +522,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
452
522
|
*
|
|
453
523
|
* If the iterator is infinite, the method will never return.
|
|
454
524
|
*
|
|
525
|
+
* ---
|
|
526
|
+
*
|
|
527
|
+
* @example
|
|
455
528
|
* ```ts
|
|
456
529
|
* const iterator = new SmartAsyncIterator<number>([1, 2, 3, 4, 5]);
|
|
457
530
|
* const result = await iterator.reduce(async (acc, value) => acc + value, 10);
|
|
@@ -459,6 +532,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
459
532
|
* console.log(result); // 25
|
|
460
533
|
* ```
|
|
461
534
|
*
|
|
535
|
+
* ---
|
|
536
|
+
*
|
|
462
537
|
* @template A The type of the accumulator value which will also be the type of the final result of the reduction.
|
|
463
538
|
*
|
|
464
539
|
* @param reducer The reducer function to apply to each element of the iterator.
|
|
@@ -504,6 +579,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
504
579
|
* This means that the original iterator won't be consumed until the
|
|
505
580
|
* new one is and that consuming one of them will consume the other as well.
|
|
506
581
|
*
|
|
582
|
+
* ---
|
|
583
|
+
*
|
|
584
|
+
* @example
|
|
507
585
|
* ```ts
|
|
508
586
|
* const iterator = new SmartAsyncIterator<number[]>([[-2, -1], 0, 1, 2, [3, 4, 5]]);
|
|
509
587
|
* const result = iterator.flatMap(async (value) => value);
|
|
@@ -511,6 +589,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
511
589
|
* console.log(await result.toArray()); // [-2, -1, 0, 1, 2, 3, 4, 5]
|
|
512
590
|
* ```
|
|
513
591
|
*
|
|
592
|
+
* ---
|
|
593
|
+
*
|
|
514
594
|
* @template V The type of the elements after the transformation.
|
|
515
595
|
*
|
|
516
596
|
* @param iteratee The transformation function to apply to each element of the iterator.
|
|
@@ -556,6 +636,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
556
636
|
* Only the dropped elements will be consumed in the process.
|
|
557
637
|
* The rest of the iterator will be consumed only once the new one is.
|
|
558
638
|
*
|
|
639
|
+
* ---
|
|
640
|
+
*
|
|
641
|
+
* @example
|
|
559
642
|
* ```ts
|
|
560
643
|
* const iterator = new SmartAsyncIterator<number>([-2, -1, 0, 1, 2]);
|
|
561
644
|
* const result = iterator.drop(3);
|
|
@@ -563,6 +646,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
563
646
|
* console.log(await result.toArray()); // [1, 2]
|
|
564
647
|
* ```
|
|
565
648
|
*
|
|
649
|
+
* ---
|
|
650
|
+
*
|
|
566
651
|
* @param count The number of elements to drop.
|
|
567
652
|
*
|
|
568
653
|
* @returns A new {@link SmartAsyncIterator} containing the remaining elements.
|
|
@@ -607,6 +692,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
607
692
|
* Only the taken elements will be consumed from the original iterator.
|
|
608
693
|
* The rest of the original iterator will be available for further consumption.
|
|
609
694
|
*
|
|
695
|
+
* ---
|
|
696
|
+
*
|
|
697
|
+
* @example
|
|
610
698
|
* ```ts
|
|
611
699
|
* const iterator = new SmartAsyncIterator<number>([-2, -1, 0, 1, 2]);
|
|
612
700
|
* const result = iterator.take(3);
|
|
@@ -615,6 +703,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
615
703
|
* console.log(await iterator.toArray()); // [1, 2]
|
|
616
704
|
* ```
|
|
617
705
|
*
|
|
706
|
+
* ---
|
|
707
|
+
*
|
|
618
708
|
* @param limit The number of elements to take.
|
|
619
709
|
*
|
|
620
710
|
* @returns A new {@link SmartAsyncIterator} containing the taken elements.
|
|
@@ -654,6 +744,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
654
744
|
* - If no element satisfies the condition, `undefined` will be returned once the entire iterator is consumed.
|
|
655
745
|
* - If the iterator is infinite and no element satisfies the condition, the method will never return.
|
|
656
746
|
*
|
|
747
|
+
* ---
|
|
748
|
+
*
|
|
749
|
+
* @example
|
|
657
750
|
* ```ts
|
|
658
751
|
* const iterator = new SmartAsyncIterator<number>([-2, -1, 0, 1, 2]);
|
|
659
752
|
* const result = await iterator.find(async (value) => value > 0);
|
|
@@ -661,6 +754,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
661
754
|
* console.log(result); // 1
|
|
662
755
|
* ```
|
|
663
756
|
*
|
|
757
|
+
* ---
|
|
758
|
+
*
|
|
664
759
|
* @param predicate The condition to check for each element of the iterator.
|
|
665
760
|
*
|
|
666
761
|
* @returns
|
|
@@ -682,6 +777,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
682
777
|
* - If no element satisfies the condition, `undefined` will be returned once the entire iterator is consumed.
|
|
683
778
|
* - If the iterator is infinite and no element satisfies the condition, the method will never return.
|
|
684
779
|
*
|
|
780
|
+
* ---
|
|
781
|
+
*
|
|
782
|
+
* @example
|
|
685
783
|
* ```ts
|
|
686
784
|
* const iterator = new SmartAsyncIterator<number | string>([-2, "-1", "0", 1, "2"]);
|
|
687
785
|
* const result = await iterator.find<number>(async (value) => typeof value === "number");
|
|
@@ -689,6 +787,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
689
787
|
* console.log(result); // -2
|
|
690
788
|
* ```
|
|
691
789
|
*
|
|
790
|
+
* ---
|
|
791
|
+
*
|
|
692
792
|
* @template S
|
|
693
793
|
* The type of the element that satisfies the condition.
|
|
694
794
|
* This allows the type-system to infer the correct type of the result.
|
|
@@ -727,6 +827,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
727
827
|
* This means that the original iterator won't be consumed until the
|
|
728
828
|
* new one is and that consuming one of them will consume the other as well.
|
|
729
829
|
*
|
|
830
|
+
* ---
|
|
831
|
+
*
|
|
832
|
+
* @example
|
|
730
833
|
* ```ts
|
|
731
834
|
* const iterator = new SmartAsyncIterator<string>(["A", "M", "N", "Z"]);
|
|
732
835
|
* const result = iterator.enumerate();
|
|
@@ -737,6 +840,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
737
840
|
* }
|
|
738
841
|
* ```
|
|
739
842
|
*
|
|
843
|
+
* ---
|
|
844
|
+
*
|
|
740
845
|
* @returns A new {@link SmartAsyncIterator} containing the enumerated elements.
|
|
741
846
|
*/
|
|
742
847
|
public enumerate(): SmartAsyncIterator<[number, T], R>
|
|
@@ -755,6 +860,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
755
860
|
* This means that the original iterator won't be consumed until the
|
|
756
861
|
* new one is and that consuming one of them will consume the other as well.
|
|
757
862
|
*
|
|
863
|
+
* ---
|
|
864
|
+
*
|
|
865
|
+
* @example
|
|
758
866
|
* ```ts
|
|
759
867
|
* const iterator = new SmartAsyncIterator<number>([1, 1, 2, 3, 2, 3, 4, 5, 5, 4]);
|
|
760
868
|
* const result = iterator.unique();
|
|
@@ -762,6 +870,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
762
870
|
* console.log(await result.toArray()); // [1, 2, 3, 4, 5]
|
|
763
871
|
* ```
|
|
764
872
|
*
|
|
873
|
+
* ---
|
|
874
|
+
*
|
|
765
875
|
* @returns A new {@link SmartAsyncIterator} containing only the unique elements.
|
|
766
876
|
*/
|
|
767
877
|
public unique(): SmartAsyncIterator<T, R>
|
|
@@ -790,6 +900,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
790
900
|
*
|
|
791
901
|
* If the iterator is infinite, the method will never return.
|
|
792
902
|
*
|
|
903
|
+
* ---
|
|
904
|
+
*
|
|
905
|
+
* @example
|
|
793
906
|
* ```ts
|
|
794
907
|
* const iterator = new SmartAsyncIterator<number>([1, 2, 3, 4, 5]);
|
|
795
908
|
* const result = await iterator.count();
|
|
@@ -797,6 +910,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
797
910
|
* console.log(result); // 5
|
|
798
911
|
* ```
|
|
799
912
|
*
|
|
913
|
+
* ---
|
|
914
|
+
*
|
|
800
915
|
* @returns A {@link Promise} that will resolve to the number of elements in the iterator.
|
|
801
916
|
*/
|
|
802
917
|
public async count(): Promise<number>
|
|
@@ -819,6 +934,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
819
934
|
* This method will consume the entire iterator in the process.
|
|
820
935
|
* If the iterator is infinite, the method will never return.
|
|
821
936
|
*
|
|
937
|
+
* ---
|
|
938
|
+
*
|
|
939
|
+
* @example
|
|
822
940
|
* ```ts
|
|
823
941
|
* const iterator = new SmartAsyncIterator<number>(["A", "M", "N", "Z"]);
|
|
824
942
|
* await iterator.forEach(async (value, index) =>
|
|
@@ -827,6 +945,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
827
945
|
* }
|
|
828
946
|
* ```
|
|
829
947
|
*
|
|
948
|
+
* ---
|
|
949
|
+
*
|
|
830
950
|
* @param iteratee The function to apply to each element of the iterator.
|
|
831
951
|
*
|
|
832
952
|
* @returns A {@link Promise} that will resolve once the iteration is complete.
|
|
@@ -852,6 +972,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
852
972
|
*
|
|
853
973
|
* Once the iterator is done, the method will return an object with the `done` property set to `true`.
|
|
854
974
|
*
|
|
975
|
+
* ---
|
|
976
|
+
*
|
|
977
|
+
* @example
|
|
855
978
|
* ```ts
|
|
856
979
|
* const iterator = new SmartAsyncIterator<number>([1, 2, 3, 4, 5]);
|
|
857
980
|
*
|
|
@@ -866,6 +989,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
866
989
|
* console.log(result); // { done: true, value: undefined }
|
|
867
990
|
* ```
|
|
868
991
|
*
|
|
992
|
+
* ---
|
|
993
|
+
*
|
|
869
994
|
* @param values The value to pass to the next element, if required.
|
|
870
995
|
*
|
|
871
996
|
* @returns
|
|
@@ -881,6 +1006,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
881
1006
|
* free the resources and perform any cleanup operation.
|
|
882
1007
|
* It may also be used to signal the end or to compute a specific final result of the iteration process.
|
|
883
1008
|
*
|
|
1009
|
+
* ---
|
|
1010
|
+
*
|
|
1011
|
+
* @example
|
|
884
1012
|
* ```ts
|
|
885
1013
|
* const iterator = new SmartAsyncIterator<number>({
|
|
886
1014
|
* _index: 0,
|
|
@@ -899,6 +1027,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
899
1027
|
* }
|
|
900
1028
|
* ```
|
|
901
1029
|
*
|
|
1030
|
+
* ---
|
|
1031
|
+
*
|
|
902
1032
|
* @param value The final value of the iterator.
|
|
903
1033
|
*
|
|
904
1034
|
* @returns A {@link Promise} that will resolve to the final result of the iterator.
|
|
@@ -917,6 +1047,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
917
1047
|
* free the resources and perform any cleanup operation.
|
|
918
1048
|
* It may also be used to signal that an error occurred during the iteration process or to handle it.
|
|
919
1049
|
*
|
|
1050
|
+
* ---
|
|
1051
|
+
*
|
|
1052
|
+
* @example
|
|
920
1053
|
* ```ts
|
|
921
1054
|
* const iterator = new SmartAsyncIterator<number>({
|
|
922
1055
|
* _index: 0,
|
|
@@ -944,6 +1077,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
944
1077
|
* }
|
|
945
1078
|
* ```
|
|
946
1079
|
*
|
|
1080
|
+
* ---
|
|
1081
|
+
*
|
|
947
1082
|
* @param error The error to throw into the iterator.
|
|
948
1083
|
*
|
|
949
1084
|
* @returns A {@link Promise} that will resolve to the final result of the iterator.
|
|
@@ -967,6 +1102,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
967
1102
|
* This means that the original iterator won't be consumed until the
|
|
968
1103
|
* the new one is and that consuming one of them will consume the other as well.
|
|
969
1104
|
*
|
|
1105
|
+
* ---
|
|
1106
|
+
*
|
|
1107
|
+
* @example
|
|
970
1108
|
* ```ts
|
|
971
1109
|
* const iterator = new SmartAsyncIterator<number>([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
|
972
1110
|
* const result = iterator.groupBy<string>(async (value) => value % 2 === 0 ? "even" : "odd");
|
|
@@ -974,6 +1112,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
974
1112
|
* console.log(await result.toObject()); // { odd: [1, 3, 5, 7, 9], even: [2, 4, 6, 8, 10] }
|
|
975
1113
|
* ```
|
|
976
1114
|
*
|
|
1115
|
+
* ---
|
|
1116
|
+
*
|
|
977
1117
|
* @template K The type of the keys used to group the elements.
|
|
978
1118
|
*
|
|
979
1119
|
* @param iteratee The key function to apply to each element of the iterator.
|
|
@@ -996,6 +1136,9 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
996
1136
|
*
|
|
997
1137
|
* If the iterator is infinite, the method will never return.
|
|
998
1138
|
*
|
|
1139
|
+
* ---
|
|
1140
|
+
*
|
|
1141
|
+
* @example
|
|
999
1142
|
* ```ts
|
|
1000
1143
|
* const iterator = new SmartAsyncIterator(async function* ()
|
|
1001
1144
|
* {
|
|
@@ -1006,6 +1149,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
1006
1149
|
* console.log(result); // [0, 1, 2, 3, 4]
|
|
1007
1150
|
* ```
|
|
1008
1151
|
*
|
|
1152
|
+
* ---
|
|
1153
|
+
*
|
|
1009
1154
|
* @returns A {@link Promise} that will resolve to an array containing all elements of the iterator.
|
|
1010
1155
|
*/
|
|
1011
1156
|
public toArray(): Promise<T[]>
|