iterable-linq-utility 0.4.0 → 0.6.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
@@ -15,6 +15,24 @@ export declare type Action<T> = (value: T, index: number) => Unit;
15
15
  */
16
16
  export declare type AsyncAction<T> = (value: T, index: number) => Promise<Unit>;
17
17
 
18
+ /**
19
+ * Returns the value at `index`, like `Array.prototype.at`; a negative index counts from the end.
20
+ * A non-negative index reads up to the value, then closes the source; a negative index reads the whole source,
21
+ * keeping only the last `-index` values.
22
+ * @operation `Action`
23
+ * @param iterable - the source `Iterable`
24
+ * @param index - an integer; `-1` is the last value
25
+ * @returns the value at `index`, or `undefined` if `iterable` has no value there
26
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `index` is not an integer (fractions, `NaN` and `Infinity` included)
27
+ * @example
28
+ * ```ts
29
+ * Functions.at([10, 20, 30], 1); // 20
30
+ * Functions.at([10, 20, 30], -1); // 30
31
+ * ```
32
+ * @since 0.6.0
33
+ */
34
+ declare function at<T>(iterable: Iterable<T>, index: number): T | undefined;
35
+
18
36
  /**
19
37
  * Implementation of a method added with `extend` or `override`. `this` is the chain the method is called on.
20
38
  * @since 0.1.0
@@ -54,6 +72,54 @@ declare type ComparerFunction<T> = (a: T, b: T) => number;
54
72
 
55
73
  declare type ComparingProps<T> = keyof T | Array<keyof T>;
56
74
 
75
+ /**
76
+ * Counts the values of `iterable`; reads the whole source.
77
+ * @operation `Action`
78
+ * @param iterable - the source `Iterable`
79
+ * @returns the number of values in `iterable`
80
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
81
+ * @example
82
+ * ```ts
83
+ * Functions.count([1, 2, 3]); // 3
84
+ * ```
85
+ * @since 0.5.0
86
+ */
87
+ declare function count<T>(iterable: Iterable<T>): number;
88
+
89
+ /**
90
+ * Counts the values that satisfy `predicate`; reads the whole source.
91
+ * If `predicate` throws, the source is closed and the error propagates.
92
+ * @operation `Action`
93
+ * @param iterable - the source `Iterable`
94
+ * @param predicate - called with each value and its index; `undefined` counts every value
95
+ * @returns the number of values that satisfy `predicate`
96
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if a provided `predicate` is not a function
97
+ * @example
98
+ * ```ts
99
+ * Functions.count([1, 5, 2, 6], v => v > 4); // 2
100
+ * ```
101
+ * @since 0.5.0
102
+ */
103
+ declare function count<T>(iterable: Iterable<T>, predicate: Predicate<T> | undefined): number;
104
+
105
+ /**
106
+ * Lazily yields the first value for each distinct value or selected key, in source order.
107
+ * Keys use `SameValueZero`, like `Set`; original values are preserved.
108
+ * Each iterator stores its own seen keys. If `keySelector` throws, the source is closed and the error propagates.
109
+ * @operation `Transformation`
110
+ * @param iterable - the source `Iterable`
111
+ * @param keySelector - called with every source value and its index; omitted or `undefined` compares values directly
112
+ * @returns a lazy, re-runnable `Iterable` of the first values for each key
113
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if a provided `keySelector` is not a function
114
+ * @example
115
+ * ```ts
116
+ * Array.from(Functions.distinct([3, 1, 3, 2, 1])); // [3, 1, 2]
117
+ * Array.from(Functions.distinct([{ id: 1 }, { id: 1 }, { id: 2 }], v => v.id)); // [{ id: 1 }, { id: 2 }]
118
+ * ```
119
+ * @since 0.5.0
120
+ */
121
+ declare function distinct<T, K>(iterable: Iterable<T>, keySelector?: Mapper<T, K>): Iterable<T>;
122
+
57
123
  /**
58
124
  * Starts a chain with no values.
59
125
  * @returns an empty chain
@@ -77,6 +143,21 @@ export declare function empty<T>(): IIterableLinq<T>;
77
143
  */
