iterable-linq-utility 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -54,6 +54,54 @@ declare type ComparerFunction<T> = (a: T, b: T) => number;
54
54
 
55
55
  declare type ComparingProps<T> = keyof T | Array<keyof T>;
56
56
 
57
+ /**
58
+ * Counts the values of `iterable`; reads the whole source.
59
+ * @operation `Action`
60
+ * @param iterable - the source `Iterable`
61
+ * @returns the number of values in `iterable`
62
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
63
+ * @example
64
+ * ```ts
65
+ * Functions.count([1, 2, 3]); // 3
66
+ * ```
67
+ * @since 0.5.0
68
+ */
69
+ declare function count<T>(iterable: Iterable<T>): number;
70
+
71
+ /**
72
+ * Counts the values that satisfy `predicate`; reads the whole source.
73
+ * If `predicate` throws, the source is closed and the error propagates.
74
+ * @operation `Action`
75
+ * @param iterable - the source `Iterable`
76
+ * @param predicate - called with each value and its index; `undefined` counts every value
77
+ * @returns the number of values that satisfy `predicate`
78
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if a provided `predicate` is not a function
79
+ * @example
80
+ * ```ts
81
+ * Functions.count([1, 5, 2, 6], v => v > 4); // 2
82
+ * ```
83
+ * @since 0.5.0
84
+ */
85
+ declare function count<T>(iterable: Iterable<T>, predicate: Predicate<T> | undefined): number;
86
+
87
+ /**
88
+ * Lazily yields the first value for each distinct value or selected key, in source order.
89
+ * Keys use `SameValueZero`, like `Set`; original values are preserved.
90
+ * Each iterator stores its own seen keys. If `keySelector` throws, the source is closed and the error propagates.
91
+ * @operation `Transformation`
92
+ * @param iterable - the source `Iterable`
93
+ * @param keySelector - called with every source value and its index; omitted or `undefined` compares values directly
94
+ * @returns a lazy, re-runnable `Iterable` of the first values for each key
95
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if a provided `keySelector` is not a function
96
+ * @example
97
+ * ```ts
98
+ * Array.from(Functions.distinct([3, 1, 3, 2, 1])); // [3, 1, 2]
99
+ * Array.from(Functions.distinct([{ id: 1 }, { id: 1 }, { id: 2 }], v => v.id)); // [{ id: 1 }, { id: 2 }]
100
+ * ```
101
+ * @since 0.5.0
102
+ */
103
+ declare function distinct<T, K>(iterable: Iterable<T>, keySelector?: Mapper<T, K>): Iterable<T>;
104
+
57
105
  /**
58
106
  * Starts a chain with no values.
59
107
  * @returns an empty chain
@@ -77,6 +125,21 @@ export declare function empty<T>(): IIterableLinq<T>;
77
125
  */
78
126
  declare function empty_2<T>(): Iterable<T>;
79
127
 
128
+ /**
129
+ * Tells whether every value satisfies `predicate`; stops and closes the source at the first rejected value.
130
+ * @operation `Action`
131
+ * @param iterable - the source `Iterable`
132
+ * @param predicate - called with each value and its index
133
+ * @returns `true` if every value satisfies `predicate`, or if `iterable` is empty; `false` otherwise
134
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
135
+ * @example
136
+ * ```ts
137
+ * Functions.every([1, 2, 3], v => v > 0); // true
138
+ * ```
139
+ * @since 0.5.0
140
+ */
141
+ declare function every<T>(iterable: Iterable<T>, predicate: Predicate<T>): boolean;
142
+
80
143
  /**
81
144
  * Adds a method to every chain, including the chains created before the call.
82
145
  * Declare the method first by augmenting `IIterableLinq`, then register it once, at application start-up.
@@ -132,6 +195,52 @@ declare function filter<T, S extends T>(iterable: Iterable<T>, predicate: (value
132
195
  */
133
196
  declare function filter<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
134
197
 
