-
Notifications
You must be signed in to change notification settings - Fork 9
/
types.ts
642 lines (630 loc) · 16.9 KB
/
types.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
export type MapFn<TItem, TResult> = (item: TItem) => TResult;
export type PredicateFn<TItem> = (item: TItem) => boolean;
export type CallbackFn<TItem> = (item: TItem) => void;
export type KeySelectorFn<TItem, TKey> = (item: TItem) => TKey;
export type ReduceCallbackFn<TPrevious, TCurrent> = (
previousValue: TPrevious,
currentValue: TCurrent
) => TPrevious;
export type ComparerFn<TItem> = (a: TItem, b: TItem) => number;
export type IteratorCreatorFn<TResult> = () => Iterator<TResult>;
export type ComparePredicate<TItem> = (a: TItem, b: TItem) => boolean;
export interface Grouping<TKey, TElement> {
key: TKey;
items: TElement[];
}
export interface StringKeyedObject<T> {
[index: string]: T;
}
export interface NumberKeyedObject<T> {
[index: number]: T;
}
/**
* A sequence of items
*/
export interface Sequence<TItem> extends Iterable<TItem> {
/**
* Returns a new sequence that contains the items in the current sequence
* and items from the given iterable.
*
* @example
* ```typescript
* // Returns sequence with values 1, 2, 3, 4
* from([1, 2]).concat([3, 4]);
* ```
*/
concat<TOther>(...others: Iterable<TOther>[]): Sequence<TItem | TOther>;
/**
* Returns unique values in the sequence. Uniqueness is checked using
* the '===' operator.
*
* @example
* ```typescript
* // Returns a sequence with the values 4, 5
* from([4, 4, 5, 4]).distinct();
* ```
*/
distinct(): Sequence<TItem>;
/**
* Checks that all items in the sequence pass the test implemented by the
* provided function.
*
* @example
* ```typescript
* // Returns false
* from([-1, 4, 5, 6]).every(x => x >= 0);
* ```
*/
every(predicate?: PredicateFn<TItem>): boolean;
/**
* Returns a new sequence where items are filtered out for which the
* predicate function returns a falsy value.
*
* @example
* ```typescript
* // Returns a squence with the value -1
* from([-1, 4, 5, 6]).filter(x => x < 0);
* ```
*/
filter(predicate: PredicateFn<TItem>): Sequence<TItem>;
/**
* Returns the value of the first element in the sequence that satisfies the
* provided testing function. Otherwise undefined is returned.
*
* @example
* ```typescript
* // Returns 4
* from([2, 4, 6]).find(x => x === 4);
* ```
*/
find(predicate: PredicateFn<TItem>): TItem | undefined;
/**
* Returns the first element of the sequence or undefined if
* the sequence is empty.
*
* @example
* ```typescript
* // Returns 1
* from([1, 3, 5]).first();
* ```
*/
first(): TItem | undefined;
/**
* First maps each element of the sequence using the given mapping function,
* then flattens the result into a new sequence.
*
* @example
* ```typescript
* // Returns [1, 2, 3, 4, 5, 6]
* from([1, 3, 5]).flatMap(x => [x, x + 1]).toArray();
* ```
*/
flatMap<TResultItem>(
mapperFn: MapFn<TItem, TResultItem[]>
): Sequence<TResultItem>;
/**
* Calls the given callback function with each item in the sequence.
*
* @example
* ```typescript
* // Logs 1, 2 and 3 to console
* from([1, 2, 3]).forEach(i => console.log(i));
* ```
*/
forEach(callback: CallbackFn<TItem>): void;
/**
* Groups the items in the sequence using the given item's key
*
* @param key Key to be used for the grouping
*
* @example
* ```typescript
* from([
* { name: "John", gender: "M" },
* { name: "Mike", gender: "M" },
* { name: "Lisa", gender: "F" },
* { name: "Mary", gender: "F" }
* ]).groupBy("gender");
* // Returns a sequence with two groupings:
* // {
* // key: "M",
* // items: [
* // { name: "John", gender: "M" },
* // { name: "Mike", gender: "M" }
* // ]
* // },
* // {
* // key: "F",
* // items: [
* // { name: "Lisa", gender: "F" },
* // { name: "Mary", gender: "F" }
* // ]
* // }
* ```
*/
groupBy<TKey extends keyof TItem>(
key: TKey
): Sequence<Grouping<TItem[TKey], TItem>>;
/**
* Groups the items in the sequence by keys returned by the given
* keySelector function.
*
* @param keySelector A function to extract the key for each element.
*
* @example
* ```typescript
* from([
* { name: "John", gender: "M" },
* { name: "Mike", gender: "M" },
* { name: "Lisa", gender: "F" },
* { name: "Mary", gender: "F" }
* ]).groupBy(user => user.gender);
* // Returns a sequence with two groupings:
* // {
* // key: "M",
* // items: [
* // { name: "John", gender: "M" },
* // { name: "Mike", gender: "M" }
* // ]
* // },
* // {
* // key: "F",
* // items: [
* // { name: "Lisa", gender: "F" },
* // { name: "Mary", gender: "F" }
* // ]
* // }
* ```
*/
groupBy<TKey>(
keySelector: KeySelectorFn<TItem, TKey>
): Sequence<Grouping<TKey, TItem>>;
/**
* Groups the items of a sequence according to a specified key and
* projects the elements for each group by using a specified function.
*
* @param key Key to be used for the grouping
* @param elementSelector A function to map each source element to an element in an Grouping<TKey,TElement>.
*
* @example
* ```typescript
* from([
* { name: "John", gender: "M" },
* { name: "Mike", gender: "M" },
* { name: "Lisa", gender: "F" },
* { name: "Mary", gender: "F" }
* ]).groupBy("gender", user => user.name);
* // Returns a sequence with two groupings:
* // {
* // key: "M",
* // items: ["John", "Mike"]
* // },
* // {
* // key: "F",
* // items: ["Lisa", "Mary"]
* // }
* ```
*/
groupBy<TKey extends keyof TItem, TElement>(
key: TKey,
elementSelector: MapFn<TItem, TElement>
): Sequence<Grouping<TItem[TKey], TItem>>;
/**
* 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 an Grouping<TKey,TElement>.
*
* @example
* ```typescript
* from([
* { name: "John", gender: "M" },
* { name: "Mike", gender: "M" },
* { name: "Lisa", gender: "F" },
* { name: "Mary", gender: "F" }
* ]).groupBy("gender", user => user.name);
* // Returns a sequence with two groupings:
* // {
* // key: "M",
* // items: ["John", "Mike"]
* // },
* // {
* // key: "F",
* // items: ["Lisa", "Mary"]
* // }
* ```
*/
groupBy<TKey, TElement>(
keySelector: KeySelectorFn<TItem, TKey>,
elementSelector: MapFn<TItem, TElement>
): Sequence<Grouping<TKey, TElement>>;
/**
* Determines whether the sequence includes the given element,
* returning true or false as appropriate. The check is done
* using '==='.
*
* @example
* ```typescript
* // Returns true
* from([1, 2, 3]).includes(3);
* ```
*/
includes(searchItem: TItem): boolean;
/**
* Returns true if the sequence is empty, false otherwise.
*
* @example
* ```typescript
* // Returns true
* from([]).isEmpty();
* ```
*/
isEmpty(): boolean;
/**
* Returns the first element of the sequence or undefined if
* the sequence is empty.
*
* @example
* ```typescript
* // Returns 5
* from([1, 3, 5]).last();
* ```
*/
last(): TItem | undefined;
/**
* Maps the sequence to a new sequence where each item is converted
* to a new value using the given mapper function.
*
* @example
* ```typescript
* // Returns [2, 4, 6]
* from([1, 2, 3]).map(x => x * 2);
* ```
*/
map<TResultItem>(mapFn: MapFn<TItem, TResultItem>): Sequence<TResultItem>;
/**
* Maps each item in the sequence to an object composed of the picked
* object properties.
*
* @example
* ```typescript
* const users = [
* { id: 1, name: "John", age: 31, active: true },
* { id: 2, name: "Jane", age: 32, active: false },
* { id: 3, name: "Luke", age: 33, active: false },
* { id: 4, name: "Mary", age: 34, active: true },
* ];
*
* // Returns a Sequence of { name: 'John' }, { name: 'Jane' }, { name: 'Luke' }, { name: 'Mary' }
* from(users).pick("name");
* ```
*/
pick<TKeys extends keyof TItem>(
...keys: TKeys[]
): Sequence<{ [P in TKeys]: TItem[P] }>;
/**
* This method yields the elements from the provided items first, followed by the items in the
* underlying sequence.
*
* @param items The provided set of items that should be in the prepended to the Sequence.
*
* @example
* ```ts
* // returns [4, 5, 6, 1, 2, 3]
* from([1, 2, 3])
* .prepend([4, 5, 6])
* .toArray();
* ```
*/
prepend(...items: Iterable<TItem>[]): Sequence<TItem>;
/**
* Executes a reducer function on each item in the sequence resulting
* in a single output value.
*
* @example
* ```typescript
* // Returns a 15
* from([1, 2, 3, 4, 5]).reduce((x, acc) => acc+x, 0)
* ```
*/
reduce<TResult>(
callback: ReduceCallbackFn<TResult, TItem>,
accumulator: TResult
): TResult;
/**
* Reverses the order of the items in the sequence
*
* @example
* ```typescript
* // Returns [3, 2, 1]
* from([1, 2, 3]).reverse().toArray();
* ```
*/
reverse(): Sequence<TItem>;
/**
* Skips the first N items in the sequence
*
* @example
* ```typescript
* // Returns [3, 4]
* from([1, 2, 3, 4]).skip(2);
* ```
*/
skip(howMany: number): Sequence<TItem>;
/**
* 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.
*
* @example
* ```ts
* // Returns [3, 4, 5]
* from([1, 2, 3, 4, 5])
* .skipWhile(i => i < 3)
* .toArray();
* ```
*/
skipWhile(predicate: PredicateFn<TItem>): Sequence<TItem>;
/**
* Returns true if sequence contains an element for which the given
* predicate returns a truthy value.
*
* @example
* ```typescript
* // Returns true
* from([1, 2, 3]).some(x => x === 1)
* ```
*/
some(predicate?: PredicateFn<TItem>): boolean;
/**
* Sorts the elements of a sequence in ascending order according to a key
* by using a specified comparer.
*
* @param keySelector A function to extract a key from an element.
* @param comparer A function to compare the keys
*/
/**
* @example
* ```typescript
* // Returns a Sequence of 1, 2, 3
* from([1, 3, 2]).sortBy()
* ```
*/
sortBy(): OrderedSequence<TItem>;
/**
* @example
* ```typescript
* const users = [
* { id: 2, name: "Jane" },
* { id: 4, name: "Mary" },
* { id: 1, name: "John" },
* { id: 3, name: "Luke" },
* ];
*
* // Returns a Sequence of
* // { id: 1, name: 'John' },
* // { id: 2, name: 'Jane' },
* // { id: 3, name: 'Luke' },
* // { id: 4, name: 'Mary' }
* from(users).sortBy(user => user.id)
* ```
*/
sortBy<TKey>(
keySelector: KeySelectorFn<TItem, TKey>,
comparer?: ComparerFn<TKey>
): OrderedSequence<TItem>;
/**
* Sorts the elements of a sequence in descending order according to a key
* by using a specified comparer.
*
* @param keySelector A function to extract a key from an element.
* @param comparer A function to compare the keys
*
* @example
* ```typescript
* // Returns a Sequence of 3, 2, 1
* from([1, 3, 2]).sortByDescending()
* ```
*/
sortByDescending<TKey = TItem>(
keySelector?: KeySelectorFn<TItem, TKey>,
comparer?: ComparerFn<TKey>
): OrderedSequence<TItem>;
/**
* Sums the elements in the sequence.
* NOTE! If the sequence is empty, 0 is returned.
*
* @example
* ```typescript
* // Returns 6
* from([1, 2, 3]).sum();
* ```
*/
sum<TItem extends number>(): number;
/**
* Sums the elements in the sequence
* NOTE! If the sequence is empty, 0 is returned.
*
* @example
* ```typescript
* // Returns "abc"
* from(["a", "b", "c"]).sum();
* ```
*/
sum<TItem extends string>(): string;
/**
* Maps the elements in the sequence using the valueSelector and sums them
* together.
* NOTE! If the sequence is empty, 0 is returned.
*
* @param valueSelector A function to select a value from an element.
*
* @example
* ```typescript
* // Returns 2
* from([true, false, true]).sum(x => x ? 1 : 0);
* ```
*/
sum<TResult extends number>(valueSelector: MapFn<TItem, TResult>): number;
/**
* Maps the elements in the sequence using the valueSelector and sums them
* together.
* NOTE! If the sequence is empty, 0 is returned.
*
* @param valueSelector A function to select a value from an element.
*
* @example
* ```typescript
* // Returns "101"
* from([true, false, true]).sum(x => x ? "1" : "0");
* ```
*/
sum<TResult extends string>(valueSelector: MapFn<TItem, TResult>): string;
/**
* Takes the firt N items from the sequence
*
* @example
* ```typescript
* // Returns [1, 2]
* from([1, 2, 3, 4]).take(2);
* ```
*/
take(howMany: number): Sequence<TItem>;
/**
* Returns elements from a sequence as long as a specified condition is true,
* and then skips the remaining elements.
*
* @param predicate A function to test each element for a condition.
*
* @example
* ```ts
* // Returns [1, 2]
* from([1, 2, 3, 4, 5])
* .takeWhile(i => i < 3)
* .toArray();
* ```
*/
takeWhile(predicate: PredicateFn<TItem>): Sequence<TItem>;
/**
* Returns elements from a sequence as long as they don't exist in the specified iterable items.
*
* @param items The provided set of items that should not be in the returned Sequence.
* @param predicate The optional predicate that determines if two TItem items are equal.
*
* @example
* ```ts
* // returns [2, 4, 6]
* from([1, 2, 3, 4, 5, 6])
* .without([1, 3, 5])
* .toArray();
*
* // returns [{ id: 1 }, { id: 3 }]
* from([{ id: 1 }, { id: 2 }, { id: 3 }])
* .without([{ id: 2 }], (a, b) => a.id === b.id)
* .toArray();
* ```
*/
without(
items: Iterable<TItem>,
predicate?: ComparePredicate<TItem>
): Sequence<TItem>;
/**
* Converts the sequence to an array
*
* @example
* ```typescript
* // Return [1, 2, 3]
* from([1, 2, 3]).toArray();
* ```
*/
toArray(): TItem[];
/**
* Converts the sequence to a Map using the given keySelectorFn and
* possible elementSelectorFn.
*
* @example
* ```typescript
* // Returns map with elements:
* // 1 -> { id: 1, name: "John" }
* // 2 -> { id: 2, name: "Jane"}
* const users = [{ id: 1, name: "John" }, { id: 2, name: "Jane"}]
* from(users).toMap(u => u.id);
* ```
*
* @param keySelectorFn
* @param elementSelectorFn
*/
toMap<TKey, TElement = TItem>(
keySelectorFn: MapFn<TItem, TKey>,
elementSelectorFn?: MapFn<TItem, TElement>
): Map<TKey, TElement>;
/**
* Converts the sequence to an object using the given keySelectorFn and
* possible elementSelectorFn.
*
* @example
* ```typescript
* // Returns an object:
* // {
* // "John": { id: 1, name: "John" },
* // "Jane": { id: 2, name: "Jane"}
* // }
* const users = [{ id: 1, name: "John" }, { id: 2, name: "Jane"}]
* from(users).toObject(u => u.name);
* ```
*
* @param keySelectorFn
* @param elementSelectorFn
*/
toObject(keySelectorFn: MapFn<TItem, string>): StringKeyedObject<TItem>;
toObject(keySelectorFn: MapFn<TItem, number>): NumberKeyedObject<TItem>;
toObject<TElement>(
keySelectorFn: MapFn<TItem, string>,
elementSelectorFn: MapFn<TItem, TElement>
): StringKeyedObject<TElement>;
toObject<TElement>(
keySelectorFn: MapFn<TItem, number>,
elementSelectorFn: MapFn<TItem, TElement>
): NumberKeyedObject<TElement>;
/**
* Converts the sequence to a Set
*
* @example
* ```typescript
* // Return a Set with elements 1, 2, 3
* from([1, 1, 2, 3]).toSet();
* ```
*/
toSet(): Set<TItem>;
/**
* Converts the sequence to a string using the given separator
*
* @param separator A string used to separate one element of a sequence from the next in the resulting String. If omitted, the elements are separated with a comma.
*
* @example
* ```typescript
* // Returns a string "1,2,3"
* from([1, 2, 3]).toString();
*
* // Returns a string "1 - 2 - 3"
* from([1, 2, 3]).toString(" - ");
* ```
*/
toString(separator?: string): string;
}
/**
* Ordered sequence of elements
*/
export interface OrderedSequence<TItem> extends Sequence<TItem> {
thenBy<TOtherKey>(
keySelector: KeySelectorFn<TItem, TOtherKey>,
comparer?: ComparerFn<TOtherKey>
): OrderedSequence<TItem>;
thenByDescending<TOtherKey>(
keySelector: KeySelectorFn<TItem, TOtherKey>,
comparer?: ComparerFn<TOtherKey>
): OrderedSequence<TItem>;
}