78
144
  declare function empty_2<T>(): Iterable<T>;
79
145
 
146
+ /**
147
+ * Tells whether every value satisfies `predicate`; stops and closes the source at the first rejected value.
148
+ * @operation `Action`
149
+ * @param iterable - the source `Iterable`
150
+ * @param predicate - called with each value and its index
151
+ * @returns `true` if every value satisfies `predicate`, or if `iterable` is empty; `false` otherwise
152
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
153
+ * @example
154
+ * ```ts
155
+ * Functions.every([1, 2, 3], v => v > 0); // true
156
+ * ```
157
+ * @since 0.5.0
158
+ */
159
+ declare function every<T>(iterable: Iterable<T>, predicate: Predicate<T>): boolean;
160
+
80
161
  /**
81
162
  * Adds a method to every chain, including the chains created before the call.
82
163
  * Declare the method first by augmenting `IIterableLinq`, then register it once, at application start-up.
@@ -132,6 +213,101 @@ declare function filter<T, S extends T>(iterable: Iterable<T>, predicate: (value
132
213
  */
133
214
  declare function filter<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
134
215
 
216
+ /**
217
+ * Returns the first value accepted by a type guard, narrowing its type; stops and closes the source at the first match.
218
+ * @operation `Action`
219
+ * @param iterable - the source `Iterable`
220
+ * @param predicate - a type guard called with each value and its index
221
+ * @returns the first value accepted by `predicate`, or `undefined` if there is none
222
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
223
+ * @example
224
+ * ```ts
225
+ * const values: (number | string)[] = [1, 'two', 3];
226
+ * Functions.find(values, (v): v is string => typeof v === 'string'); // string | undefined, 'two'
227
+ * ```
228
+ * @since 0.5.0
229
+ */
230
+ declare function find<T, S extends T>(iterable: Iterable<T>, predicate: (value: T, index: number) => value is S): S | undefined;
231
+
232
+ /**
233
+ * Returns the first value that satisfies `predicate`; stops and closes the source at the first match.
234
+ * @operation `Action`
235
+ * @param iterable - the source `Iterable`
236
+ * @param predicate - called with each value and its index
237
+ * @returns the first value that satisfies `predicate`, or `undefined` if there is none
238
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
239
+ * @example
240
+ * ```ts
241
+ * Functions.find([1, 5, 6], v => v > 4); // 5
242
+ * ```
243
+ * @since 0.5.0
244
+ */
245
+ declare function find<T>(iterable: Iterable<T>, predicate: Predicate<T>): T | undefined;
246
+
247
+ /**
248
+ * Returns the index of the first value that satisfies `predicate`; stops and closes the source at the first match.
249
+ * @operation `Action`
250
+ * @param iterable - the source `Iterable`
251
+ * @param predicate - called with each value and its index
252
+ * @returns the index of the first value that satisfies `predicate`, or `-1` if there is none
253
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
254
+ * @example
255
+ * ```ts
256
+ * Functions.findIndex([1, 5, 6], v => v > 4); // 1
257
+ * ```
258
+ * @since 0.5.0
259
+ */
260
+ declare function findIndex<T>(iterable: Iterable<T>, predicate: Predicate<T>): number;
261
+
262
+ /**
263
+ * Returns the last value accepted by a type guard, narrowing its type; reads the whole source.
264
+ * If `predicate` throws, the source is closed and the error propagates.
265
+ * @operation `Action`
266
+ * @param iterable - the source `Iterable`
267
+ * @param predicate - a type guard called with each value and its index
268
+ * @returns the last value accepted by `predicate`, or `undefined` if there is none
269
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
270
+ * @example
271
+ * ```ts
272
+ * const values: (number | string)[] = [1, 'two', 3, 'four'];
273
+ * Functions.findLast(values, (v): v is string => typeof v === 'string'); // string | undefined, 'four'
274
+ * ```
275
+ * @since 0.6.0
276
+ */
277
+ declare function findLast<T, S extends T>(iterable: Iterable<T>, predicate: (value: T, index: number) => value is S): S | undefined;
278
+
279
+ /**
280
+ * Returns the last value that satisfies `predicate`; reads the whole source.
281
+ * If `predicate` throws, the source is closed and the error propagates.
282
+ * @operation `Action`
283
+ * @param iterable - the source `Iterable`
284
+ * @param predicate - called with each value and its index
285
+ * @returns the last value that satisfies `predicate`, or `undefined` if there is none
286
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
287
+ * @example
288
+ * ```ts
289
+ * Functions.findLast([1, 5, 6, 2], v => v > 4); // 6
290
+ * ```
291
+ * @since 0.6.0
292
+ */
293
+ declare function findLast<T>(iterable: Iterable<T>, predicate: Predicate<T>): T | undefined;
294
+
295
+ /**
296
+ * Returns the index of the last value that satisfies `predicate`; reads the whole source.
297
+ * If `predicate` throws, the source is closed and the error propagates.
298
+ * @operation `Action`
299
+ * @param iterable - the source `Iterable`
300
+ * @param predicate - called with each value and its index
301
+ * @returns the index of the last value that satisfies `predicate`, or `-1` if there is none
302
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
303
+ * @example
304
+ * ```ts
305
+ * Functions.findLastIndex([1, 5, 6, 2], v => v > 4); // 2
306
+ * ```
307
+ * @since 0.6.0
308
+ */
309
+ declare function findLastIndex<T>(iterable: Iterable<T>, predicate: Predicate<T>): number;
310
+
135
311
  /**
136
312
  * Lazily maps each value to an `Iterable` and flattens the results.
137
313
  * Each inner `Iterable` is read completely before the next value is mapped.
@@ -244,12 +420,23 @@ export declare function fromRange(start: number, end: number, options?: IRangeOp
244
420
 
245
421
  declare namespace Functions {
246
422
  export {
423
+ at,
247
424
  collectToArray,
425
+ count,
426
+ distinct,
248
427
  empty_2 as empty,
428
+ every,
249
429
  filter,
430
+ find,
431
+ findIndex,
432
+ findLast,
433
+ findLastIndex,
250
434
  flatMap,
251
435
  forEach,
252
436
  forEachAsync,
437
+ includes,
438
+ indexOf,
439
+ lastIndexOf,
253
440
  map,
254
441
  materialize,
255
442
  max,
@@ -260,8 +447,10 @@ declare namespace Functions {
260
447
  reduce,
261
448
  repeat_2 as repeat,
262
449
  skip,
450
+ skipWhile,
263
451
  some,
264
452
  take,
453
+ takeWhile,
265
454
  tap,
266
455
  tapChain
267
456
  }
@@ -303,6 +492,22 @@ export declare interface IIterableLinqBase<T> {
303
492
  * @since 0.0.1
304
493
  */