198
+ /**
199
+ * Returns the first value accepted by a type guard, narrowing its type; stops and closes the source at the first match.
200
+ * @operation `Action`
201
+ * @param iterable - the source `Iterable`
202
+ * @param predicate - a type guard called with each value and its index
203
+ * @returns the first value accepted by `predicate`, or `undefined` if there is none
204
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
205
+ * @example
206
+ * ```ts
207
+ * const values: (number | string)[] = [1, 'two', 3];
208
+ * Functions.find(values, (v): v is string => typeof v === 'string'); // string | undefined, 'two'
209
+ * ```
210
+ * @since 0.5.0
211
+ */
212
+ declare function find<T, S extends T>(iterable: Iterable<T>, predicate: (value: T, index: number) => value is S): S | undefined;
213
+
214
+ /**
215
+ * Returns the first value that satisfies `predicate`; stops and closes the source at the first match.
216
+ * @operation `Action`
217
+ * @param iterable - the source `Iterable`
218
+ * @param predicate - called with each value and its index
219
+ * @returns the first value that satisfies `predicate`, or `undefined` if there is none
220
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
221
+ * @example
222
+ * ```ts
223
+ * Functions.find([1, 5, 6], v => v > 4); // 5
224
+ * ```
225
+ * @since 0.5.0
226
+ */
227
+ declare function find<T>(iterable: Iterable<T>, predicate: Predicate<T>): T | undefined;
228
+
229
+ /**
230
+ * Returns the index of the first value that satisfies `predicate`; stops and closes the source at the first match.
231
+ * @operation `Action`
232
+ * @param iterable - the source `Iterable`
233
+ * @param predicate - called with each value and its index
234
+ * @returns the index of the first value that satisfies `predicate`, or `-1` if there is none
235
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
236
+ * @example
237
+ * ```ts
238
+ * Functions.findIndex([1, 5, 6], v => v > 4); // 1
239
+ * ```
240
+ * @since 0.5.0
241
+ */
242
+ declare function findIndex<T>(iterable: Iterable<T>, predicate: Predicate<T>): number;
243
+
135
244
  /**
136
245
  * Lazily maps each value to an `Iterable` and flattens the results.
137
246
  * Each inner `Iterable` is read completely before the next value is mapped.
@@ -245,11 +354,17 @@ export declare function fromRange(start: number, end: number, options?: IRangeOp
245
354
  declare namespace Functions {
246
355
  export {
247
356
  collectToArray,
357
+ count,
358
+ distinct,
248
359
  empty_2 as empty,
360
+ every,
249
361
  filter,
362
+ find,
363
+ findIndex,
250
364
  flatMap,
251
365
  forEach,
252
366
  forEachAsync,
367
+ includes,
253
368
  map,
254
369
  materialize,
255
370
  max,
@@ -260,8 +375,10 @@ declare namespace Functions {
260
375
  reduce,
261
376
  repeat_2 as repeat,
262
377
  skip,
378
+ skipWhile,
263
379
  some,
264
380
  take,
381
+ takeWhile,
265
382
  tap,
266
383
  tapChain
267
384
  }
@@ -314,6 +431,60 @@ export declare interface IIterableLinqBase<T> {
314
431
  * @since 0.0.1
315
432
  */
316
433
  collectToArray(): T[];
