forked from lord-saumagen/TypeScript-LINQ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTS-Linq.js
More file actions
3386 lines (3386 loc) · 156 KB
/
TS-Linq.js
File metadata and controls
3386 lines (3386 loc) · 156 KB
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
/// <reference path="../_references.ts" />
var TS;
(function (TS) {
var Linq;
(function (Linq) {
/**
* @class TS.Linq.BaseEnumerator<T>
*
* @description The main purpose of this class is to implement the extension functions defined in
* 'TS.Linq.Extensions' in order to make them available in subclasses.
*
* @abstract
*
* @implements {Iterable<T>}
*/
class BaseEnumerator {
aggregate(accumulator, seed) {
return TS.Linq.Extensions.aggregate(this, accumulator, seed);
}
/**
* @description Determines whether all elements of a sequence satisfy a condition.
* @description Extension function.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb548541.aspx | MSDN}
*
* @param {(item: TSource) => boolean} predicate
*
* @returns {boolean}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
all(predicate) {
return TS.Linq.Extensions.all(this, predicate);
}
any(predicate) {
if (!TS.Utils.Assert.isNullOrUndefined(predicate)) {
TS.Utils.checkFunctionParameter("predicate", predicate, "TS.Collections.CollectionBase.any");
} //END if
return TS.Linq.Extensions.any(this, predicate);
}
/**
* @description Computes the average of a sequence of number values.
* @description Extension function.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb354760.aspx | MSDN}
*
* @returns {number}
*
* @throws {TS.InvalidOperationException}
*/
average() {
try {
return TS.Linq.Extensions.average(this);
}
catch (ex) {
throw new TS.InvalidOperationException("The operation 'average' failed on the current collection. See the inner exception for further details.", ex);
}
}
/**
* @description Concatenates two sequences.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb302894.aspx | MSDN}
*
* @param {Iterable<TSource>} secondEnumerator
*
* @returns { TS.Linq.Enumerator<T>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}.
*/
concat(secondEnumerator) {
return TS.Linq.Extensions.concat(this, secondEnumerator);
}
contains(element, equalityComparer) {
return TS.Linq.Extensions.contains(this, element, equalityComparer);
}
count(predicate) {
return TS.Linq.Extensions.count(this, predicate);
}
/**
* @description This function returns an endless number of elements from the underlying sequence by running over
* that sequence in cycles. The function enumerates the elements of the base sequence from the start to then end
* and starts over with the first element as soon as the last element is reached. This function will never run
* out of data. There is one exception of that rule. If the underlying sequence is an empty sequence, the cycle
* function will never give a result.
*
* Attention:
* Use this function with a subsequent call to 'take' to limit the output or you will run out of memory.
* @description Extension function.
* @description Deferred execution.
*
* @returns {TS.Linq.Enumerator<T>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
cycle() {
return TS.Linq.Extensions.cycle(this);
}
/**
* @description Returns the elements of an enumerator, or a default valued singleton collection if the sequence is
* empty. That function differs from the .NET counterpart in that way that is has a 'defaultConstructorOrValue'
* in the signature. That argument is needed because javascript doesn't offer reflections or a type system which
* you can rely on at runtime. Hence there is no way to tell which constructor to use for the default when you
* are dealing with a complex type or which default value to use when you are dealing with a primitive type. The
* only way to make sure that you get the right type at runtime is to place the default constructor or value in
* the parameter list of that function.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/system.linq.enumerable.defaultifempty.aspx | MSDN}
*
* @param { new (): T; } | T) defaultConstructorOrValue
*
* @retuns {TS.Linq.Enumerator<T>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
*/
defaultIfEmpty(defaultConstructorOrValue) {
return TS.Linq.Extensions.defaultIfEmpty(this, defaultConstructorOrValue);
}
distinct(equalityComparer) {
return TS.Linq.Extensions.distinct(this, equalityComparer);
}
/**
* @description Returns the element at a specified index in a sequence.
* @description Extension function.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb299233(v=vs.110).aspx | MSDN}
*
* @param {number} index
*
* @retuns {T}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.IndexOutOfRangeException}
* @throws {TS.InvalidTypeException}
*/
elementAt(index) {
return TS.Linq.Extensions.elementAt(this, index);
}
/**
* @description Returns the element at a specified index in a sequence or a default value
* if the index is out of the range of the sequence.
* That function differs from the .NET counterpart in that way that is has a 'defaultConstructorOrValue' in the
* signature. That argument is needed because javascript doesn't offer reflections or a type system which you
* can rely on at runtime. Hence there is no way to tell which constructor to use for the default when you are
* dealing with a complex type or which default value to use when you are dealing with a primitive type. The only
* way to make sure that you get the right type at runtime is to place the default constructor or value in the
* parameter list of that function.
* @description Extension function.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb494386(v=vs.110).aspx | MSDN}
*
* @param {number} index
* @param {{ new (): T; } | T} defaultConstructorOrValue
*
* @retuns {T}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
elementAtOrDefault(index, defaultConstructorOrValue) {
return TS.Linq.Extensions.elementAtOrDefault(this, index, defaultConstructorOrValue);
}
except(secondEnumerator, equalityComparer) {
return TS.Linq.Extensions.except(this, secondEnumerator, equalityComparer);
}
first(predicate) {
return TS.Linq.Extensions.first(this, predicate);
}
firstOrDefault(defaultConstructorOrValue, predicate) {
return TS.Linq.Extensions.firstOrDefault(this, defaultConstructorOrValue, predicate);
}
/**
* @description Performs the specified action on each element of the underlying sequence. This function is not a
* Linq function. I implemented this extension for your convenience. Without that function you had to call
* 'toArray' first before you could use the array method for each. Please read the article below from
* 'Eric Lippert's' blog to make sure that you understand all the implications of this extension function.
* @description Extension function.
* @description Immediate execution.
*
* @see {@link http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx | MSDN}
*
* @param {Iterable<TSource>} enumerator
* @param {(item: TSource) => void } action
*
* @retuns {Iterable<TSource>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
forEach(action) {
return TS.Linq.Extensions.forEach(this, action);
}
groupBy(keySelector, equalityComparer, elementSelector) {
return TS.Linq.Extensions.groupBy(this, keySelector, equalityComparer, elementSelector);
}
groupJoin(innerEnumerator, outerKeySelector, innerKeySelector, resultSelector, equalityComparer) {
return TS.Linq.Extensions.groupJoin(this, innerEnumerator, outerKeySelector, innerKeySelector, resultSelector, equalityComparer);
}
intersect(secondEnumerator, equalityComparer) {
return TS.Linq.Extensions.intersect(this, secondEnumerator, equalityComparer);
}
/**
* @description Correlates the elements of two sequences based on matching keys.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb534675(v=vs.110).aspx | MSDN}
*
* @param {Iterable<T>} innerEnumerator
* @param {(outerItem: T) => TKey} outerKeySelector
* @param {(innerItem: TInner) => TKey} innerKeySelector
* @param {(outerItem: T, innerItem: TInner) => TResult} resultSelector
*
* @retuns {TS.Linq.Enumerator<TResult>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
join(innerEnumerator, outerKeySelector, innerKeySelector, resultSelector) {
return TS.Linq.Extensions.join(this, innerEnumerator, outerKeySelector, innerKeySelector, resultSelector);
}
/**
* @description Returns the last element of a sequence. Returns the last element of a sequence that satisfies the
* predicate function if specified.
* @description Extension function.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb549138.aspx | MSDN}
*
* @param {(item: T) => boolean} predicate
*
* @retuns {T}
*
* @throws {TS.InvalidTypeException}
* @throws {TS.InvalidOperationException}
*/
last(predicate) {
return TS.Linq.Extensions.last(this, predicate);
}
lastOrDefault(defaultConstructorOrValue, predicate) {
return TS.Linq.Extensions.lastOrDefault(this, defaultConstructorOrValue, predicate);
}
/**
* @description Returns the maximum value in a sequence of values.
* @description Extension function.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/system.linq.enumerable.max.aspx | MSDN}
*
* @retuns {number}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
* @throws {TS.Linq.EmptyEnumerableException}
*/
max() {
return TS.Linq.Extensions.max(this);
}
/**
* @description Returns the minimum value in a sequence of values.
* @description Extension function.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/system.linq.enumerable.min.aspx | MSDN}
*
* @retuns {number}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
* @throws {TS.Linq.EmptyEnumerableException}
*/
min() {
return TS.Linq.Extensions.min(this);
}
orderBy(keySelector, comparer) {
return TS.Linq.Extensions.orderBy(this, keySelector, comparer);
}
orderByDescending(keySelector, comparer) {
return TS.Linq.Extensions.orderByDescending(this, keySelector, comparer);
}
/**
* @description Returns random elements from the base enumeration. This function is not a Linq function. The
* function uses a generator to select the current random element. For that reason the function will return as
* much elements as required, regardless how much elements the underlying sequence holds.
*
* The function throws a 'TS.Linq.EmptyEnumeratorException' If the underlying sequence is empty.
*
* Attention:
* Limit the number of returned elements by calling a 'take' operator or some other limiting operator. Otherwise
* you will run out of memory.
* @description Extension function.
* @description Deferred execution.
*
* @returns {TS.Linq.Enumerator<TSource>}
*
* @throws {TS.Linq.EmptyEnumeratorException}
*/
random() {
return TS.Linq.Extensions.random(this);
}
//Not implemented
//pubic range(start: number, count: number): TS.Linq.Enumerator<Number>
//Not implemented
//public repeat(element: TSource, count: number)
/**
* @description Inverts the order of the elements in a sequence.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb358497(v=vs.110).aspx | MSDN}
*
* @retuns {TS.Linq.Enumerator<T>}
*
*/
reverse() {
return TS.Linq.Extensions.reverse(this);
}
/**
* @description Projects each element of a sequence into a new form.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/system.linq.enumerable.select(v=vs.110).aspx | MSDN}
*
* @param {(item: T) => TResult} selector
*
* @retuns {TS.Linq.Enumerator<TResult>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
* @throws {TS.Linq.SelectorException}
*/
select(selector) {
return TS.Linq.Extensions.select(this, selector);
}
/**
* @description Projects each element of a sequence to an Iterable<TSource> and flattens the resulting sequences
* into one sequence
* @description Extension function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/system.linq.enumerable.selectmany(v=vs.110).aspx | MSDN}
*
* @param {(item: T) => TResult} selector
*
* @retuns {TS.Linq.Enumerator<TResult>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
* @throws {TS.Linq.SelectorException}
*/
selectMany(selector) {
return TS.Linq.Extensions.selectMany(this, selector);
}
sequenceEqual(secondEnumerator, equalityComparer) {
return TS.Linq.Extensions.sequenceEqual(this, secondEnumerator, equalityComparer);
}
/**
* @description Creates and returns a new enumerator which holds exact the same elements as the input enumerator
* but in randomized order.
*
* This function is not a Linq function.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link http://www.dotnetperls.com/fisher-yates-shuffle}
*
* @param {Iterable<TSource>} enumerator
*
* @retuns {TS.Linq.Enumerator<TSource>}
*/
shuffle() {
return TS.Linq.Extensions.shuffle(this);
}
single(predicate) {
return TS.Linq.Extensions.single(this, predicate);
}
singleOrDefault(defaultConstructorOrValue, predicate) {
return TS.Linq.Extensions.singleOrDefault(this, defaultConstructorOrValue, predicate);
}
/**
* @description Bypasses a specified number of elements in a sequence and returns the remaining elements.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb358985.aspx | MSDN}
*
* @paream {number} count
*
* @retuns {TS.Linq.Enumerator<T>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
skip(count) {
return TS.Linq.Extensions.skip(this, count);
}
/**
* @description Bypasses elements in a sequence as long as a specified condition is true and then returns the
* remaining elements.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/system.linq.enumerable.skipwhile.aspx | MSDN}
*
* @param {(item: T) => boolean} predicate
*
* @returns {TS.Linq.Enumerator<Te>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
skipWhile(predicate) {
return TS.Linq.Extensions.skipWhile(this, predicate);
}
/**
* @description Computes the sum of a sequence of numeric values.
* @description Extension function.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/system.linq.enumerable.sum.aspx | MSDN}
*
* @retuns {number}
*
* @throws {TS.InvalidTypeException}
* @throws {TS.Linq.EmptyEnumeratorException}
* @throws {TS.OverflowException}
*/
sum() {
return TS.Linq.Extensions.sum(this);
}
/**
* @description Returns a specified number of contiguous elements from the start of a sequence.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link http://msdn.microsoft.com/en-us/library/bb503062.aspx | MSDN}
*
* @returns {TS.Linq.Enumerator<T>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
take(count) {
return TS.Linq.Extensions.take(this, count);
}
/**
* @description Returns elements from a sequence as long as a specified condition is true.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb534804.aspx | MSDN}
*
* @param { (item: T) => boolean} predicate
*
* @returns {TS.Linq.Enumerator<T>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
takeWhile(predicate) {
return TS.Linq.Extensions.takeWhile(this, predicate);
}
//***********************************************************************
// Function 'thenBy' and 'thenByDescending' which would appear here are
// only available on 'TS.Linq.OrderedEnumerator' objects for obvious
// reasons.
//***********************************************************************
/**
* @description Creates an Array<T> from the list.
* @description Extension function.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb298736 | MSDN}
*
* @returns {Array<T>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
toArray() {
return TS.Linq.Extensions.toArray(this);
}
/**
* @description Creates a Dictionary<TKey, TSource> from an Iterable<TSource> according to a specified key
* selector function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb549277(v=vs.110).aspx | MSDN }
*
* @param {Iterable<TSource>} enumerator
* @param { (item: TSource) => TKey} keySelector
*
* @returns {TS.Collections.Dictionary<TKey, TSource>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.ArgumentUndefinedException}
* @throws {TS.Collections.DuplicateKeyException}
* @throws {TS.InvalidTypeException}
* @throws {TS.InvalidOperationException}
*/
toDictionary(keySelector) {
return TS.Linq.Extensions.toDictionary(this, keySelector);
}
/**
* @description Creates a List<TSource> from an Iterable<TSource>. The list will have the 'allowNull' flag set to
* true.
* @description Extension function.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb342261(v=vs.110).aspx | MSDN}
*
* @returns {TS.Collections.List<T>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
toList() {
return TS.Linq.Extensions.toList(this);
}
union(secondEnumerator, equalityComparer) {
return TS.Linq.Extensions.union(this, secondEnumerator, equalityComparer);
}
/**
* @description Filters a sequence of values based on a predicate.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/system.linq.enumerable.where.aspx | MSDN}
*
* @param {(item: T) => boolean} predicate
*
* @retuns {TS.Linq.Enumerator<T>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
where(predicate) {
return TS.Linq.Extensions.where(this, predicate);
}
/**
* @description Applies a specified function to the corresponding elements of two sequences, producing a sequence
* of the results.
* @description Extension function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/dd267698(v=vs.110).aspx | MSDN}
*
* @param {Iterable<TSecond>} secondEnum
* @param {(first: TFirst, second: TSecond) => TResult} func
*
* @retuns {TS.Linq.Enumerator<TResult>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
zip(secondEnum, func) {
return TS.Linq.Extensions.zip(this, secondEnum, func);
}
}
Linq.BaseEnumerator = BaseEnumerator; //END class
})(Linq = TS.Linq || (TS.Linq = {})); //END namespace
})(TS || (TS = {})); //END namespace
/// <reference path="../_references.ts" />
var TS;
(function (TS) {
var Linq;
(function (Linq) {
/**
* @class TS.Linq.Enumerator<T>
*
* @descripton The 'TS.Linq.Enumerator' class is used by the Linq extension functions. The Enumerator class is the
* TypeScript equivalent to the ES6 Iteration protocols.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols | MDN}
*
* @extends {TS.Linq.BaseEnumerator<T>}
*/
class Enumerator extends TS.Linq.BaseEnumerator {
constructor(sourceOrGenerator, predicate) {
super();
/**
* @private
*/
this.genFunc = null;
this.genFunc = null;
TS.Utils.checkParameter("source or generator", sourceOrGenerator, "TS.Linq.Enumerator constructor");
if (TS.Utils.Assert.isFunction(sourceOrGenerator)) {
if (TS.Utils.Assert.isGenerator(sourceOrGenerator)) {
this.genFunc = sourceOrGenerator;
}
else {
throw new TS.InvalidInvocationException("The constructor of 'TS.Linq.Enumerator' requires a valid generator function.");
}
}
else if (TS.Utils.Assert.isIterable(sourceOrGenerator) || TS.Utils.Assert.isArrayLike(sourceOrGenerator)) {
if (!TS.Utils.Assert.isNullOrUndefined(predicate)) {
if (!TS.Utils.Assert.isFunction(predicate)) {
throw new TS.InvalidInvocationException("The constructor of 'TS.Linq.Enumerator' requires a valid selector argument.");
}
}
else {
predicate = (item) => true;
}
this.genFunc = function* () {
for (let item of sourceOrGenerator) {
if (predicate(item)) {
yield item;
}
}
};
}
else {
throw new TS.InvalidInvocationException("The constructor of 'TS.Linq.Enumerator' requires a valid generator function in argument 'generator' or an iterable or array like type in argument 'source'.");
}
}
/**
* @description Property which returns an empty 'Enumerator'.
*
* @get {Enumerator<any>}
*/
static get Empty() {
return new Enumerator(new Array());
}
/**
* @description This function returns the Iterator of the current Enumerator as soon as an iteration starts. E.g.
* when a 'for ( let x of enumerator)' is called.
*
* @implements {BaseEnumerator<T>}
*
* @returns {IterableIterator<T>}, An instance of the iterator type.
*/
[Symbol.iterator]() {
return new Generator(this.genFunc);
}
}
Linq.Enumerator = Enumerator; //END class Enumerator
//*************************************************************************
// Private class: Generator
//*************************************************************************
/**
* @class Generator<T>
*
* @internal
*/
class Generator {
/**
* @constructor
*
* @param {genFunc: () => IterableIterator<T>} genFunc
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
constructor(genFunc) {
this.genFunc = null;
this.innerIterator = null;
this.initalized = false;
this.initalized = false;
this.innerIterator = null;
TS.Utils.checkParameter("genFunc", genFunc, "TS.Linq.Generator");
TS.Utils.checkFunctionParameter("genFunc", genFunc, "TS.Linq.Generator");
this.genFunc = genFunc;
}
/**
* @description This function returns a 'IteratorResult<T>' result for each invocation.
*
* @returns {IteratorResult<T>}
*/
next() {
if (!this.initalized) {
this.innerIterator = this.genFunc();
this.initalized = true;
}
return this.innerIterator.next();
}
}
//END class
})(Linq = TS.Linq || (TS.Linq = {})); //END namespace
})(TS || (TS = {})); //END namespace
/// <reference path="../_references.ts" />
var TS;
(function (TS) {
var Linq;
(function (Linq) {
var Extensions;
(function (Extensions) {
function aggregate(enumerator, accumulator, seed) {
let resultValue;
let isEmpty = true;
let isFirst = true;
let useSeed = false;
TS.Utils.checkIterableParameter("enumerator", enumerator, "TS.Linq.Extensions.aggregate");
TS.Utils.checkFunctionParameter("accumulator", accumulator, "TS.Linq.Extensions.aggregate");
useSeed = !TS.Utils.Assert.isNullOrUndefined(seed);
for (let current of enumerator) {
if (isFirst) {
if (useSeed) {
resultValue = accumulator(seed, current);
}
else {
resultValue = current;
}
isFirst = false;
}
else {
resultValue = accumulator(resultValue, current);
}
}
if (isFirst) {
if (useSeed) {
resultValue = seed;
}
else {
throw new TS.Linq.EmptyEnumeratorException(enumerator, "The argument 'enumerator' must not be an empty enumerator in function 'TS.Linq.Extensions.aggregate'.");
}
}
return resultValue;
}
Extensions.aggregate = aggregate;
/**
* @description Determines whether all elements of a sequence satisfy a condition.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb548541.aspx | MSDN}
*
* @param {Iterable<TSource>} enumerator
* @param {(item: TSource) => boolean} predicate
*
* @returns {boolean}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
function all(enumerator, predicate) {
let resultValue = true;
TS.Utils.checkIterableParameter("enumerator", enumerator, "TS.Linq.Extensions.all");
TS.Utils.checkFunctionParameter("predicate", predicate, "TS.Linq.Extensions.all");
for (let item of enumerator) {
if (!predicate(item)) {
return false;
}
}
return true;
}
Extensions.all = all;
function any(enumerator, predicate) {
TS.Utils.checkIterableParameter("enumerator", enumerator, "TS.Linq.Extensions.any");
if (!TS.Utils.Assert.isNullOrUndefined(predicate)) {
TS.Utils.checkFunctionParameter("predicate", predicate, "TS.Linq.Extensions.any");
} //END if
else {
predicate = item => true;
} //END else
for (let item of enumerator) {
if (predicate(item)) {
return true;
}
}
return false;
}
Extensions.any = any;
/**
* @description Computes the average of a sequence of number values.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb354760.aspx | MSDN}
*
* @param {Iterable<number>} enumerator
*
* @returns {number} The average of all items in the enumerable.
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
* @throws {TS.Linq.EmptyEnumeratorException}
* @throws {TS.OverflowException}
*/
function average(enumerator) {
TS.Utils.checkIterableParameter("enumerator", enumerator, "TS.Linq.Extensions.average");
let sum = 0;
let count = 0;
for (let item of enumerator) {
if (!TS.Utils.Assert.isNumberValue(item)) {
throw new TS.InvalidTypeException("enumerator", enumerator, "Enumerator is not a valid number enumerator in function 'TS.Linq.Extensions.average'.");
}
sum += item;
count++;
}
if (!TS.Utils.Assert.isNumberValue(sum)) {
throw new TS.InvalidTypeException("enumerator", enumerator, "Enumerator is not a valid number enumerator in function 'TS.Linq.Extensions.average'.");
}
if (TS.Utils.Assert.isInfiniteNumber(sum)) {
throw new TS.OverflowException("An arithmetic overflow occurred during the execution of 'TS.Extensions.average'.");
}
if (count == 0) {
throw new TS.Linq.EmptyEnumeratorException(enumerator, "The argument 'enumerator' must not be an empty enumerator in function 'TS.Linq.Extensions.average'.");
}
return sum / count;
}
Extensions.average = average;
/**
* @description Concatenates two sequences.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb302894.aspx | MSDN}
*
* @param {Iterable<TSource>} firstEnumerator
* @param {Iterable<TSource>} secondEnumerator
*
* @returns { TS.Linq.Enumerator<TSource>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}.
*/
function concat(firstEnumerator, secondEnumerator) {
TS.Utils.checkIterableParameter("firstEnumerator", firstEnumerator, "TS.Linq.Extensions.concat");
TS.Utils.checkIterableParameter("secondEnumerator", secondEnumerator, "TS.Linq.Extensions.concat");
let arr;
let resultEnumerator;
let generatorFunc = function* () {
let firstIter = firstEnumerator[Symbol.iterator]();
let secondIter = secondEnumerator[Symbol.iterator]();
let result;
do {
result = firstIter.next();
if (!result.done) {
yield result.value;
}
} while (!result.done);
do {
result = secondIter.next();
if (!result.done) {
yield result.value;
}
} while (!result.done);
};
return new TS.Linq.Enumerator(generatorFunc);
}
Extensions.concat = concat;
function contains(enumerator, element, equalityComparer) {
TS.Utils.checkIterableParameter("enumerator", enumerator, "TS.Linq.Extensions.contains");
TS.Utils.checkParameter("element", element, "TS.Linq.Extensions.contains");
if (!TS.Utils.Assert.isNullOrUndefined(equalityComparer)) {
TS.Utils.checkFunctionParameter("equalityComparer", equalityComparer, "TS.Linq.Extensions.contains");
} //END if
else {
equalityComparer = (first, second) => first === second;
} //END else
for (let item of enumerator) {
if (equalityComparer(item, element)) {
return true;
}
}
return false;
}
Extensions.contains = contains;
function count(enumerator, predicate) {
TS.Utils.checkIterableParameter("enumerator", enumerator, "TS.Linq.Extensions.count");
if (!TS.Utils.Assert.isNullOrUndefined(predicate)) {
TS.Utils.checkFunctionParameter("predicate", predicate, "TS.Linq.Extensions.count");
} //END if
else {
predicate = item => true;
} //END else
let count = 0;
for (let item of enumerator) {
if (predicate(item)) {
count++;
}
}
return count;
}
Extensions.count = count;
/**
* @description This function returns an endless number of elements from the underlying sequence by running over
* the that sequence in cycles. The function enumerates the elements of the base sequence from the start to then
* end and starts over with the first element. This function will never run out of elements. There is one
* exception of that rule. If the underlying sequence is an empty sequence, the cycle function will never give a
* result.
*
* Attention:
* Limit the number of returned elements by calling a 'take' operator or some other limiting operator.
* Otherwise you will run out of memory.
*
* This function is not a Linq function.
* @description Deferred execution.
*
* @param {Iterable<TSource>} enumerator
*
* @returns {TS.Linq.Enumerator<TSource>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
function cycle(enumerator) {
TS.Utils.checkIterableParameter("enumerator", enumerator, "TS.Linq.Extensions.cycle");
let generatorFunction = function* () {
if (TS.Linq.Extensions.count(enumerator) == 0) {
return null;
}
while (true) {
for (let item of enumerator) {
yield item;
}
}
};
return new TS.Linq.Enumerator(generatorFunction);
}
Extensions.cycle = cycle;
/**
* @description Returns the elements of an enumerator, or a default valued singleton collection if the sequence is
* empty. That function differs from the .NET counterpart in that way that is has a 'defaultConstructorOrValue'
* in the signature. That argument is needed because javascript doesn't offer reflection or a type system which
* you can rely on at runtime. Hence there is no way to tell which constructor to use for the default when you
* are dealing with a complex type or which default value to use when you are dealing with a primitive type. The
* only way to make sure that you get the right type at runtime is to place the default constructor or value in
* the parameter list of that function.
* @description Deferred execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/system.linq.enumerable.defaultifempty.aspx | MSDN}
*
* @param {Iterable<TSource>} enumerator
* @param { new (): TSource; } | T) defaultConstructorOrValue
*
* @retuns {TS.Linq.Enumerator<TSource>}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.InvalidTypeException}
*/
function defaultIfEmpty(enumerator, defaultConstructorOrValue) {
TS.Utils.checkIterableParameter("enumerator", enumerator, "TS.Linq.Extensions.defaultIfEmpty");
TS.Utils.checkParameter("defaultConstructorOrValue", defaultConstructorOrValue, "TS.Linq.Extensions.defaultIfEmpty");
let generatorFunction = function* () {
let hasElements = false;
for (let item of enumerator) {
hasElements = true;
yield item;
}
if (!hasElements) {
if (TS.Utils.Assert.isConstructor(defaultConstructorOrValue)) {
yield new defaultConstructorOrValue();
}
else {
yield defaultConstructorOrValue;
}
}
};
return new TS.Linq.Enumerator(generatorFunction);
}
Extensions.defaultIfEmpty = defaultIfEmpty;
function distinct(enumerator, equalityComparer) {
TS.Utils.checkIterableParameter("enumerator", enumerator, "TS.Linq.Extensions.distinct");
if (!TS.Utils.Assert.isNullOrUndefined(equalityComparer)) {
TS.Utils.checkFunctionParameter("equalityComparer", equalityComparer, "TS.Linq.Extensions.distinct");
} //END if
else {
equalityComparer = (first, second) => first === second;
} //END else
let generatorFunction = function* () {
let tempArray = new Array();
for (let item of enumerator) {
if (tempArray.find((value, index, arr) => { return equalityComparer(value, item); }) == undefined) {
tempArray.push(item);
yield item;
}
}
};
return new TS.Linq.Enumerator(generatorFunction);
}
Extensions.distinct = distinct;
/**
* @description Returns the element at a specified index in a sequence.
* @description Immediate execution.
*
* @see {@link https://msdn.microsoft.com/en-us/library/bb299233(v=vs.110).aspx | MSDN}
*
* @param {Iterable<TSource>} enumerator
* @param {number} index
*
* @retuns {TSource}
*
* @throws {TS.ArgumentNullOrUndefinedException}
* @throws {TS.IndexOutOfRangeException}
* @throws {TS.InvalidTypeException}
*/
function elementAt(enumerator, index) {
TS.Utils.checkIterableParameter("enumerator", enumerator, "TS.Linq.Extensions.elementAt");
TS.Utils.checkUIntNumberParameter("index", index, "TS.Linq.Extensions.elementAt");
let temArray = TS.Linq.Extensions.toArray(enumerator);
if (index < temArray.length) {