305
494
  [Symbol.iterator](): Iterator<T, any, undefined>;
495
+ /**
496
+ * Returns the value at `index`, like `Array.prototype.at`; a negative index counts from the end.
497
+ * A non-negative index runs the chain up to the value, then closes the source; a negative index runs the whole chain,
498
+ * keeping only the last `-index` values.
499
+ * @operation `Action`
500
+ * @param index - an integer; `-1` is the last value
501
+ * @returns the value at `index`, or `undefined` if the chain has no value there
502
+ * @throws Error if `index` is not an integer (fractions, `NaN` and `Infinity` included)
503
+ * @example
504
+ * ```ts
505
+ * IterableLinq.from([10, 20, 30]).at(1); // 20
506
+ * IterableLinq.from([10, 20, 30]).at(-1); // 30
507
+ * ```
508
+ * @since 0.6.0
509
+ */
510
+ at(index: number): T | undefined;
306
511
  /**
307
512
  * Runs the chain and collects its values into an `Array`.
308
513
  * @operation `Action`
@@ -314,6 +519,60 @@ export declare interface IIterableLinqBase<T> {
314
519
  * @since 0.0.1
315
520
  */
316
521
  collectToArray(): T[];
522
+ /**
523
+ * Counts the values of the chain; runs the whole chain.
524
+ * @operation `Action`
525
+ * @returns the number of values in the chain
526
+ * @example
527
+ * ```ts
528
+ * IterableLinq.from([1, 2, 3]).count(); // 3
529
+ * ```
530
+ * @since 0.5.0
531
+ */
532
+ count(): number;
533
+ /**
534
+ * Counts the values that satisfy `predicate`; runs the whole chain.
535
+ * If `predicate` throws, the source is closed and the error propagates.
536
+ * @operation `Action`
537
+ * @param predicate - called with each value and its index; `undefined` counts every value
538
+ * @returns the number of values that satisfy `predicate`
539
+ * @throws Error if a provided `predicate` is not a function
540
+ * @example
541
+ * ```ts
542
+ * IterableLinq.from([1, 5, 2, 6]).count(v => v > 4); // 2
543
+ * ```
544
+ * @since 0.5.0
545
+ */
546
+ count(predicate: Predicate<T> | undefined): number;
547
+ /**
548
+ * Yields the first value for each distinct value or selected key, in source order.
549
+ * Keys use `SameValueZero`, like `Set`; original values are preserved.
550
+ * Each iteration stores its own seen keys. If `keySelector` throws, the source is closed and the error propagates.
551
+ * @operation `Transformation`
552
+ * @param keySelector - called with every source value and its index; omitted or `undefined` compares values directly
553
+ * @returns a new lazy, re-runnable chain of the first values for each key
554
+ * @throws Error if a provided `keySelector` is not a function
555
+ * @example
556
+ * ```ts
557
+ * IterableLinq.from([3, 1, 3, 2, 1]).distinct().collectToArray(); // [3, 1, 2]
558
+ * IterableLinq.from([{ id: 1 }, { id: 1 }, { id: 2 }]).distinct(v => v.id).collectToArray(); // [{ id: 1 }, { id: 2 }]
559
+ * ```
560
+ * @since 0.5.0
561
+ */
562
+ distinct<K>(keySelector?: Mapper<T, K>): IIterableLinq<T>;
563
+ /**
564
+ * Tells whether every value satisfies `predicate`; stops and closes the source at the first rejected value.
565
+ * @operation `Action`
566
+ * @param predicate - called with each value and its index
567
+ * @returns `true` if every value satisfies `predicate`, or if the chain is empty; `false` otherwise
568
+ * @throws Error if `predicate` is not a function
569
+ * @example
570
+ * ```ts
571
+ * IterableLinq.from([1, 2, 3]).every(v => v > 0); // true
572
+ * ```
573
+ * @since 0.5.0
574
+ */
575
+ every(predicate: Predicate<T>): boolean;
317
576
  /**
318
577
  * Keeps the values accepted by a type guard and narrows their type.
319
578
  * If `predicate` throws, the source is closed and the error propagates.
@@ -343,6 +602,89 @@ export declare interface IIterableLinqBase<T> {
343
602
  * @since 0.0.1
344
603
  */