434
+ /**
435
+ * Counts the values of the chain; runs the whole chain.
436
+ * @operation `Action`
437
+ * @returns the number of values in the chain
438
+ * @example
439
+ * ```ts
440
+ * IterableLinq.from([1, 2, 3]).count(); // 3
441
+ * ```
442
+ * @since 0.5.0
443
+ */
444
+ count(): number;
445
+ /**
446
+ * Counts the values that satisfy `predicate`; runs the whole chain.
447
+ * If `predicate` throws, the source is closed and the error propagates.
448
+ * @operation `Action`
449
+ * @param predicate - called with each value and its index; `undefined` counts every value
450
+ * @returns the number of values that satisfy `predicate`
451
+ * @throws Error if a provided `predicate` is not a function
452
+ * @example
453
+ * ```ts
454
+ * IterableLinq.from([1, 5, 2, 6]).count(v => v > 4); // 2
455
+ * ```
456
+ * @since 0.5.0
457
+ */
458
+ count(predicate: Predicate<T> | undefined): number;
459
+ /**
460
+ * Yields the first value for each distinct value or selected key, in source order.
461
+ * Keys use `SameValueZero`, like `Set`; original values are preserved.
462
+ * Each iteration stores its own seen keys. If `keySelector` throws, the source is closed and the error propagates.
463
+ * @operation `Transformation`
464
+ * @param keySelector - called with every source value and its index; omitted or `undefined` compares values directly
465
+ * @returns a new lazy, re-runnable chain of the first values for each key
466
+ * @throws Error if a provided `keySelector` is not a function
467
+ * @example
468
+ * ```ts
469
+ * IterableLinq.from([3, 1, 3, 2, 1]).distinct().collectToArray(); // [3, 1, 2]
470
+ * IterableLinq.from([{ id: 1 }, { id: 1 }, { id: 2 }]).distinct(v => v.id).collectToArray(); // [{ id: 1 }, { id: 2 }]
471
+ * ```
472
+ * @since 0.5.0
473
+ */
474
+ distinct<K>(keySelector?: Mapper<T, K>): IIterableLinq<T>;
475
+ /**
476
+ * Tells whether every value satisfies `predicate`; stops and closes the source at the first rejected value.
477
+ * @operation `Action`
478
+ * @param predicate - called with each value and its index
479
+ * @returns `true` if every value satisfies `predicate`, or if the chain is empty; `false` otherwise
480
+ * @throws Error if `predicate` is not a function
481
+ * @example
482
+ * ```ts
483
+ * IterableLinq.from([1, 2, 3]).every(v => v > 0); // true
484
+ * ```
485
+ * @since 0.5.0
486
+ */
487
+ every(predicate: Predicate<T>): boolean;
317
488
  /**
318
489
  * Keeps the values accepted by a type guard and narrows their type.
319
490
  * If `predicate` throws, the source is closed and the error propagates.
@@ -343,6 +514,46 @@ export declare interface IIterableLinqBase<T> {
343
514
  * @since 0.0.1
344
515
  */
345
516
  filter(predicate: Predicate<T>): IIterableLinq<T>;
517
+ /**
518
+ * Returns the first value accepted by a type guard, narrowing its type; stops and closes the source at the first match.
519
+ * @operation `Action`
520
+ * @param predicate - a type guard called with each value and its index
521
+ * @returns the first value accepted by `predicate`, or `undefined` if there is none
522
+ * @throws Error if `predicate` is not a function
523
+ * @example
524
+ * ```ts
525
+ * const values: (number | string)[] = [1, 'two', 3];
526
+ * IterableLinq.from(values).find((v): v is string => typeof v === 'string'); // string | undefined, 'two'
527
+ * ```
528
+ * @since 0.5.0
529
+ */
530
+ find<S extends T>(predicate: (value: T, index: number) => value is S): S | undefined;
531
+ /**
532
+ * Returns the first value that satisfies `predicate`; stops and closes the source at the first match.
533
+ * @operation `Action`
534
+ * @param predicate - called with each value and its index
535
+ * @returns the first value that satisfies `predicate`, or `undefined` if there is none
536
+ * @throws Error if `predicate` is not a function
537
+ * @example
538
+ * ```ts
539
+ * IterableLinq.from([1, 5, 6]).find(v => v > 4); // 5
540
+ * ```
541
+ * @since 0.5.0
542
+ */
543
+ find(predicate: Predicate<T>): T | undefined;
544
+ /**
545
+ * Returns the index of the first value that satisfies `predicate`; stops and closes the source at the first match.
546
+ * @operation `Action`
547
+ * @param predicate - called with each value and its index
548
+ * @returns the index of the first value that satisfies `predicate`, or `-1` if there is none
549
+ * @throws Error if `predicate` is not a function
550
+ * @example
551
+ * ```ts
552
+ * IterableLinq.from([1, 5, 6]).findIndex(v => v > 4); // 1
553
+ * ```
554
+ * @since 0.5.0
555
+ */
556
+ findIndex(predicate: Predicate<T>): number;
346
557
  /**
347
558
  * Maps each value to an `Iterable` and flattens the results into one chain.
348
559
  * Each inner `Iterable` is read completely before the next value of the chain is mapped.
@@ -394,6 +605,19 @@ export declare interface IIterableLinqBase<T> {
394
605
  * @since 0.0.11
395
606
  */
396
607
  forEachAsync(action: AsyncAction<T>): Promise<Unit>;
