-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
1363 lines (1236 loc) · 43.7 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type {Comparator} from "@softwareventures/ordered";
import {compare as defaultCompare, equal as defaultEqual, reverse} from "@softwareventures/ordered";
import type {AsyncIterableLike} from "@softwareventures/async-iterable";
import {asyncIterable} from "@softwareventures/async-iterable";
import {hasProperty} from "unknown";
import {isNotNull} from "@softwareventures/nullable";
export type AsyncIteratorLike<T> =
| AsyncIterator<T>
| Iterator<T | Promise<T>>
| Promise<AsyncIterator<T>>
| Promise<Iterator<T | Promise<T>>>
| AsyncIterableLike<T>;
export function asyncIterator<T>(iterator: AsyncIteratorLike<T>): AsyncIterator<T> {
return hasProperty(iterator, Symbol.asyncIterator)
? iterator[Symbol.asyncIterator]()
: hasProperty(iterator, Symbol.iterator)
? asyncIterable(iterator)[Symbol.asyncIterator]()
: hasProperty(iterator, "then")
? fromPromiseOfIteratorLike(iterator)
: fromSimpleIterator(iterator);
}
type SimpleAsyncIteratorLike<T> = AsyncIterator<T> | Iterator<T | Promise<T>>;
function fromSimpleIterator<T>(iterator: SimpleAsyncIteratorLike<T>): AsyncIterator<T> {
return {
next: async (): Promise<IteratorResult<T>> => {
const n = await iterator.next();
if (n.done === true) {
return {done: true, value: undefined};
} else {
return {value: await n.value};
}
}
};
}
type SimpleAsyncIteratorOrIterable<T> =
| SimpleAsyncIteratorLike<T>
| AsyncIterable<T>
| Iterable<T | Promise<T>>;
function fromSimpleIteratorOrIterable<T>(
iterator: SimpleAsyncIteratorOrIterable<T>
): AsyncIterator<T> {
return hasProperty(iterator, Symbol.asyncIterator)
? iterator[Symbol.asyncIterator]()
: hasProperty(iterator, Symbol.iterator)
? asyncIterable(iterator)[Symbol.asyncIterator]()
: fromSimpleIterator(iterator);
}
type PromiseOfAsyncIteratorLike<T> =
| Promise<AsyncIterator<T>>
| Promise<Iterator<T | Promise<T>>>
| Promise<AsyncIterable<T>>
| Promise<Iterable<T | Promise<T>>>;
function fromPromiseOfIteratorLike<T>(iterator: PromiseOfAsyncIteratorLike<T>): AsyncIterator<T> {
let next = async (): Promise<IteratorResult<T>> => {
const it = iterator.then(it => fromSimpleIteratorOrIterable(it));
const internal = async (): Promise<IteratorResult<T>> => (await it).next();
next = internal;
return internal();
};
return {next: async () => next()};
}
export async function asyncToArrayOnce<T>(iterator: AsyncIteratorLike<T>): Promise<T[]> {
const it = asyncIterator(iterator);
const array: T[] = [];
let element = await it.next();
while (element.done !== true) {
array.push(element.value);
element = await it.next();
}
return array;
}
export async function asyncToSetOnce<T>(iterator: AsyncIteratorLike<T>): Promise<Set<T>> {
const it = asyncIterator(iterator);
const set = new Set<T>();
let element = await it.next();
while (element.done !== true) {
set.add(element.value);
element = await it.next();
}
return set;
}
export async function asyncFirstOnce<T>(iterator: AsyncIteratorLike<T>): Promise<T | null> {
const it = asyncIterator(iterator);
const element = await it.next();
if (element.done === true) {
return null;
} else {
return element.value;
}
}
export function asyncTailOnce<T>(iterator: AsyncIteratorLike<T>): AsyncIterator<T> {
const it = asyncIterator(iterator);
const tail = it.next().then(() => it);
return fromPromiseOfIteratorLike(tail);
}
export function asyncPushOnce<T>(
iterator: AsyncIteratorLike<T>,
value: T | Promise<T>
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let next = async (): Promise<IteratorResult<T>> => {
const element = await it.next();
if (element.done === true) {
next = async () => Promise.resolve({done: true, value: undefined});
return {value: await value};
} else {
return element;
}
};
return {next: async () => next()};
}
export function asyncPushOnceFn<T>(
value: T | Promise<T>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncPushOnce(iterator, value);
}
export function asyncUnshiftOnce<T>(
iterator: AsyncIteratorLike<T>,
value: T | Promise<T>
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let next = async (): Promise<IteratorResult<T>> => {
next = async () => it.next();
return {value: await value};
};
return {next: async () => next()};
}
export function asyncUnshiftOnceFn<T>(
value: T | Promise<T>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncUnshiftOnce(iterator, value);
}
export function asyncInitialOnce<T>(iterator: AsyncIteratorLike<T>): AsyncIterator<T> {
const it = asyncIterator(iterator);
let prev = it.next();
return {
next: async () => {
const [element, result] = await prev.then(async () => {
const result = prev;
prev = it.next();
return [await prev, await result] as const;
});
return element.done === true ? element : result;
}
};
}
export async function asyncLastOnce<T>(iterator: AsyncIteratorLike<T>): Promise<T | null> {
const it = asyncIterator(iterator);
let last = await it.next();
if (last.done === true) {
return null;
}
let element = await it.next();
while (element.done !== true) {
last = element;
element = await it.next();
}
return last.value;
}
export async function asyncOnlyOnce<T>(iterator: AsyncIteratorLike<T>): Promise<T | null> {
const it = asyncIterator(iterator);
const first = await it.next();
if (first.done === true) {
return null;
}
const second = await it.next();
if (second.done === true) {
return first.value;
} else {
return null;
}
}
export async function asyncEmptyOnce<T>(iterator: AsyncIteratorLike<T>): Promise<boolean> {
return (await asyncIterator(iterator).next()).done === true;
}
export async function asyncNotEmptyOnce<T>(iterator: AsyncIteratorLike<T>): Promise<boolean> {
return (await asyncIterator(iterator).next()).done !== true;
}
export function asyncSliceOnce<T>(
iterator: AsyncIteratorLike<T>,
start: number | Promise<number> = 0,
end: number | Promise<number> = Infinity
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let i = 0;
const done: IteratorResult<T> = {done: true, value: undefined};
const first = async (): Promise<IteratorResult<T>> => {
if ((await end) <= (await start)) {
next = after;
return done;
} else {
next = before;
return before();
}
};
const before = async (): Promise<IteratorResult<T>> => {
let element = await it.next();
const s = await start;
while (i++ < s && element.done !== true) {
element = await it.next();
}
if (element.done === true) {
next = after;
return done;
} else {
next = during;
return element;
}
};
const during = async (): Promise<IteratorResult<T>> => {
const element = await it.next();
if (i++ < (await end) && element.done !== true) {
return element;
} else {
next = after;
return done;
}
};
const after = async (): Promise<IteratorResult<T>> => done;
let next = first;
return {next: async () => next()};
}
export function asyncSliceOnceFn<T>(
start: number | Promise<number>,
end: number | Promise<number> = Infinity
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncSliceOnce(iterator, start, end);
}
export function asyncTakeOnce<T>(
iterator: AsyncIteratorLike<T>,
count: number | Promise<number>
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let i = 0;
const done: IteratorResult<T> = {done: true, value: undefined};
const during = async (): Promise<IteratorResult<T>> => {
const element = await it.next();
const c = await count;
if (i++ < c && element.done !== true) {
return element;
} else {
next = after;
return done;
}
};
const after = async (): Promise<IteratorResult<T>> => done;
let next = during;
return {next: async () => next()};
}
export function asyncTakeOnceFn<T>(
count: number | Promise<number>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncTakeOnce(iterator, count);
}
export function asyncDropOnce<T>(
iterator: AsyncIteratorLike<T>,
count: number | Promise<number>
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let i = 0;
const before = async (): Promise<IteratorResult<T>> => {
let element = await it.next();
const c = await count;
while (i++ < c && element.done !== true) {
element = await it.next();
}
next = during;
return element;
};
const during = async (): Promise<IteratorResult<T>> => it.next();
let next = before;
return {next: async () => next()};
}
export function asyncDropOnceFn<T>(
count: number | Promise<number>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncDropOnce(iterator, count);
}
export function asyncTakeWhileOnce<T, U extends T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => element is U
): AsyncIterator<T>;
export function asyncTakeWhileOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): AsyncIterator<T>;
export function asyncTakeWhileOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let i = 0;
const done: IteratorResult<T> = {done: true, value: undefined};
const during = async (): Promise<IteratorResult<T>> => {
const element = await it.next();
if (element.done !== true && (await predicate(element.value, i++))) {
return element;
} else {
next = after;
return done;
}
};
const after = async (): Promise<IteratorResult<T>> => done;
let next = during;
return {next: async () => next()};
}
export function asyncTakeWhileOnceFn<T, U extends T>(
predicate: (element: T, index: number) => element is U
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<U>;
export function asyncTakeWhileOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T>;
export function asyncTakeWhileOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncTakeWhileOnce(iterator, predicate);
}
export function asyncTakeUntilOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let i = 0;
const done: IteratorResult<T> = {done: true, value: undefined};
const during = async (): Promise<IteratorResult<T>> => {
const element = await it.next();
if (element.done !== true && !(await predicate(element.value, i++))) {
return element;
} else {
next = after;
return done;
}
};
const after = async (): Promise<IteratorResult<T>> => done;
let next = during;
return {next: async () => next()};
}
export function asyncTakeUntilOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncTakeUntilOnce(iterator, predicate);
}
export function asyncDropWhileOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let i = 0;
const before = async (): Promise<IteratorResult<T>> => {
let element = await it.next();
while (element.done !== true && (await predicate(element.value, i++))) {
element = await it.next();
}
next = during;
return element;
};
const during = async (): Promise<IteratorResult<T>> => it.next();
let next = before;
return {next: async () => next()};
}
export function asyncDropWhileOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncDropWhileOnce(iterator, predicate);
}
export function asyncDropUntilOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let i = 0;
const before = async (): Promise<IteratorResult<T>> => {
let element = await it.next();
while (element.done !== true && !(await predicate(element.value, i++))) {
element = await it.next();
}
next = during;
return element;
};
const during = async (): Promise<IteratorResult<T>> => it.next();
let next = before;
return {next: async () => next()};
}
export function asyncDropUntilOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncDropUntilOnce(iterator, predicate);
}
export async function asyncEqualOnce<T>(
a: AsyncIteratorLike<T>,
b: AsyncIteratorLike<T>,
elementsEqual: (a: T, b: T) => boolean | Promise<boolean> = defaultEqual
): Promise<boolean> {
const ait = asyncIterator(a);
const bit = asyncIterator(b);
let [aElement, bElement] = await Promise.all([ait.next(), bit.next()] as const);
while (
aElement.done !== true &&
bElement.done !== true &&
(await elementsEqual(aElement.value, bElement.value))
) {
[aElement, bElement] = await Promise.all([ait.next(), bit.next()] as const);
}
return aElement.done === true && bElement.done === true;
}
export function asyncEqualOnceFn<T>(
b: AsyncIteratorLike<T>,
elementsEqual: (a: T, b: T) => boolean | Promise<boolean> = defaultEqual
): (a: AsyncIteratorLike<T>) => Promise<boolean> {
return async a => asyncEqualOnce(a, b, elementsEqual);
}
export async function asyncNotEqualOnce<T>(
a: AsyncIteratorLike<T>,
b: AsyncIteratorLike<T>,
elementsEqual: (a: T, b: T) => boolean | Promise<boolean> = defaultEqual
): Promise<boolean> {
return !(await asyncEqualOnce(a, b, elementsEqual));
}
export function asyncNotEqualOnceFn<T>(
b: AsyncIteratorLike<T>,
elementsEqual: (a: T, b: T) => boolean | Promise<boolean> = defaultEqual
): (a: AsyncIteratorLike<T>) => Promise<boolean> {
return async a => !(await asyncEqualOnce(a, b, elementsEqual));
}
export async function asyncPrefixMatchOnce<T>(
a: AsyncIteratorLike<T>,
b: AsyncIteratorLike<T>,
elementsEqual: (a: T, b: T) => boolean | Promise<boolean> = defaultEqual
): Promise<boolean> {
const ait = asyncIterator(a);
const bit = asyncIterator(b);
let [aElement, bElement] = await Promise.all([ait.next(), bit.next()] as const);
while (
aElement.done !== true &&
bElement.done !== true &&
(await elementsEqual(aElement.value, bElement.value))
) {
[aElement, bElement] = await Promise.all([ait.next(), bit.next()] as const);
}
return bElement.done === true;
}
export function asyncMapOnce<T, U>(
iterator: AsyncIteratorLike<T>,
f: (element: T, index: number) => U
): AsyncIterator<U> {
const it = asyncIterator(iterator);
let i = 0;
const done: IteratorResult<U> = {done: true, value: undefined};
const during = async (): Promise<IteratorResult<U>> => {
const element = await it.next();
if (element.done === true) {
next = after;
return done;
} else {
return {value: f(element.value, i++)};
}
};
const after = async (): Promise<IteratorResult<U>> => done;
let next = during;
return {next: async () => next()};
}
export function asyncMapOnceFn<T, U>(
f: (element: T, index: number) => U
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<U> {
return iterator => asyncMapOnce(iterator, f);
}
export function asyncFilterOnce<T, U extends T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => element is U
): AsyncIterator<U>;
export function asyncFilterOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): AsyncIterator<T>;
export function asyncFilterOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let i = 0;
return {
next: async () => {
let element = await it.next();
while (element.done !== true && !(await predicate(element.value, i++))) {
element = await it.next();
}
return element;
}
};
}
export function asyncFilterOnceFn<T, U extends T>(
predicate: (element: T, index: number) => element is U
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<U>;
export function asyncFilterOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T>;
export function asyncFilterOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncFilterOnce(iterator, predicate);
}
export function asyncExcludeOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let i = 0;
return {
next: async () => {
let element = await it.next();
while (element.done !== true && (await predicate(element.value, i++))) {
element = await it.next();
}
return element;
}
};
}
export function asyncExcludeOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncExcludeOnce(iterator, predicate);
}
export function asyncExcludeNullOnce<T>(
iterator: AsyncIteratorLike<T | null | undefined>
): AsyncIterator<T> {
return asyncFilterOnce(iterator, isNotNull);
}
export function asyncExcludeFirstOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): AsyncIterator<T> {
const it = asyncIterator(iterator);
let i = 0;
const before = async (): Promise<IteratorResult<T>> => {
const element = await it.next();
if (element.done !== true && !(await predicate(element.value, i++))) {
return element;
}
next = after;
return it.next();
};
const after = async (): Promise<IteratorResult<T>> => it.next();
let next = before;
return {next: async () => next()};
}
export function asyncExcludeFirstOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncExcludeFirstOnce(iterator, predicate);
}
export function asyncRemoveOnce<T>(
iterator: AsyncIteratorLike<T>,
value: T | Promise<T>
): AsyncIterator<T> {
return asyncExcludeOnce(iterator, async element => element === (await value));
}
export function asyncRemoveOnceFn<T>(
value: T | Promise<T>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncRemoveOnce(iterator, value);
}
export function asyncRemoveFirstOnce<T>(
iterator: AsyncIteratorLike<T>,
value: T | Promise<T>
): AsyncIterator<T> {
return asyncExcludeFirstOnce(iterator, async element => element === (await value));
}
export function asyncRemoveFirstOnceFn<T>(
value: T | Promise<T>
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
return iterator => asyncRemoveFirstOnce(iterator, value);
}
export async function asyncFoldOnce<T, U>(
iterator: AsyncIteratorLike<T>,
f: (accumulator: U, element: T, index: number) => U | Promise<U>,
initial: U | Promise<U>
): Promise<U> {
const it = asyncIterator(iterator);
let [element, accumulator] = await Promise.all([it.next(), initial] as const);
let i = 0;
while (element.done !== true) {
[accumulator, element] = await Promise.all([
f(accumulator, element.value, i++),
it.next()
] as const);
}
return accumulator;
}
export function asyncFoldOnceFn<T, U>(
f: (accumulator: U, element: T, index: number) => U | Promise<U>,
initial: U | Promise<U>
): (iterator: AsyncIteratorLike<T>) => Promise<U> {
return async iterator => asyncFoldOnce(iterator, f, initial);
}
export async function asyncFold1Once<T>(
iterator: AsyncIteratorLike<T>,
f: (accumulator: T, element: T, index: number) => T | Promise<T>
): Promise<T> {
const it = asyncIterator(iterator);
const element = await it.next();
if (element.done === true) {
throw new TypeError("asyncFold1Once: empty AsyncIterator");
}
return asyncFoldOnce(
it,
async (accumulator, element, index) => f(accumulator, element, index + 1),
element.value
);
}
export function asyncFold1OnceFn<T>(
f: (accumulator: T, element: T, index: number) => T | Promise<T>
): (iterator: AsyncIteratorLike<T>) => Promise<T> {
return async iterator => asyncFold1Once(iterator, f);
}
export async function asyncIndexOnce<T>(
iterator: AsyncIteratorLike<T>,
index: number | Promise<number>
): Promise<T | null> {
const it = asyncIterator(iterator);
const [i, e] = await Promise.all([index, it.next()] as const);
if (i < 0 || !isFinite(i) || Math.floor(i) !== i) {
throw new RangeError("illegal index");
}
let element = e;
for (let j = 0; element.done !== true && j < i; ++j) {
element = await it.next();
}
if (element.done === true) {
return null;
} else {
return element.value;
}
}
export function asyncIndexOnceFn<T>(
index: number | Promise<number>
): (iterator: AsyncIteratorLike<T>) => Promise<T | null> {
return async iterator => asyncIndexOnce(iterator, index);
}
export async function asyncContainsOnce<T>(
iterator: AsyncIteratorLike<T>,
value: T | Promise<T>
): Promise<boolean> {
const it = asyncIterator(iterator);
const [v, e] = await Promise.all([value, it.next()] as const);
let element = e;
while (element.done !== true) {
if (element.value === v) {
return true;
}
element = await it.next();
}
return false;
}
export function asyncContainsOnceFn<T>(
value: T | Promise<T>
): (iterator: AsyncIteratorLike<T>) => Promise<boolean> {
return async iterator => asyncContainsOnce(iterator, value);
}
export async function asyncIndexOfOnce<T>(
iterator: AsyncIteratorLike<T>,
value: T | Promise<T>
): Promise<number | null> {
const it = asyncIterator(iterator);
const [v, e] = await Promise.all([value, it.next()] as const);
let element = e;
for (let i = 0; element.done !== true; ++i) {
if (element.value === v) {
return i;
}
element = await it.next();
}
return null;
}
export function asyncIndexOfOnceFn<T>(
value: T | Promise<T>
): (iterator: AsyncIteratorLike<T>) => Promise<number | null> {
return async iterator => asyncIndexOfOnce(iterator, value);
}
export async function asyncFindIndexOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): Promise<number | null> {
const it = asyncIterator(iterator);
let element = await it.next();
for (let i = 0; element.done !== true; ++i) {
if (await predicate(element.value, i)) {
return i;
}
element = await it.next();
}
return null;
}
export function asyncFindIndexOnceFn<T>(
predicate: (element: T, index: number) => boolean
): (iterator: AsyncIteratorLike<T>) => Promise<number | null> {
return async iterator => asyncFindIndexOnce(iterator, predicate);
}
export async function asyncFindOnce<T, U extends T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => element is U
): Promise<U | null>;
export async function asyncFindOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): Promise<T | null>;
export async function asyncFindOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): Promise<T | null> {
const it = asyncIterator(iterator);
let element = await it.next();
for (let i = 0; element.done !== true; ++i) {
if (await predicate(element.value, i)) {
return element.value;
}
element = await it.next();
}
return null;
}
export function asyncFindOnceFn<T, U extends T>(
predicate: (element: T, index: number) => element is U
): (iterator: AsyncIteratorLike<T>) => Promise<U | null>;
export function asyncFindOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => Promise<T | null>;
export function asyncFindOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => Promise<T | null> {
return async iterator => asyncFindOnce(iterator, predicate);
}
export async function asyncMaximumOnce<T extends string | number | boolean>(
iterator: AsyncIteratorLike<T>
): Promise<T | null>;
export async function asyncMaximumOnce<T>(
iterator: AsyncIteratorLike<T>,
compare: Comparator<T>
): Promise<T | null>;
export async function asyncMaximumOnce<T>(
iterator: AsyncIteratorLike<T>,
compare?: Comparator<T>
): Promise<T | null> {
return internalAsyncMaximumOnce(
iterator,
compare ?? (defaultCompare as unknown as Comparator<T>)
);
}
export function asyncMaximumOnceFn<T>(
compare: Comparator<T>
): (iterator: AsyncIteratorLike<T>) => Promise<T | null> {
return async iterator => internalAsyncMaximumOnce(iterator, compare);
}
async function internalAsyncMaximumOnce<T>(
iterator: AsyncIteratorLike<T>,
compare: Comparator<T>
): Promise<T | null> {
const it = asyncIterator(iterator);
let element = await it.next();
if (element.done === true) {
return null;
}
let max = element.value;
element = await it.next();
while (element.done !== true) {
if (compare(element.value, max) > 0) {
max = element.value;
}
element = await it.next();
}
return max;
}
export async function asyncMaximumByOnce<T>(
iterator: AsyncIteratorLike<T>,
select: (element: T, index: number) => number | Promise<number>
): Promise<T | null> {
const it = asyncIterator(iterator);
let element = await it.next();
if (element.done === true) {
return null;
}
let max = element.value;
let maxBy: number;
[maxBy, element] = await Promise.all([select(element.value, 0), it.next()] as const);
for (let i = 1; element.done !== true; ++i) {
const [by, next] = await Promise.all([select(element.value, i), it.next()] as const);
if (by > maxBy) {
max = element.value;
maxBy = by;
}
element = next;
}
return max;
}
export function asyncMaximumByOnceFn<T>(
select: (element: T, index: number) => number | Promise<number>
): (iterator: AsyncIteratorLike<T>) => Promise<T | null> {
return async iterator => asyncMaximumByOnce(iterator, select);
}
export async function asyncMinimumOnce<T extends string | number | boolean>(
iterator: AsyncIteratorLike<T>
): Promise<T | null>;
export async function asyncMinimumOnce<T>(
iterator: AsyncIteratorLike<T>,
compare: Comparator<T>
): Promise<T | null>;
export async function asyncMinimumOnce<T>(
iterator: AsyncIteratorLike<T>,
compare?: Comparator<T>
): Promise<T | null> {
return internalAsyncMaximumOnce(
iterator,
reverse(compare ?? (defaultCompare as unknown as Comparator<T>))
);
}
export function asyncMinimumOnceFn<T>(
compare: Comparator<T>
): (iterator: AsyncIteratorLike<T>) => Promise<T | null> {
return async iterator => internalAsyncMaximumOnce(iterator, reverse(compare));
}
export async function asyncMinimumByOnce<T>(
iterator: AsyncIteratorLike<T>,
select: (element: T, index: number) => number | Promise<number>
): Promise<T | null> {
const it = asyncIterator(iterator);
let element = await it.next();
if (element.done === true) {
return null;
}
let min = element.value;
let minBy: number;
[minBy, element] = await Promise.all([select(element.value, 0), it.next()] as const);
for (let i = 1; element.done !== true; ++i) {
const [by, next] = await Promise.all([select(element.value, i), it.next()] as const);
if (by < minBy) {
min = element.value;
minBy = by;
}
element = next;
}
return min;
}
export function asyncMinimumByOnceFn<T>(
select: (element: T, index: number) => number | Promise<number>
): (iterator: AsyncIteratorLike<T>) => Promise<T | null> {
return async iterator => asyncMinimumByOnce(iterator, select);
}
export async function asyncSumOnce(iterator: AsyncIteratorLike<number>): Promise<number> {
return asyncFoldOnce(iterator, (a, e) => a + e, 0);
}
export async function asyncProductOnce(iterator: AsyncIteratorLike<number>): Promise<number> {
return asyncFoldOnce(iterator, (a, e) => a * e, 1);
}
export async function asyncAverageOnce(
iterator: AsyncIteratorLike<number>
): Promise<number | null> {
const [sum, count] = await asyncFoldOnce(
iterator,
([sum], element, index) => [sum + element, index + 1],
[0, 0]
);
return count === 0 ? null : sum / count;
}
export async function asyncAndOnce(iterator: AsyncIteratorLike<boolean>): Promise<boolean> {
return (await asyncFindIndexOnce(iterator, element => !element)) == null;
}
export async function asyncOrOnce(iterator: AsyncIteratorLike<boolean>): Promise<boolean> {
return (await asyncFindIndexOnce(iterator, element => element)) != null;
}
export async function asyncAnyOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): Promise<boolean> {
return (await asyncFindIndexOnce(iterator, predicate)) != null;
}
export function asyncAnyOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => Promise<boolean> {
return async iterator => asyncAnyOnce(iterator, predicate);
}
export async function asyncAllOnce<T>(
iterator: AsyncIteratorLike<T>,
predicate: (element: T, index: number) => boolean | Promise<boolean>
): Promise<boolean> {
return (
(await asyncFindIndexOnce(
iterator,
async (element, index) => !(await predicate(element, index))
)) == null
);
}
export function asyncAllOnceFn<T>(
predicate: (element: T, index: number) => boolean | Promise<boolean>
): (iterator: AsyncIteratorLike<T>) => Promise<boolean> {
return async iterator => asyncAllOnce(iterator, predicate);
}
export function asyncConcatOnce<T>(
iterators: AsyncIteratorLike<AsyncIteratorLike<T>>
): AsyncIterator<T> {
const its = asyncIterator(iterators);