345
604
  filter(predicate: Predicate<T>): IIterableLinq<T>;
605
+ /**
606
+ * Returns the first value accepted by a type guard, narrowing its type; stops and closes the source at the first match.
607
+ * @operation `Action`
608
+ * @param predicate - a type guard called with each value and its index
609
+ * @returns the first value accepted by `predicate`, or `undefined` if there is none
610
+ * @throws Error if `predicate` is not a function
611
+ * @example
612
+ * ```ts
613
+ * const values: (number | string)[] = [1, 'two', 3];
614
+ * IterableLinq.from(values).find((v): v is string => typeof v === 'string'); // string | undefined, 'two'
615
+ * ```
616
+ * @since 0.5.0
617
+ */
618
+ find<S extends T>(predicate: (value: T, index: number) => value is S): S | undefined;
619
+ /**
620
+ * Returns the first value that satisfies `predicate`; stops and closes the source at the first match.
621
+ * @operation `Action`
622
+ * @param predicate - called with each value and its index
623
+ * @returns the first value that satisfies `predicate`, or `undefined` if there is none
624
+ * @throws Error if `predicate` is not a function
625
+ * @example
626
+ * ```ts
627
+ * IterableLinq.from([1, 5, 6]).find(v => v > 4); // 5
628
+ * ```
629
+ * @since 0.5.0
630
+ */
631
+ find(predicate: Predicate<T>): T | undefined;
632
+ /**
633
+ * Returns the index of the first value that satisfies `predicate`; stops and closes the source at the first match.
634
+ * @operation `Action`
635
+ * @param predicate - called with each value and its index
636
+ * @returns the index of the first value that satisfies `predicate`, or `-1` if there is none
637
+ * @throws Error if `predicate` is not a function
638
+ * @example
639
+ * ```ts
640
+ * IterableLinq.from([1, 5, 6]).findIndex(v => v > 4); // 1
641
+ * ```
642
+ * @since 0.5.0
643
+ */
644
+ findIndex(predicate: Predicate<T>): number;
645
+ /**
646
+ * Returns the last value accepted by a type guard, narrowing its type; runs the whole chain.
647
+ * If `predicate` throws, the source is closed and the error propagates.
648
+ * @operation `Action`
649
+ * @param predicate - a type guard called with each value and its index
650
+ * @returns the last value accepted by `predicate`, or `undefined` if there is none
651
+ * @throws Error if `predicate` is not a function
652
+ * @example
653
+ * ```ts
654
+ * const values: (number | string)[] = [1, 'two', 3, 'four'];
655
+ * IterableLinq.from(values).findLast((v): v is string => typeof v === 'string'); // string | undefined, 'four'
656
+ * ```
657
+ * @since 0.6.0
658
+ */
659
+ findLast<S extends T>(predicate: (value: T, index: number) => value is S): S | undefined;
660
+ /**
661
+ * Returns the last value that satisfies `predicate`; runs the whole chain.
662
+ * If `predicate` throws, the source is closed and the error propagates.
663
+ * @operation `Action`
664
+ * @param predicate - called with each value and its index
665
+ * @returns the last value that satisfies `predicate`, or `undefined` if there is none
666
+ * @throws Error if `predicate` is not a function
667
+ * @example
668
+ * ```ts
669
+ * IterableLinq.from([1, 5, 6, 2]).findLast(v => v > 4); // 6
670
+ * ```
671
+ * @since 0.6.0
672
+ */
673
+ findLast(predicate: Predicate<T>): T | undefined;
674
+ /**
675
+ * Returns the index of the last value that satisfies `predicate`; runs the whole chain.
676
+ * If `predicate` throws, the source is closed and the error propagates.
677
+ * @operation `Action`
678
+ * @param predicate - called with each value and its index
679
+ * @returns the index of the last value that satisfies `predicate`, or `-1` if there is none
680
+ * @throws Error if `predicate` is not a function
681
+ * @example
682
+ * ```ts
683
+ * IterableLinq.from([1, 5, 6, 2]).findLastIndex(v => v > 4); // 2
684
+ * ```
685
+ * @since 0.6.0
686
+ */
687
+ findLastIndex(predicate: Predicate<T>): number;
346
688
  /**
347
689
  * Maps each value to an `Iterable` and flattens the results into one chain.
348
690
  * Each inner `Iterable` is read completely before the next value of the chain is mapped.
@@ -394,6 +736,45 @@ export declare interface IIterableLinqBase<T> {
394
736
  * @since 0.0.11
395
737
  */