608
+ /**
609
+ * Tells whether the chain contains `value`, compared with `SameValueZero` like `Array.prototype.includes`;
610
+ * stops and closes the source at the first match.
611
+ * @operation `Action`
612
+ * @param value - the value to look for; `NaN` matches `NaN`, and `+0` matches `-0`
613
+ * @returns `true` if the chain contains `value`; `false` otherwise
614
+ * @example
615
+ * ```ts
616
+ * IterableLinq.from([1, 2, NaN]).includes(NaN); // true
617
+ * ```
618
+ * @since 0.5.0
619
+ */
620
+ includes(value: T): boolean;
397
621
  /**
398
622
  * Transforms each value with `mapper`.
399
623
  * If `mapper` throws, the source is closed and the error propagates.
@@ -508,6 +732,21 @@ export declare interface IIterableLinqBase<T> {
508
732
  * @since 0.3.0
509
733
  */
510
734
  skip(count: number): IIterableLinq<T>;
735
+ /**
736
+ * Skips the values while `predicate` returns `true`, then yields the first rejected value and all the rest.
737
+ * After the first rejected value, `predicate` is not called again.
738
+ * If `predicate` throws, the source is closed and the error propagates.
739
+ * @operation `Transformation`
740
+ * @param predicate - called with each value and its index until it returns `false`
741
+ * @returns a new chain of the values from the first rejected one
742
+ * @throws Error if `predicate` is not a function
743
+ * @example
744
+ * ```ts
745
+ * IterableLinq.from([1, 2, 5, 3]).skipWhile(v => v < 4).collectToArray(); // [5, 3]
746
+ * ```
747
+ * @since 0.5.0
748
+ */
749
+ skipWhile(predicate: Predicate<T>): IIterableLinq<T>;
511
750
  /**
512
751
  * Runs the chain until its first value, then stops and closes the source.
513
752
  * @operation `Action`
@@ -546,6 +785,37 @@ export declare interface IIterableLinqBase<T> {
546
785
  * @since 0.3.0
547
786
  */
548
787
  take(count: number): IIterableLinq<T>;
788
+ /**
789
+ * Yields the values while a type guard accepts them, narrowing their type, then closes the source.
790
+ * The source is never read past the first rejected value, which is not yielded.
791
+ * If `predicate` throws, the source is closed and the error propagates.
792
+ * @operation `Transformation`
793
+ * @param predicate - a type guard called with each value and its index; the first `false` ends the chain
794
+ * @returns a new chain of the narrowed values before the first rejected one
795
+ * @throws Error if `predicate` is not a function
796
+ * @example
797
+ * ```ts
798
+ * const values: (number | string)[] = [1, 2, 'three', 4];
799
+ * IterableLinq.from(values).takeWhile((v): v is number => typeof v === 'number').collectToArray(); // number[], [1, 2]
800
+ * ```
801
+ * @since 0.5.0
802
+ */
803
+ takeWhile<S extends T>(predicate: (value: T, index: number) => value is S): IIterableLinq<S>;
804
+ /**
805
+ * Yields the values while `predicate` returns `true`, then closes the source.
806
+ * The source is never read past the first rejected value, which is not yielded, so `takeWhile` can end an infinite chain.
807
+ * If `predicate` throws, the source is closed and the error propagates.
808
+ * @operation `Transformation`
809
+ * @param predicate - called with each value and its index; the first `false` ends the chain
810
+ * @returns a new chain of the values before the first rejected one
811
+ * @throws Error if `predicate` is not a function
812
+ * @example
813
+ * ```ts
814
+ * IterableLinq.from([1, 2, 5, 3]).takeWhile(v => v < 4).collectToArray(); // [1, 2]
815
+ * ```
816
+ * @since 0.5.0
817
+ */
818
+ takeWhile(predicate: Predicate<T>): IIterableLinq<T>;
549
819
  /**
550
820
  * Calls `tapper` on each value as it flows through the chain, without changing it.
551
821
  * If `tapper` throws, the source is closed and the error propagates.
@@ -609,6 +879,22 @@ export declare interface IMemoizeOptions {
609
879
  allowPartialMemoization?: boolean;
610
880
  }
611
881
 
882
+ /**
883
+ * Tells whether `iterable` contains `value`, compared with `SameValueZero` like `Array.prototype.includes`;
884
+ * stops and closes the source at the first match.
885
+ * @operation `Action`
886
+ * @param iterable - the source `Iterable`
887
+ * @param value - the value to look for; `NaN` matches `NaN`, and `+0` matches `-0`
888
+ * @returns `true` if `iterable` contains `value`; `false` otherwise
889
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
890
+ * @example
891
+ * ```ts
892
+ * Functions.includes([1, 2, NaN], NaN); // true
893
+ * ```
894
+ * @since 0.5.0
895
+ */
896
+ declare function includes<T>(iterable: Iterable<T>, value: T): boolean;
897
+
612
898
  /**
613
899
  * Options of `range`.
614
900
  * @since 0.1.0
@@ -874,6 +1160,23 @@ declare function repeat_2<T>(value: T, count: number): Iterable<T>;
874
1160
  */
