-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
1552 lines (1370 loc) · 48.3 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
// MIT License
//
// Copyright (c) 2023 Feast
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// noinspection JSUnusedGlobalSymbols,JSUnusedLocalSymbols
declare interface Generator<T> {
/**
* Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
* @param seed The initial accumulator value.
* @param accumulator An accumulator function to be invoked on each element.
*/
aggregate<TSeed, TReturn>(
seed : TSeed,
accumulator : (seed : TSeed, item : T) => TReturn) : TReturn;
/**
* Determines whether all elements of a sequence satisfy a condition.
* @param predicate
*/
all(predicate : (item : T) => boolean) : boolean;
/**
* Determines whether a sequence contains any elements.
*/
any() : boolean;
/**
* Determines whether any element of a sequence satisfies a condition.
* @param predicate A function to test each element for a condition.
*/
any(predicate : (item : T) => boolean) : boolean;
/**
* Appends a value to the end of the sequence.
* @param element The value to append to
*/
append(element : T) : IEnumerable<T>;
/**
* Returns the input typed as enumerable.
*/
asEnumerable() : IEnumerable<T>;
/**
* Splits the elements of a sequence into chunks of size at most
* @param size The maximum size of each chunk.
*/
chunk(size : number) : IEnumerable<Array<T>>;
/**
* Concatenates two sequences.
* @param source The sequence to concatenate to the current.
*/
concat(source : IEnumerable<T>) : IEnumerable<T>;
/**
* Determines whether a sequence contains a specified element by using the default equality comparer.
* @param value The value to locate in the sequence.
*/
contains(value : T) : boolean;
/**
* Determines whether a sequence contains a specified element by using a specified comparer.
* @param value The value to locate in the sequence.
* @param comparer An equality comparer to compare values.
*/
contains(
value : T,
comparer? : (left : T, right : T) => boolean) : boolean;
/**
* Returns the number of elements in a sequence.
*/
count() : number;
/**
* Returns a number that represents how many elements in the specified sequence satisfy a condition.
* @param predicate A function to test each element for a condition.
*/
count(predicate : (item : T) => boolean) : number;
/**
* Returns distinct elements from a sequence by using the default equality comparer to compare values.
*/
distinct() : IEnumerable<T>;
/**
* Returns distinct elements from a sequence by using a specified comparer to compare values.
* @param comparer An comparer to compare values.
*/
distinct(comparer : (left : T, right : T) => boolean) : IEnumerable<T>;
/**
* Returns distinct elements from a sequence according to a specified key selector function.
* @param keySelector A function to extract the key for each element.
*/
distinctBy<TKey>(keySelector : (item : T) => TKey) : IEnumerable<T>;
/**
* Returns distinct elements from a sequence according to a specified key selector function and using a specified
* comparer to compare keys.
* @param keySelector A function to extract the key for each element.
* @param comparer An comparer to compare keys.
*/
distinctBy<TKey>(
keySelector : (item : T) => TKey,
comparer? : (left : TKey, right : TKey) => boolean) : IEnumerable<T>;
/**
* Returns the element at a specified index in a sequence.
* @param index The zero-based index of the element to retrieve.
*/
elementAt(index : number) : T;
/**
* Returns the element at a specified index in a sequence.
* @param index The index of the element to retrieve, which is either from the beginning or the end of the sequence.
*/
elementAtOrDefault(index : number) : T | null;
/**
* Produces the set difference of two sequences by using the default equality comparer to compare values.
* @param source An array whose elements that also occur in the first sequence will cause those elements to be
* removed from the returned sequence.
*/
except(source : IEnumerable<T>) : IEnumerable<T>;
/**
* Produces the set difference of two sequences by using the specified comparer to compare values.
* @param source An array whose elements that also occur in the first sequence will cause those elements to be
* removed from the returned sequence.
* @param comparer An comparer to compare values.
*/
except(
source : IEnumerable<T>,
comparer? : (left : T, right : T) => boolean) : IEnumerable<T>;
/**
* Produces the set difference of two sequences according to a specified key selector function.
* @param source An source whose keys that also occur in the first sequence will cause those elements to be removed
* from the returned sequence.
* @param keySelector A function to extract the key for each element.
*/
exceptBy<TKey>(
source : IEnumerable<T>,
keySelector : (item : T) => TKey) : IEnumerable<T>;
/**
* Produces the set difference of two sequences according to a specified key selector function.
* @param source An source whose keys that also occur in the first sequence will cause those elements to be removed
* from the returned sequence.
* @param keySelector A function to extract the key for each element.
* @param comparer The comparer to compare values.
*/
exceptBy<TKey>(
source : IEnumerable<T>,
keySelector : (item : T) => TKey,
comparer? : (left : TKey, right : TKey) => boolean) : IEnumerable<T>;
/**
* Returns the first element of a sequence.
*/
first() : T;
/**
* Returns the first element in a sequence that satisfies a specified condition.
* @param predicate A function to test each element for a condition.
*/
first(predicate : (item : T) => boolean) : T;
/**
* Returns the first element of a sequence, or a default value if the sequence contains no elements.
*/
firstOrDefault() : T | null;
/**
* Returns the first element of a sequence, or a specified default value if the sequence contains no elements.
* @param defaultValue The default value to return if the sequence is empty.
*/
firstOrDefault(defaultValue : T) : T | null;
/**
* Returns the first element of a sequence, or a default value if the sequence contains no elements.
* @param predicate A function to test each element for a condition.
*/
firstOrDefault(predicate : (item : T) => boolean) : T | null;
/**
* Returns the first element of the sequence that satisfies a condition, or a specified default value if no such element is found.
* @param predicate A function to test each element for a condition.
* @param defaultValue The default value to return if the sequence is empty.
*/
firstOrDefault(
predicate : (item : T) => boolean,
defaultValue : T) : T | null;
/**
* Groups the elements of a sequence according to a specified key selector function.
* @param keySelector A function to extract the key for each element.
*/
groupBy<TKey>(keySelector : (item : T) => TKey) : IEnumerable<Array<T> & { key : TKey }>;
/**
* Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer.
* @param keySelector A function to extract the key for each element.
* @param comparer An comparer to compare keys.
*/
groupBy<TKey>(
keySelector : (item : T) => TKey,
comparer : (left : TKey, right : TKey) => boolean) : IEnumerable<Array<T> & { key : TKey }>;
/**
* Groups the elements of a sequence according to a specified key selector function and projects the elements for
* each group by using a specified function.
* @param keySelector A function to extract the key for each element.
* @param elementSelector A function to map each source element to an element in the source.
*/
groupBy<TKey, TElement>(
keySelector : (item : T) => TKey,
elementSelector : (item : T) => TElement) : IEnumerable<Array<TElement> & { key : TKey }>;
/**
* Groups the elements of a sequence according to a key selector function. The keys are compared by using a comparer
* and each group's elements are projected by using a specified function.
* @param keySelector A function to extract the key for each element.
* @param elementSelector A function to map each source element to an element in the source.
* @param comparer An comparer to compare keys.
*/
groupBy<TKey, TElement>(
keySelector : (item : T) => TKey,
elementSelector : (item : T) => TElement,
comparer : (left : TKey, right : TKey) => boolean) : IEnumerable<Array<TElement> & { key : TKey }>;
/**
* Correlates the elements of two sequences based on equality of keys and groups the results. The default equality
* comparer is used to compare keys.
* @param inner The sequence to join to the sequence.
* @param keySelector A function to extract the join key from each element of the source sequence.
* @param innerKeySelector A function to extract the join key from each element of the inner sequence.
* @param resultSelector A function to create a result element from an element from the first sequence and a collection
* of matching elements from the second sequence.
*/
groupJoin<TInner, TKey, TResult>(
inner : IEnumerable<TInner>,
keySelector : (item : T) => TKey,
innerKeySelector : (item : TInner) => TKey,
resultSelector : (outer : T, inner : Array<TInner>) => TResult) : IEnumerable<TResult>;
/**
* Correlates the elements of two sequences based on equality of keys and groups the results. A specified comparer
* is used to compare keys.
* @param inner The sequence to join to the sequence.
* @param keySelector A function to extract the join key from each element of the source sequence.
* @param innerKeySelector A function to extract the join key from each element of the inner sequence.
* @param resultSelector A function to create a result element from an element from the first sequence and a collection
* of matching elements from the second sequence.
* @param comparer A comparer to hash and compare keys.
*/
groupJoin<TInner, TKey, TResult>(
inner : IEnumerable<TInner>,
keySelector : (item : T) => TKey,
innerKeySelector : (item : TInner) => TKey,
resultSelector : (outer : T, inner : Array<TInner>) => TResult,
comparer : (outerKey : TKey, innerKey : TKey) => boolean) : IEnumerable<TResult>;
/**
* Correlates the elements of two sequences based on matching keys. The default equality comparer is used to compare keys.
* @param inner The sequence to join to the sequence.
* @param keySelector A function to extract the join key from each element of the source sequence.
* @param innerKeySelector A function to extract the join key from each element of the inner sequence.
* @param resultSelector A function to create a result element from two matching elements.
*/
join<TInner, TKey, TResult>(
inner : IEnumerable<TInner>,
keySelector : (item : T) => TKey,
innerKeySelector : (item : TInner) => TKey,
resultSelector : (outer : T, inner : TInner) => TResult) : IEnumerable<TResult>;
/**
* Correlates the elements of two sequences based on matching keys. A specified comparer is used to compare keys.
* @param inner The sequence to join to the sequence.
* @param keySelector A function to extract the join key from each element of the source sequence.
* @param innerKeySelector A function to extract the join key from each element of the inner sequence.
* @param resultSelector A function to create a result element from two matching elements.
* @param comparer A comparer to hash and compare keys.
*/
join<TInner, TKey, TResult>(
inner : IEnumerable<TInner>,
keySelector : (item : T) => TKey,
innerKeySelector : (item : TInner) => TKey,
resultSelector : (outer : T, inner : TInner) => TResult,
comparer : (outerKey : TKey, innerKey : TKey) => boolean) : IEnumerable<TResult>;
/**
* Returns the last element of a sequence.
*/
last() : T;
/**
* Returns the last element of a sequence that satisfies a specified condition.
* @param predicate A function to test each element for a condition.
*/
last(predicate : (item : T) => boolean) : T;
/**
* Returns the last element of a sequence, or a default value if the sequence contains no elements.
*/
lastOrDefault() : T | null;
/**
* Returns the last element of a sequence, or a specified default value if the sequence contains no elements.
* @param defaultValue The default value to return if the sequence is empty.
*/
lastOrDefault(defaultValue : T) : T | null;
/**
* Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
* @param predicate A function to test each element for a condition.
*/
lastOrDefault(predicate : (item : T) => boolean) : T | null;
/**
* Returns the last element of a sequence that satisfies a condition, or a specified default value if no such element is found.
* @param predicate A function to test each element for a condition.
* @param defaultValue The default value to return if the sequence is empty.
*/
lastOrDefault(
predicate : (item : T) => boolean,
defaultValue : T) : T | null;
/**
* Sorts the elements of a sequence in ascending order.
*/
order() : IEnumerable<T>;
/**
* Sorts the elements of a sequence in ascending order.
* @param comparer An comparer to compare keys.
*/
order(comparer : (current : T, exist : T) => number) : IEnumerable<T>;
/**
* Sorts the elements of a sequence in ascending order according to a key.
* @param keySelector A function to extract a key from an element.
*/
orderBy<TKey>(keySelector : (item : T) => TKey) : IEnumerable<T>;
/**
* Sorts the elements of a sequence in ascending order according to a key.
* @param keySelector A function to extract a key from an element.
* @param comparer An comparer to compare keys.
*/
orderBy<TKey>(
keySelector : (item : T) => TKey,
comparer : (current : TKey, exist : TKey) => number) : IEnumerable<T>;
/**
* Sorts the elements of a sequence in descending order.
*/
orderDescending() : IEnumerable<T>;
/**
* Sorts the elements of a sequence in descending order.
* @param comparer An comparer to compare keys.
*/
orderDescending(comparer : (current : T, exist : T) => number) : IEnumerable<T>;
/**
* Sorts the elements of a sequence in descending order according to a key.
* @param keySelector A function to extract a key from an element.
*/
orderByDescending<TKey>(keySelector : (item : T) => TKey) : IEnumerable<T>;
/**
* Sorts the elements of a sequence in descending order according to a key.
* @param keySelector A function to extract a key from an element.
* @param comparer An comparer to compare keys.
*/
orderByDescending<TKey>(
keySelector : (item : T) => TKey,
comparer : (current : TKey, exist : TKey) => number) : IEnumerable<T>;
/**
* Adds a value to the beginning of the sequence.
* @param element The value to prepend to
*/
prepend(element : T) : IEnumerable<T>;
/**
* Inverts the order of the elements in a sequence.
*/
reverse() : IEnumerable<T>;
/**
* Projects each element of a sequence into a new form by incorporating the element's index.
* @param selector A transform function to apply to each element.
*/
select<TReturn>(selector : (item : T) => TReturn) : IEnumerable<TReturn>;
/**
* Projects each element of a sequence into a new form by incorporating the element's index.
* @param selector A transform function to apply to each element. The second parameter of the function
* represents the index of the source element.
*/
select<TReturn>(selector : (item : T, index : number) => TReturn) : IEnumerable<TReturn>;
/**
* Projects each element of a sequence to an array and flattens the resulting sequences into one sequence.
* @param selector A transform function to apply to each element.
*/
selectMany<TReturn>(selector : (item : T) => IEnumerable<TReturn>) : IEnumerable<TReturn>;
/**
* Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than
* one such element exists.
*/
single() : T;
/**
* Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than
* one such element exists.
* @param predicate A function to test an element for a condition.
*/
single(predicate : (item : T) => boolean) : T;
/**
* Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception
* if there is more than one element in the sequence.
*/
singleOrDefault() : T | null;
/**
* Returns the only element of a sequence, or a specified default value if the sequence is empty; this method throws
* an exception if there is more than one element in the sequence.
* @param defaultValue The default value to return if the sequence is empty.
*/
singleOrDefault(defaultValue : T) : T | null;
/**
* Returns the only element of a sequence, or a specified default value if the sequence is empty; this method throws
* an exception if there is more than one element in the sequence.
* @param predicate A function to test an element for a condition.
*/
singleOrDefault(predicate : (item : T) => boolean) : T | null;
/**
* Returns the only element of a sequence that satisfies a specified condition, or a specified default value if no
* such element exists; this method throws an exception if more than one element satisfies the condition.
* @param predicate A function to test an element for a condition.
* @param defaultValue The default value to return if the sequence is empty.
*/
singleOrDefault(
predicate : (item : T) => boolean,
defaultValue : T) : T | null;
/**
* Bypasses a specified number of elements in a sequence and then returns the remaining elements.
* @param count The number of elements to skip before returning the remaining elements.
*/
skip(count : number) : IEnumerable<T>;
/**
* Returns a new enumerable collection that contains the elements from source with the last count elements of the
* source collection omitted.
* @param count The number of elements to omit from the end of the collection.
*/
skipLast(count : number) : IEnumerable<T>;
/**
* Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
* @param predicate A function to test each element for a condition.
*/
skipWhile(predicate : (item : T) => boolean) : IEnumerable<T>;
/**
* Returns a specified number of contiguous elements from the start of a sequence.
* @param count The number of elements to return
*/
take(count : number) : IEnumerable<T>;
/**
* Returns a specified number of contiguous elements from the start of a sequence.
* @param range The range of elements to return, which has start and end indexes either from the beginning or the
* end of the sequence.
*/
take(range : [start : number, end : number]) : IEnumerable<T>;
/**
* Returns a new enumerable collection that contains the last count elements from source.
* @param count The number of elements to take from the end of the collection.
*/
takeLast(count : number) : IEnumerable<T>;
/**
* Returns elements from a sequence as long as a specified condition is true.
* @param predicate A function to test each element for a condition.
*/
takeWhile(predicate : (item : T) => boolean) : IEnumerable<T>;
/**
* Returns elements from a sequence as long as a specified condition is true.
* @param predicate A function to test each element for a condition. The second parameter of the function
* represents the index of the source element.
*/
takeWhile(predicate : (item : T, index : number) => boolean) : IEnumerable<T>;
/**
* Creates an array from an enumerable.
*/
toArray() : Array<T>;
/**
* Creates a Map from an enumerable according to specified key selector and element selector functions.
* @param keySelector A function to extract a key from each element.
* @param valueSelector A transform function to produce a result element value from each element.
*/
toDictionary<K, V>(keySelector : (item : T) => K, valueSelector : (item : T) => V) : Map<K, V>;
/**
* Produces the set union of two sequences by using a specified comparer.
* @param source An enumerable whose distinct elements form the second set for the union.
* @param comparer The comparer to compare values.
*/
union(source : IEnumerable<T>, comparer? : (left : T, right : T) => boolean) : IEnumerable<T>;
/**
* Produces the set union of two sequences according to a specified key selector function.
* @param source An enumerable whose distinct elements form the second set for the union.
* @param keySelector A function to extract the key for each element.
* @param comparer The comparer to compare values.
*/
unionBy<TKey>(
source : IEnumerable<T>, keySelector : (item : T) => TKey,
comparer? : (left : TKey, right : TKey) => boolean) : IEnumerable<T>;
/**
* Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function.
* @param predicate A function to test each source element for a condition;
*/
where(predicate : (item : T) => boolean) : IEnumerable<T>;
/**
* Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function.
* @param predicate A function to test each source element for a condition; The second parameter of the function
* represents the index of the source element.
*/
where(predicate : (item : T, index : number) => boolean) : IEnumerable<T>;
}
(function () {
/**
* Compatible array's existing types cache
*/
const compatibles = (function () {
const concat = Array.prototype.concat;
const join = Array.prototype.join;
return {
concat: function <T>(thisArg : any, ...items : (T | ConcatArray<T>)[]) : T[] {
return concat.call(thisArg, ...items);
},
join : function (thisArg : any, separator : string) : string {
return join.call(thisArg, separator);
}
};
})()
interface IEnumerableArray<T> extends IEnumerable<T> {
/**
* Adds the given object to the end of this list. The size of the list is
* increased by one. If required, the capacity of the list is doubled
* before adding the new element.
* @param item
*/
add(item : T) : void
/**
* Adds the elements of the given collection to the end of this list. If
* required, the capacity of the list is increased to twice the previous
* capacity or the new size, whichever is larger.
* @param items
*/
addRange(items : IEnumerable<T>) : void
/**
* Clears the contents of List.
*/
clear() : void
/**
* Determines whether the array contains elements that match the conditions defined by the specified predicate.
* @param match The delegate that defines the conditions of the elements to search for.
*/
exists(match : (item : T) => boolean) : boolean
/**
* Retrieves all the elements that match the conditions defined by the specified predicate.
* @param match The delegate that defines the conditions of the elements to search for.
*/
findAll(match : (item : T) => boolean) : Array<T>
/**
* Creates a shallow copy of a range of elements in the source.
* @param start The zero-based index at which the range starts.
* @param count The number of elements in the range.
*/
getRange(
start : number,
count : number) : Array<T>
/**
* Inserts an element into the array at the specified index.
* @param index The zero-based index at which item should be inserted.
* @param item The object to insert. The value can be null for reference types.
*/
insert(
index : number,
item : T) : void
/**
* Removes the first occurrence of a specific object from the array.
* @param item The object to remove from the array. The value can be null for reference types.
*/
remove(item : T) : boolean
/**
* Removes all the elements that match the conditions defined by the specified predicate.
* @param match The delegate that defines the conditions of the elements to remove.
*/
removeAll(match : (item : T) => boolean) : void
/**
* Removes the element at the specified index of the array.
* @param index The zero-based index of the element to remove.
*/
removeAt(index : number) : T | undefined
}
interface IEnumerableMap<K, V> extends IEnumerable<[K, V]> {
containsKey(key : K) : boolean
containsValue(value : V) : boolean
tryAdd(
key : K,
value : V) : boolean
tryGetValue(
key : K,
valueGetter : (value : V) => void) : boolean
}
class Enumerable<T> implements IEnumerable<T> {
[Symbol.iterator]() : Iterator<T> {
return this
}
next(...args : [] | [unknown]) : IteratorResult<T, T>;
next(...args : [] | [unknown]) : IteratorResult<T, T>;
next(...args : [] | [unknown]) : IteratorResult<T, T> {
return undefined;
}
return(value : any) : IteratorResult<T, T> {
return undefined;
}
throw(e : any) : IteratorResult<T, T> {
return undefined;
}
aggregate<TSeed, TReturn>(
seed : TSeed,
accumulator : (seed : TSeed, item : T) => TReturn) : TReturn {
let tmp : TSeed | TReturn = seed;
for (const item of this) tmp = accumulator(seed, item)
return tmp as TReturn;
}
all(predicate : (item : T) => boolean) : boolean {
for (const item of this) if (!predicate(item)) return false;
return true;
}
any() : boolean;
any(predicate : (item : T) => boolean) : boolean;
any(predicate? : (item : T) => boolean) : boolean {
return this.firstOrDefault(predicate) != null;
}
* append(element : T) : IEnumerable<T> {
yield * this
yield element
}
asEnumerable() : IEnumerable<T> {
return this;
}
* chunk(size : number) : IEnumerable<Array<T>> {
let chunk = new Array<T>()
for (const item of this) {
if (chunk.length === size) {
yield chunk
chunk = []
}
chunk.push(item)
}
yield chunk
}
* concat(source : IEnumerable<T>) : IEnumerable<T> {
yield * this
yield * source
}
contains(value : T) : boolean;
contains(value : T, comparer : (left : T, right : T) => boolean) : boolean;
contains(value : T, comparer? : (left : T, right : T) => boolean) : boolean {
comparer ??= Object.is;
for (const item of this) if (comparer(value, item)) return true;
return false
}
count() : number;
count(predicate : (item : T) => boolean) : number;
count(predicate? : (item : T) => boolean) : number {
predicate ??= () => true;
let count = 0;
for (const item of this) if (predicate(item)) count++;
return count;
}
distinct() : IEnumerable<T>;
distinct(comparer : (left : T, right : T) => boolean) : IEnumerable<T>;
* distinct(comparer? : (left : T, right : T) => boolean) : IEnumerable<T>
{
comparer ??= Object.is;
const stack = []
for (const item of this) {
if (stack.findIndex((x : any) => comparer(x, item)) < 0) {
stack.push(item)
}
}
yield * stack
}
distinctBy<TKey>(keySelector : (item : T) => TKey) : IEnumerable<T>;
* distinctBy<TKey>(
keySelector : (item : T) => TKey,
comparer? : (left : TKey, right : TKey) => boolean) : IEnumerable<T>
{
comparer ??= Object.is;
const stack = []
for (const item of this) {
if (stack.findIndex((x : any) => comparer(keySelector(x), keySelector(item))) < 0) {
stack.push(item)
}
}
yield * stack
}
elementAt(index : number) : T {
const ret = this.elementAtOrDefault(index);
if (ret == null) throw 'Yield no result'
return ret;
}
elementAtOrDefault(index : number) : T | null {
for (const item of this) {
if (index === 0) return item;
index--;
}
return null;
}
except(source : IEnumerable<T>) : IEnumerable<T>;
except(
source : IEnumerable<T>,
comparer? : (left : T, right : T) => boolean) : IEnumerable<T>;
* except(
source : IEnumerable<T>,
comparer? : (left : T, right : T) => boolean) : IEnumerable<T>
{
comparer ??= Object.is
const compares = []
for (const item of source) {
compares.push(item)
}
for (const item of this) {
if (compares.findIndex((x : any) => comparer(x, item)) < 0) {
yield item
compares.push(item)
}
}
}
exceptBy<TKey>(
source : IEnumerable<T>,
keySelector : (item : T) => TKey) : IEnumerable<T>;
* exceptBy<TKey>(
source : IEnumerable<T>,
keySelector : (item : T) => TKey,
comparer? : (left : TKey, right : TKey) => boolean) : IEnumerable<T>
{
comparer ??= Object.is
const compares = []
for (const item of source) {
compares.push(item)
}
for (const item of this) {
if (compares.findIndex(x => comparer(keySelector(x), keySelector(item))) < 0) {
yield item
compares.push(item)
}
}
}
first() : T;
first(predicate : (item : T) => boolean) : T;
first(predicate? : (item : T) => boolean) : T {
const ret = this.firstOrDefault(predicate);
if (ret == null) throw 'Yield no result'
return ret;
}
firstOrDefault() : T | null;
firstOrDefault(defaultValue : T) : T | null;
firstOrDefault(predicate : (item : T) => boolean) : T | null;
firstOrDefault(
predicate : (item : T) => boolean,
defaultValue : T) : T | null;
firstOrDefault(
predicate? : T | ((item : T) => boolean),
defaultValue? : T) : T | null {
predicate ??= () => true;
if (typeof (predicate) != 'function') {
defaultValue = predicate;
predicate = () => true;
}
for (const item of this) if ((predicate as (item? : T) => boolean)(item)) return item;
return defaultValue;
}
groupBy<TKey>(keySelector : (item : T) => TKey) : IEnumerable<Array<T> & { key : TKey }>;
groupBy<TKey>(
keySelector : (item : T) => TKey,
comparer : (left : TKey, right : TKey) => boolean) : IEnumerable<Array<T> & { key : TKey }>;
groupBy<TKey, TElement>(
keySelector : (item : T) => TKey,
elementSelector : (item : T) => TElement) : IEnumerable<Array<TElement> & { key : TKey }>;
groupBy<TKey, TElement>(
keySelector : (item : T) => TKey,
elementSelector : (item : T) => TElement,
comparer : (left : TKey, right : TKey) => boolean) : IEnumerable<Array<TElement> & { key : TKey }>;
* groupBy<TKey, TElement>(
keySelector : (item : T) => TKey,
elementSelector? : ((item : T) => TElement) | ((left : TKey, right : TKey) => boolean),
comparer? : (left : TKey, right : TKey) => boolean)
: IEnumerable<Array<T> & { key : TKey } | Array<TElement> & { key : TKey }>
{
comparer ??= Object.is;
elementSelector ??= (x : any) => x
if (elementSelector.length == 2) {
comparer = elementSelector as (left : TKey, right : TKey) => boolean
elementSelector = (x : any) => x
}
const groups = []
for (const item of this) {
const key = keySelector(item)
let cache = groups.find(x => comparer(x.key, key))
if (!cache) {
cache = []
cache.key = key
groups.push(cache)
}
cache.push((elementSelector as (item : T) => any)(item))
}
for (const group of groups) yield group;
}
groupJoin<TInner, TKey, TResult>(
inner : IEnumerable<TInner>,
keySelector : (item : T) => TKey,
innerKeySelector : (item : TInner) => TKey,
resultSelector : (outer : T, inner : Array<TInner>) => TResult) : IEnumerable<TResult>;
groupJoin<TInner, TKey, TResult>(
inner : IEnumerable<TInner>,
keySelector : (item : T) => TKey,
innerKeySelector : (item : TInner) => TKey,
resultSelector : (outer : T, inner : Array<TInner>) => TResult,
comparer : ((outerKey : TKey, innerKey : TKey) => boolean)) : IEnumerable<TResult>
* groupJoin<TInner, TKey, TResult>(
inner : IEnumerable<TInner>,
keySelector : (item : T) => TKey,
innerKeySelector : (item : TInner) => TKey,
resultSelector : (outer : T, inner : Array<TInner>) => TResult,
comparer? : ((outerKey : TKey, innerKey : TKey) => boolean)) : IEnumerable<TResult>
{
comparer ??= Object.is;
for (const item of this) {
const key = keySelector(item)
const stack = new Array<TInner>()
for (const innerItem of inner) {
const innerKey = innerKeySelector(innerItem)
if (comparer(key, innerKey)) {
stack.push(innerItem)
}
}
yield resultSelector(item, stack)
}
}
join<TInner, TKey, TResult>(
inner : IEnumerable<TInner>,
keySelector : (item : T) => TKey,
innerKeySelector : (item : TInner) => TKey,
resultSelector : (outer : T, inner : TInner) => TResult) : IEnumerable<TResult>;
join<TInner, TKey, TResult>(
inner : IEnumerable<TInner>,
keySelector : (item : T) => TKey,
innerKeySelector : (item : TInner) => TKey,
resultSelector : (outer : T, inner : TInner) => TResult,
comparer : ((outerKey : TKey, innerKey : TKey) => boolean)) : IEnumerable<TResult>;
* join<TInner, TKey, TResult>(
inner : IEnumerable<TInner>,
keySelector : (item : T) => TKey,
innerKeySelector : (item : TInner) => TKey,
resultSelector : (outer : T, inner : TInner) => TResult,
comparer? : ((outerKey : TKey, innerKey : TKey) => boolean)) : IEnumerable<TResult>
{
comparer ??= Object.is;
for (const item of this) {
const key = keySelector(item)
for (const innerItem of inner) {
const innerKey = innerKeySelector(innerItem)
if (comparer(key, innerKey)) {
yield resultSelector(item, innerItem)
}
}
}
}
last() : T;
last(predicate : (item : T) => boolean) : T;
last(predicate? : (item : T) => boolean) : T {
const ret = this.lastOrDefault(predicate)
if (ret == null) throw 'Yield no result'
return ret;
}
lastOrDefault() : T | null;
lastOrDefault(defaultValue : T) : T | null;
lastOrDefault(predicate : (item : T) => boolean) : T | null;
lastOrDefault(
predicate : (item : T) => boolean,
defaultValue : T) : T | null;
lastOrDefault(
predicate? : T | ((item : T) => boolean),
defaultValue? : T) : T | null {
predicate ??= () => true;
if (typeof (predicate) != 'function') {
defaultValue = predicate;
predicate = () => true;
}
let last = defaultValue;
for (const item of this) {
if ((predicate as (item? : T) => boolean)(item)) last = item;
}
return last;
}
order() : IEnumerable<T>;
order(comparer : (current : T, exist : T) => number) : IEnumerable<T>;
* order(comparer? : (current : T, exist : T) => number) : IEnumerable<T> {
comparer ??= (left, right) => left as any - (right as any)
const stack = []
for (const item of this) {