396
738
  forEachAsync(action: AsyncAction<T>): Promise<Unit>;
739
+ /**
740
+ * Tells whether the chain contains `value`, compared with `SameValueZero` like `Array.prototype.includes`;
741
+ * stops and closes the source at the first match.
742
+ * @operation `Action`
743
+ * @param value - the value to look for; `NaN` matches `NaN`, and `+0` matches `-0`
744
+ * @returns `true` if the chain contains `value`; `false` otherwise
745
+ * @example
746
+ * ```ts
747
+ * IterableLinq.from([1, 2, NaN]).includes(NaN); // true
748
+ * ```
749
+ * @since 0.5.0
750
+ */
751
+ includes(value: T): boolean;
752
+ /**
753
+ * Returns the index of the first value strictly equal (`===`) to `value`, like `Array.prototype.indexOf`;
754
+ * stops and closes the source at the first match.
755
+ * @operation `Action`
756
+ * @param value - the value to look for; `NaN` is never found, use `includes` or `findIndex` for it
757
+ * @returns the index of the first value equal to `value`, or `-1` if there is none
758
+ * @example
759
+ * ```ts
760
+ * IterableLinq.from([1, 2, 3, 2]).indexOf(2); // 1
761
+ * ```
762
+ * @since 0.6.0
763
+ */
764
+ indexOf(value: T): number;
765
+ /**
766
+ * Returns the index of the last value strictly equal (`===`) to `value`, like `Array.prototype.lastIndexOf`;
767
+ * runs the whole chain.
768
+ * @operation `Action`
769
+ * @param value - the value to look for; `NaN` is never found, use `findLastIndex` for it
770
+ * @returns the index of the last value equal to `value`, or `-1` if there is none
771
+ * @example
772
+ * ```ts
773
+ * IterableLinq.from([1, 2, 3, 2]).lastIndexOf(2); // 3
774
+ * ```
775
+ * @since 0.6.0
776
+ */
777
+ lastIndexOf(value: T): number;
397
778
  /**
398
779
  * Transforms each value with `mapper`.
399
780
  * If `mapper` throws, the source is closed and the error propagates.
@@ -508,6 +889,21 @@ export declare interface IIterableLinqBase<T> {
508
889
  * @since 0.3.0
509
890
  */