875
1161
  declare function skip<T>(iterable: Iterable<T>, count: number): Iterable<T>;
876
1162
 
1163
+ /**
1164
+ * Lazily skips the values while `predicate` returns `true`, then yields the first rejected value and all the rest.
1165
+ * After the first rejected value, `predicate` is not called again.
1166
+ * If `predicate` throws, the source is closed and the error propagates.
1167
+ * @operation `Transformation`
1168
+ * @param iterable - the source `Iterable`
1169
+ * @param predicate - called with each value and its index until it returns `false`
1170
+ * @returns a lazy, re-runnable `Iterable` of the values from the first rejected one
1171
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
1172
+ * @example
1173
+ * ```ts
1174
+ * Array.from(Functions.skipWhile([1, 2, 5, 3], v => v < 4)); // [5, 3]
1175
+ * ```
1176
+ * @since 0.5.0
1177
+ */
1178
+ declare function skipWhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
1179
+
877
1180
  /**
878
1181
  * Tells whether `iterable` contains a value; reads one value, then closes the source.
879
1182
  * @operation `Action`
@@ -919,6 +1222,41 @@ declare function some<T>(iterable: Iterable<T>, predicate: Predicate<T> | undefi
919
1222
  */
920
1223
  declare function take<T>(iterable: Iterable<T>, count: number): Iterable<T>;
921
1224
 
1225
+ /**
1226
+ * Lazily yields the values while a type guard accepts them, narrowing their type, then closes the source.
1227
+ * The source is never read past the first rejected value, which is not yielded.
1228
+ * If `predicate` throws, the source is closed and the error propagates.
1229
+ * @operation `Transformation`
1230
+ * @param iterable - the source `Iterable`
1231
+ * @param predicate - a type guard called with each value and its index; the first `false` ends the iterable
1232
+ * @returns a lazy, re-runnable `Iterable` of the narrowed values before the first rejected one
1233
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
1234
+ * @example
1235
+ * ```ts
1236
+ * const values: (number | string)[] = [1, 2, 'three', 4];
1237
+ * Array.from(Functions.takeWhile(values, (v): v is number => typeof v === 'number')); // number[], [1, 2]
1238
+ * ```
1239
+ * @since 0.5.0
1240
+ */
1241
+ declare function takeWhile<T, S extends T>(iterable: Iterable<T>, predicate: (value: T, index: number) => value is S): Iterable<S>;
1242
+
1243
+ /**
1244
+ * Lazily yields the values while `predicate` returns `true`, then closes the source.
1245
+ * The source is never read past the first rejected value, which is not yielded, so `takeWhile` can end an infinite source.
1246
+ * If `predicate` throws, the source is closed and the error propagates.
1247
+ * @operation `Transformation`
1248
+ * @param iterable - the source `Iterable`
1249
+ * @param predicate - called with each value and its index; the first `false` ends the iterable
1250
+ * @returns a lazy, re-runnable `Iterable` of the values before the first rejected one
1251
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
1252
+ * @example
1253
+ * ```ts
1254
+ * Array.from(Functions.takeWhile([1, 2, 5, 3], v => v < 4)); // [1, 2]
1255
+ * ```
1256
+ * @since 0.5.0
1257
+ */
1258
+ declare function takeWhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
1259
+
922
1260
  /**
923
1261
  * Lazily calls `tapper` on each value as it flows through, without changing it.
924
1262
  * If `tapper` throws, the source is closed and the error propagates.