510
891
  skip(count: number): IIterableLinq<T>;
892
+ /**
893
+ * Skips the values while `predicate` returns `true`, then yields the first rejected value and all the rest.
894
+ * After the first rejected value, `predicate` is not called again.
895
+ * If `predicate` throws, the source is closed and the error propagates.
896
+ * @operation `Transformation`
897
+ * @param predicate - called with each value and its index until it returns `false`
898
+ * @returns a new chain of the values from the first rejected one
899
+ * @throws Error if `predicate` is not a function
900
+ * @example
901
+ * ```ts
902
+ * IterableLinq.from([1, 2, 5, 3]).skipWhile(v => v < 4).collectToArray(); // [5, 3]
903
+ * ```
904
+ * @since 0.5.0
905
+ */
906
+ skipWhile(predicate: Predicate<T>): IIterableLinq<T>;
511
907
  /**
512
908
  * Runs the chain until its first value, then stops and closes the source.
513
909
  * @operation `Action`
@@ -546,6 +942,37 @@ export declare interface IIterableLinqBase<T> {
546
942
  * @since 0.3.0
547
943
  */
548
944
  take(count: number): IIterableLinq<T>;
945
+ /**
946
+ * Yields the values while a type guard accepts them, narrowing their type, then closes the source.
947
+ * The source is never read past the first rejected value, which is not yielded.
948
+ * If `predicate` throws, the source is closed and the error propagates.
949
+ * @operation `Transformation`
950
+ * @param predicate - a type guard called with each value and its index; the first `false` ends the chain
951
+ * @returns a new chain of the narrowed values before the first rejected one
952
+ * @throws Error if `predicate` is not a function
953
+ * @example
954
+ * ```ts
955
+ * const values: (number | string)[] = [1, 2, 'three', 4];
956
+ * IterableLinq.from(values).takeWhile((v): v is number => typeof v === 'number').collectToArray(); // number[], [1, 2]
957
+ * ```
958
+ * @since 0.5.0
959
+ */
960
+ takeWhile<S extends T>(predicate: (value: T, index: number) => value is S): IIterableLinq<S>;
961
+ /**
962
+ * Yields the values while `predicate` returns `true`, then closes the source.
963
+ * The source is never read past the first rejected value, which is not yielded, so `takeWhile` can end an infinite chain.
964
+ * If `predicate` throws, the source is closed and the error propagates.
965
+ * @operation `Transformation`
966
+ * @param predicate - called with each value and its index; the first `false` ends the chain
967
+ * @returns a new chain of the values before the first rejected one
968
+ * @throws Error if `predicate` is not a function
969
+ * @example
970
+ * ```ts
971
+ * IterableLinq.from([1, 2, 5, 3]).takeWhile(v => v < 4).collectToArray(); // [1, 2]
972
+ * ```
973
+ * @since 0.5.0
974
+ */
975
+ takeWhile(predicate: Predicate<T>): IIterableLinq<T>;
549
976
  /**
550
977
  * Calls `tapper` on each value as it flows through the chain, without changing it.
551
978
  * If `tapper` throws, the source is closed and the error propagates.
@@ -609,6 +1036,38 @@ export declare interface IMemoizeOptions {
609
1036
  allowPartialMemoization?: boolean;
610
1037
  }
611
1038
 
1039
+ /**
1040
+ * Tells whether `iterable` contains `value`, compared with `SameValueZero` like `Array.prototype.includes`;
1041
+ * stops and closes the source at the first match.
1042
+ * @operation `Action`
1043
+ * @param iterable - the source `Iterable`
1044
+ * @param value - the value to look for; `NaN` matches `NaN`, and `+0` matches `-0`
1045
+ * @returns `true` if `iterable` contains `value`; `false` otherwise
1046
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
1047
+ * @example
1048
+ * ```ts
1049
+ * Functions.includes([1, 2, NaN], NaN); // true
1050
+ * ```
1051
+ * @since 0.5.0
1052
+ */
1053
+ declare function includes<T>(iterable: Iterable<T>, value: T): boolean;
1054
+
1055
+ /**
1056
+ * Returns the index of the first value strictly equal (`===`) to `value`, like `Array.prototype.indexOf`;
1057
+ * stops and closes the source at the first match.
1058
+ * @operation `Action`
1059
+ * @param iterable - the source `Iterable`
1060
+ * @param value - the value to look for; `NaN` is never found, use `includes` or `findIndex` for it
1061
+ * @returns the index of the first value equal to `value`, or `-1` if there is none
1062
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
1063
+ * @example
1064
+ * ```ts
1065
+ * Functions.indexOf([1, 2, 3, 2], 2); // 1
1066
+ * ```
1067
+ * @since 0.6.0
1068
+ */
1069
+ declare function indexOf<T>(iterable: Iterable<T>, value: T): number;
1070
+
612
1071
  /**
613
1072
  * Options of `range`.
614
1073
  * @since 0.1.0
@@ -635,6 +1094,22 @@ export declare interface IRangeOptions {
635
1094
  */
636
1095
  export declare function isIterableLinq(value: unknown): value is IIterableLinq<unknown>;
637
1096
 
1097
+ /**
1098
+ * Returns the index of the last value strictly equal (`===`) to `value`, like `Array.prototype.lastIndexOf`;
1099
+ * reads the whole source.
1100
+ * @operation `Action`
1101
+ * @param iterable - the source `Iterable`
1102
+ * @param value - the value to look for; `NaN` is never found, use `findLastIndex` for it
1103
+ * @returns the index of the last value equal to `value`, or `-1` if there is none
1104
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
1105
+ * @example
1106
+ * ```ts
1107
+ * Functions.lastIndexOf([1, 2, 3, 2], 2); // 3
1108
+ * ```
1109
+ * @since 0.6.0
1110
+ */
1111
+ declare function lastIndexOf<T>(iterable: Iterable<T>, value: T): number;
1112
+
638
1113
  /**
639
1114
  * Lazily transforms each value with `mapper`.
640
1115
  * If `mapper` throws, the source is closed and the error propagates.
@@ -874,6 +1349,23 @@ declare function repeat_2<T>(value: T, count: number): Iterable<T>;
874
1349
  */
875
1350
  declare function skip<T>(iterable: Iterable<T>, count: number): Iterable<T>;
876
1351
 
1352
+ /**
1353
+ * Lazily skips the values while `predicate` returns `true`, then yields the first rejected value and all the rest.
1354
+ * After the first rejected value, `predicate` is not called again.
1355
+ * If `predicate` throws, the source is closed and the error propagates.
1356
+ * @operation `Transformation`
1357
+ * @param iterable - the source `Iterable`
1358
+ * @param predicate - called with each value and its index until it returns `false`
1359
+ * @returns a lazy, re-runnable `Iterable` of the values from the first rejected one
1360
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
1361
+ * @example
1362
+ * ```ts
1363
+ * Array.from(Functions.skipWhile([1, 2, 5, 3], v => v < 4)); // [5, 3]
1364
+ * ```
1365
+ * @since 0.5.0
1366
+ */
1367
+ declare function skipWhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
1368
+
877
1369
  /**
878
1370
  * Tells whether `iterable` contains a value; reads one value, then closes the source.
879
1371
  * @operation `Action`
@@ -919,6 +1411,41 @@ declare function some<T>(iterable: Iterable<T>, predicate: Predicate<T> | undefi
919
1411
  */
920
1412
  declare function take<T>(iterable: Iterable<T>, count: number): Iterable<T>;
921
1413
 
1414
+ /**
1415
+ * Lazily yields the values while a type guard accepts them, narrowing their type, then closes the source.
1416
+ * The source is never read past the first rejected value, which is not yielded.
1417
+ * If `predicate` throws, the source is closed and the error propagates.
1418
+ * @operation `Transformation`
1419
+ * @param iterable - the source `Iterable`
1420
+ * @param predicate - a type guard called with each value and its index; the first `false` ends the iterable
1421
+ * @returns a lazy, re-runnable `Iterable` of the narrowed values before the first rejected one
1422
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
1423
+ * @example
1424
+ * ```ts
1425
+ * const values: (number | string)[] = [1, 2, 'three', 4];
1426
+ * Array.from(Functions.takeWhile(values, (v): v is number => typeof v === 'number')); // number[], [1, 2]
1427
+ * ```
1428
+ * @since 0.5.0
1429
+ */
1430
+ declare function takeWhile<T, S extends T>(iterable: Iterable<T>, predicate: (value: T, index: number) => value is S): Iterable<S>;
1431
+
1432
+ /**
1433
+ * Lazily yields the values while `predicate` returns `true`, then closes the source.
1434
+ * The source is never read past the first rejected value, which is not yielded, so `takeWhile` can end an infinite source.
1435
+ * If `predicate` throws, the source is closed and the error propagates.
1436
+ * @operation `Transformation`
1437
+ * @param iterable - the source `Iterable`
1438
+ * @param predicate - called with each value and its index; the first `false` ends the iterable
1439
+ * @returns a lazy, re-runnable `Iterable` of the values before the first rejected one
1440
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
1441
+ * @example
1442
+ * ```ts
1443
+ * Array.from(Functions.takeWhile([1, 2, 5, 3], v => v < 4)); // [1, 2]
1444
+ * ```
1445
+ * @since 0.5.0
1446
+ */
1447
+ declare function takeWhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
1448
+
922
1449
  /**
923
1450
  * Lazily calls `tapper` on each value as it flows through, without changing it.
924
1451
  * If `tapper` throws, the source is closed and the error propagates.