iterable-linq-utility 0.5.0 → 0.7.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 +470 -0
- package/dist/index.d.ts +470 -0
- package/dist/iterable-linq-utility.js +369 -202
- package/dist/iterable-linq-utility.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -15,6 +15,55 @@ 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
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Returns the average of the values of `iterable`: their sum with `+` divided by their number; reads the whole source.
|
|
38
|
+
* The values are not checked: `NaN` makes the result `NaN`.
|
|
39
|
+
* @operation `Action`
|
|
40
|
+
* @param iterable - the source `Iterable` of numbers
|
|
41
|
+
* @returns the average of the values, `undefined` when `iterable` is empty
|
|
42
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* Functions.average([1, 2, 3, 4]); // 2.5
|
|
46
|
+
* ```
|
|
47
|
+
* @since 0.7.0
|
|
48
|
+
*/
|
|
49
|
+
declare function average(iterable: Iterable<number>): number | undefined;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Returns the average of the numbers returned by `selector` for each value; reads the whole source.
|
|
53
|
+
* If `selector` throws, the source is closed and the error propagates.
|
|
54
|
+
* @operation `Action`
|
|
55
|
+
* @param iterable - the source `Iterable`
|
|
56
|
+
* @param selector - called with each value and its index, returns the number to average; `undefined` averages the values themselves
|
|
57
|
+
* @returns the average of the selected numbers, `undefined` when `iterable` is empty
|
|
58
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if a provided `selector` is not a function
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* Functions.average(['a', 'bb', 'ccc'], v => v.length); // 2
|
|
62
|
+
* ```
|
|
63
|
+
* @since 0.7.0
|
|
64
|
+
*/
|
|
65
|
+
declare function average<T>(iterable: Iterable<T>, selector: Mapper<T, number> | undefined): number | undefined;
|
|
66
|
+
|
|
18
67
|
/**
|
|
19
68
|
* Implementation of a method added with `extend` or `override`. `this` is the chain the method is called on.
|
|
20
69
|
* @since 0.1.0
|
|
@@ -241,6 +290,55 @@ declare function find<T>(iterable: Iterable<T>, predicate: Predicate<T>): T | un
|
|
|
241
290
|
*/
|
|
242
291
|
declare function findIndex<T>(iterable: Iterable<T>, predicate: Predicate<T>): number;
|
|
243
292
|
|
|
293
|
+
/**
|
|
294
|
+
* Returns the last value accepted by a type guard, narrowing its type; reads the whole source.
|
|
295
|
+
* If `predicate` throws, the source is closed and the error propagates.
|
|
296
|
+
* @operation `Action`
|
|
297
|
+
* @param iterable - the source `Iterable`
|
|
298
|
+
* @param predicate - a type guard called with each value and its index
|
|
299
|
+
* @returns the last value accepted by `predicate`, or `undefined` if there is none
|
|
300
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
|
|
301
|
+
* @example
|
|
302
|
+
* ```ts
|
|
303
|
+
* const values: (number | string)[] = [1, 'two', 3, 'four'];
|
|
304
|
+
* Functions.findLast(values, (v): v is string => typeof v === 'string'); // string | undefined, 'four'
|
|
305
|
+
* ```
|
|
306
|
+
* @since 0.6.0
|
|
307
|
+
*/
|
|
308
|
+
declare function findLast<T, S extends T>(iterable: Iterable<T>, predicate: (value: T, index: number) => value is S): S | undefined;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Returns the last value that satisfies `predicate`; reads the whole source.
|
|
312
|
+
* If `predicate` throws, the source is closed and the error propagates.
|
|
313
|
+
* @operation `Action`
|
|
314
|
+
* @param iterable - the source `Iterable`
|
|
315
|
+
* @param predicate - called with each value and its index
|
|
316
|
+
* @returns the last value that satisfies `predicate`, or `undefined` if there is none
|
|
317
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
|
|
318
|
+
* @example
|
|
319
|
+
* ```ts
|
|
320
|
+
* Functions.findLast([1, 5, 6, 2], v => v > 4); // 6
|
|
321
|
+
* ```
|
|
322
|
+
* @since 0.6.0
|
|
323
|
+
*/
|
|
324
|
+
declare function findLast<T>(iterable: Iterable<T>, predicate: Predicate<T>): T | undefined;
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Returns the index of the last value that satisfies `predicate`; reads the whole source.
|
|
328
|
+
* If `predicate` throws, the source is closed and the error propagates.
|
|
329
|
+
* @operation `Action`
|
|
330
|
+
* @param iterable - the source `Iterable`
|
|
331
|
+
* @param predicate - called with each value and its index
|
|
332
|
+
* @returns the index of the last value that satisfies `predicate`, or `-1` if there is none
|
|
333
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
|
|
334
|
+
* @example
|
|
335
|
+
* ```ts
|
|
336
|
+
* Functions.findLastIndex([1, 5, 6, 2], v => v > 4); // 2
|
|
337
|
+
* ```
|
|
338
|
+
* @since 0.6.0
|
|
339
|
+
*/
|
|
340
|
+
declare function findLastIndex<T>(iterable: Iterable<T>, predicate: Predicate<T>): number;
|
|
341
|
+
|
|
244
342
|
/**
|
|
245
343
|
* Lazily maps each value to an `Iterable` and flattens the results.
|
|
246
344
|
* Each inner `Iterable` is read completely before the next value is mapped.
|
|
@@ -353,6 +451,8 @@ export declare function fromRange(start: number, end: number, options?: IRangeOp
|
|
|
353
451
|
|
|
354
452
|
declare namespace Functions {
|
|
355
453
|
export {
|
|
454
|
+
at,
|
|
455
|
+
average,
|
|
356
456
|
collectToArray,
|
|
357
457
|
count,
|
|
358
458
|
distinct,
|
|
@@ -361,10 +461,15 @@ declare namespace Functions {
|
|
|
361
461
|
filter,
|
|
362
462
|
find,
|
|
363
463
|
findIndex,
|
|
464
|
+
findLast,
|
|
465
|
+
findLastIndex,
|
|
364
466
|
flatMap,
|
|
365
467
|
forEach,
|
|
366
468
|
forEachAsync,
|
|
367
469
|
includes,
|
|
470
|
+
indexOf,
|
|
471
|
+
join,
|
|
472
|
+
lastIndexOf,
|
|
368
473
|
map,
|
|
369
474
|
materialize,
|
|
370
475
|
max,
|
|
@@ -374,9 +479,12 @@ declare namespace Functions {
|
|
|
374
479
|
range,
|
|
375
480
|
reduce,
|
|
376
481
|
repeat_2 as repeat,
|
|
482
|
+
sequenceEqual,
|
|
483
|
+
single,
|
|
377
484
|
skip,
|
|
378
485
|
skipWhile,
|
|
379
486
|
some,
|
|
487
|
+
sum,
|
|
380
488
|
take,
|
|
381
489
|
takeWhile,
|
|
382
490
|
tap,
|
|
@@ -420,6 +528,48 @@ export declare interface IIterableLinqBase<T> {
|
|
|
420
528
|
* @since 0.0.1
|
|
421
529
|
*/
|
|
422
530
|
[Symbol.iterator](): Iterator<T, any, undefined>;
|
|
531
|
+
/**
|
|
532
|
+
* Returns the value at `index`, like `Array.prototype.at`; a negative index counts from the end.
|
|
533
|
+
* A non-negative index runs the chain up to the value, then closes the source; a negative index runs the whole chain,
|
|
534
|
+
* keeping only the last `-index` values.
|
|
535
|
+
* @operation `Action`
|
|
536
|
+
* @param index - an integer; `-1` is the last value
|
|
537
|
+
* @returns the value at `index`, or `undefined` if the chain has no value there
|
|
538
|
+
* @throws Error if `index` is not an integer (fractions, `NaN` and `Infinity` included)
|
|
539
|
+
* @example
|
|
540
|
+
* ```ts
|
|
541
|
+
* IterableLinq.from([10, 20, 30]).at(1); // 20
|
|
542
|
+
* IterableLinq.from([10, 20, 30]).at(-1); // 30
|
|
543
|
+
* ```
|
|
544
|
+
* @since 0.6.0
|
|
545
|
+
*/
|
|
546
|
+
at(index: number): T | undefined;
|
|
547
|
+
/**
|
|
548
|
+
* Returns the average of the values of a chain of numbers: their sum with `+` divided by their number; runs the whole chain.
|
|
549
|
+
* The values are not checked: `NaN` makes the result `NaN`.
|
|
550
|
+
* @operation `Action`
|
|
551
|
+
* @returns the average of the values, `undefined` when the chain is empty
|
|
552
|
+
* @example
|
|
553
|
+
* ```ts
|
|
554
|
+
* IterableLinq.from([1, 2, 3, 4]).average(); // 2.5
|
|
555
|
+
* ```
|
|
556
|
+
* @since 0.7.0
|
|
557
|
+
*/
|
|
558
|
+
average(this: IIterableLinqBase<number>): number | undefined;
|
|
559
|
+
/**
|
|
560
|
+
* Returns the average of the numbers returned by `selector` for each value; runs the whole chain.
|
|
561
|
+
* If `selector` throws, the source is closed and the error propagates.
|
|
562
|
+
* @operation `Action`
|
|
563
|
+
* @param selector - called with each value and its index, returns the number to average; `undefined` averages the values themselves
|
|
564
|
+
* @returns the average of the selected numbers, `undefined` when the chain is empty
|
|
565
|
+
* @throws Error if a provided `selector` is not a function
|
|
566
|
+
* @example
|
|
567
|
+
* ```ts
|
|
568
|
+
* IterableLinq.from(['a', 'bb', 'ccc']).average(v => v.length); // 2
|
|
569
|
+
* ```
|
|
570
|
+
* @since 0.7.0
|
|
571
|
+
*/
|
|
572
|
+
average(selector: Mapper<T, number> | undefined): number | undefined;
|
|
423
573
|
/**
|
|
424
574
|
* Runs the chain and collects its values into an `Array`.
|
|
425
575
|
* @operation `Action`
|
|
@@ -554,6 +704,49 @@ export declare interface IIterableLinqBase<T> {
|
|
|
554
704
|
* @since 0.5.0
|
|
555
705
|
*/
|
|
556
706
|
findIndex(predicate: Predicate<T>): number;
|
|
707
|
+
/**
|
|
708
|
+
* Returns the last value accepted by a type guard, narrowing its type; runs the whole chain.
|
|
709
|
+
* If `predicate` throws, the source is closed and the error propagates.
|
|
710
|
+
* @operation `Action`
|
|
711
|
+
* @param predicate - a type guard called with each value and its index
|
|
712
|
+
* @returns the last value accepted by `predicate`, or `undefined` if there is none
|
|
713
|
+
* @throws Error if `predicate` is not a function
|
|
714
|
+
* @example
|
|
715
|
+
* ```ts
|
|
716
|
+
* const values: (number | string)[] = [1, 'two', 3, 'four'];
|
|
717
|
+
* IterableLinq.from(values).findLast((v): v is string => typeof v === 'string'); // string | undefined, 'four'
|
|
718
|
+
* ```
|
|
719
|
+
* @since 0.6.0
|
|
720
|
+
*/
|
|
721
|
+
findLast<S extends T>(predicate: (value: T, index: number) => value is S): S | undefined;
|
|
722
|
+
/**
|
|
723
|
+
* Returns the last value that satisfies `predicate`; runs the whole chain.
|
|
724
|
+
* If `predicate` throws, the source is closed and the error propagates.
|
|
725
|
+
* @operation `Action`
|
|
726
|
+
* @param predicate - called with each value and its index
|
|
727
|
+
* @returns the last value that satisfies `predicate`, or `undefined` if there is none
|
|
728
|
+
* @throws Error if `predicate` is not a function
|
|
729
|
+
* @example
|
|
730
|
+
* ```ts
|
|
731
|
+
* IterableLinq.from([1, 5, 6, 2]).findLast(v => v > 4); // 6
|
|
732
|
+
* ```
|
|
733
|
+
* @since 0.6.0
|
|
734
|
+
*/
|
|
735
|
+
findLast(predicate: Predicate<T>): T | undefined;
|
|
736
|
+
/**
|
|
737
|
+
* Returns the index of the last value that satisfies `predicate`; runs the whole chain.
|
|
738
|
+
* If `predicate` throws, the source is closed and the error propagates.
|
|
739
|
+
* @operation `Action`
|
|
740
|
+
* @param predicate - called with each value and its index
|
|
741
|
+
* @returns the index of the last value that satisfies `predicate`, or `-1` if there is none
|
|
742
|
+
* @throws Error if `predicate` is not a function
|
|
743
|
+
* @example
|
|
744
|
+
* ```ts
|
|
745
|
+
* IterableLinq.from([1, 5, 6, 2]).findLastIndex(v => v > 4); // 2
|
|
746
|
+
* ```
|
|
747
|
+
* @since 0.6.0
|
|
748
|
+
*/
|
|
749
|
+
findLastIndex(predicate: Predicate<T>): number;
|
|
557
750
|
/**
|
|
558
751
|
* Maps each value to an `Iterable` and flattens the results into one chain.
|
|
559
752
|
* Each inner `Iterable` is read completely before the next value of the chain is mapped.
|
|
@@ -618,6 +811,46 @@ export declare interface IIterableLinqBase<T> {
|
|
|
618
811
|
* @since 0.5.0
|
|
619
812
|
*/
|
|
620
813
|
includes(value: T): boolean;
|
|
814
|
+
/**
|
|
815
|
+
* Returns the index of the first value strictly equal (`===`) to `value`, like `Array.prototype.indexOf`;
|
|
816
|
+
* stops and closes the source at the first match.
|
|
817
|
+
* @operation `Action`
|
|
818
|
+
* @param value - the value to look for; `NaN` is never found, use `includes` or `findIndex` for it
|
|
819
|
+
* @returns the index of the first value equal to `value`, or `-1` if there is none
|
|
820
|
+
* @example
|
|
821
|
+
* ```ts
|
|
822
|
+
* IterableLinq.from([1, 2, 3, 2]).indexOf(2); // 1
|
|
823
|
+
* ```
|
|
824
|
+
* @since 0.6.0
|
|
825
|
+
*/
|
|
826
|
+
indexOf(value: T): number;
|
|
827
|
+
/**
|
|
828
|
+
* Joins the values of the chain in a string, like `Array.prototype.join`; runs the whole chain.
|
|
829
|
+
* `null` and `undefined` become empty strings, every other value is converted with its `toString`.
|
|
830
|
+
* @operation `Action`
|
|
831
|
+
* @param separator - the string between two values; defaults to `,`
|
|
832
|
+
* @returns the joined values, `''` when the chain is empty
|
|
833
|
+
* @example
|
|
834
|
+
* ```ts
|
|
835
|
+
* IterableLinq.from([1, 2, 3]).join(); // '1,2,3'
|
|
836
|
+
* IterableLinq.from(['a', 'b']).join(' - '); // 'a - b'
|
|
837
|
+
* ```
|
|
838
|
+
* @since 0.7.0
|
|
839
|
+
*/
|
|
840
|
+
join(separator?: string): string;
|
|
841
|
+
/**
|
|
842
|
+
* Returns the index of the last value strictly equal (`===`) to `value`, like `Array.prototype.lastIndexOf`;
|
|
843
|
+
* runs the whole chain.
|
|
844
|
+
* @operation `Action`
|
|
845
|
+
* @param value - the value to look for; `NaN` is never found, use `findLastIndex` for it
|
|
846
|
+
* @returns the index of the last value equal to `value`, or `-1` if there is none
|
|
847
|
+
* @example
|
|
848
|
+
* ```ts
|
|
849
|
+
* IterableLinq.from([1, 2, 3, 2]).lastIndexOf(2); // 3
|
|
850
|
+
* ```
|
|
851
|
+
* @since 0.6.0
|
|
852
|
+
*/
|
|
853
|
+
lastIndexOf(value: T): number;
|
|
621
854
|
/**
|
|
622
855
|
* Transforms each value with `mapper`.
|
|
623
856
|
* If `mapper` throws, the source is closed and the error propagates.
|
|
@@ -719,6 +952,66 @@ export declare interface IIterableLinqBase<T> {
|
|
|
719
952
|
* @since 0.0.10
|
|
720
953
|
*/
|
|
721
954
|
reduce<R>(neutralElement: R, reducer: Reducer<T, R>): R;
|
|
955
|
+
/**
|
|
956
|
+
* Tells whether the chain and `other` have the same values in the same order.
|
|
957
|
+
* It reads the two sources side by side, and stops and closes both at the first difference or when one ends before the other.
|
|
958
|
+
* If `equals` throws, both sources are closed and the error propagates; if a source throws, the other one is closed.
|
|
959
|
+
* @operation `Action`
|
|
960
|
+
* @param other - the `Iterable` to compare with, for example another chain
|
|
961
|
+
* @param equals - called with a value of the chain and the value of `other` at the same position; defaults to `===`
|
|
962
|
+
* @returns `true` if the two sources have the same number of values and every pair is equal
|
|
963
|
+
* @throws Error if `other` is missing or does not implement `[Symbol.iterator]`, or if a provided `equals` is not a function
|
|
964
|
+
* @example
|
|
965
|
+
* ```ts
|
|
966
|
+
* IterableLinq.from([1, 2, 3]).sequenceEqual([1, 2, 3]); // true
|
|
967
|
+
* IterableLinq.from(['a', 'bb']).sequenceEqual(['x', 'yy'], (a, b) => a.length === b.length); // true
|
|
968
|
+
* ```
|
|
969
|
+
* @since 0.7.0
|
|
970
|
+
*/
|
|
971
|
+
sequenceEqual(other: Iterable<T>, equals?: (a: T, b: T) => boolean): boolean;
|
|
972
|
+
/**
|
|
973
|
+
* Returns the only value of the chain, `undefined` if it is empty; throws if it has more than one.
|
|
974
|
+
* It stops and closes the source at the second value, so it also ends an infinite chain.
|
|
975
|
+
* @operation `Action`
|
|
976
|
+
* @returns the only value, or `undefined` when the chain is empty
|
|
977
|
+
* @throws Error if the chain contains more than one value
|
|
978
|
+
* @example
|
|
979
|
+
* ```ts
|
|
980
|
+
* IterableLinq.from([5]).single(); // 5
|
|
981
|
+
* IterableLinq.from([1, 2]).single(); // throws
|
|
982
|
+
* ```
|
|
983
|
+
* @since 0.7.0
|
|
984
|
+
*/
|
|
985
|
+
single(): T | undefined;
|
|
986
|
+
/**
|
|
987
|
+
* Returns the only value accepted by a type guard, narrowing its type, `undefined` if there is none; throws if there is more than one.
|
|
988
|
+
* It stops and closes the source at the second match. If `predicate` throws, the source is closed and the error propagates.
|
|
989
|
+
* @operation `Action`
|
|
990
|
+
* @param predicate - a type guard called with each value and its index
|
|
991
|
+
* @returns the only value accepted by `predicate`, or `undefined` if there is none
|
|
992
|
+
* @throws Error if `predicate` is not a function, or if more than one value satisfies it
|
|
993
|
+
* @example
|
|
994
|
+
* ```ts
|
|
995
|
+
* const values: (number | string)[] = [1, 'two', 3];
|
|
996
|
+
* IterableLinq.from(values).single((v): v is string => typeof v === 'string'); // string | undefined, 'two'
|
|
997
|
+
* ```
|
|
998
|
+
* @since 0.7.0
|
|
999
|
+
*/
|
|
1000
|
+
single<S extends T>(predicate: (value: T, index: number) => value is S): S | undefined;
|
|
1001
|
+
/**
|
|
1002
|
+
* Returns the only value that satisfies `predicate`, `undefined` if there is none; throws if there is more than one.
|
|
1003
|
+
* It stops and closes the source at the second match. If `predicate` throws, the source is closed and the error propagates.
|
|
1004
|
+
* @operation `Action`
|
|
1005
|
+
* @param predicate - called with each value and its index; `undefined` looks for the only value of the chain
|
|
1006
|
+
* @returns the only value that satisfies `predicate`, or `undefined` if there is none
|
|
1007
|
+
* @throws Error if a provided `predicate` is not a function, or if more than one value satisfies it
|
|
1008
|
+
* @example
|
|
1009
|
+
* ```ts
|
|
1010
|
+
* IterableLinq.from([1, 5, 2]).single(v => v > 4); // 5
|
|
1011
|
+
* ```
|
|
1012
|
+
* @since 0.7.0
|
|
1013
|
+
*/
|
|
1014
|
+
single(predicate: Predicate<T> | undefined): T | undefined;
|
|
722
1015
|
/**
|
|
723
1016
|
* Lazily skips the first `count` values and yields the rest.
|
|
724
1017
|
* @operation `Transformation`
|
|
@@ -771,6 +1064,32 @@ export declare interface IIterableLinqBase<T> {
|
|
|
771
1064
|
* @since 0.0.1
|
|
772
1065
|
*/
|
|
773
1066
|
some(predicate: Predicate<T> | undefined): boolean;
|
|
1067
|
+
/**
|
|
1068
|
+
* Sums the values of a chain of numbers with `+`; runs the whole chain.
|
|
1069
|
+
* The values are not checked: `NaN` makes the result `NaN`.
|
|
1070
|
+
* @operation `Action`
|
|
1071
|
+
* @returns the sum of the values, `0` when the chain is empty
|
|
1072
|
+
* @example
|
|
1073
|
+
* ```ts
|
|
1074
|
+
* IterableLinq.from([1, 2, 3]).sum(); // 6
|
|
1075
|
+
* ```
|
|
1076
|
+
* @since 0.7.0
|
|
1077
|
+
*/
|
|
1078
|
+
sum(this: IIterableLinqBase<number>): number;
|
|
1079
|
+
/**
|
|
1080
|
+
* Sums the numbers returned by `selector` for each value; runs the whole chain.
|
|
1081
|
+
* If `selector` throws, the source is closed and the error propagates.
|
|
1082
|
+
* @operation `Action`
|
|
1083
|
+
* @param selector - called with each value and its index, returns the number to add; `undefined` sums the values themselves
|
|
1084
|
+
* @returns the sum of the selected numbers, `0` when the chain is empty
|
|
1085
|
+
* @throws Error if a provided `selector` is not a function
|
|
1086
|
+
* @example
|
|
1087
|
+
* ```ts
|
|
1088
|
+
* IterableLinq.from(['a', 'bb', 'ccc']).sum(v => v.length); // 6
|
|
1089
|
+
* ```
|
|
1090
|
+
* @since 0.7.0
|
|
1091
|
+
*/
|
|
1092
|
+
sum(selector: Mapper<T, number> | undefined): number;
|
|
774
1093
|
/**
|
|
775
1094
|
* Yields the first `count` values, then closes the source.
|
|
776
1095
|
* The source is never read past the `count`-th value, so `take` also ends an infinite chain.
|
|
@@ -895,6 +1214,22 @@ export declare interface IMemoizeOptions {
|
|
|
895
1214
|
*/
|
|
896
1215
|
declare function includes<T>(iterable: Iterable<T>, value: T): boolean;
|
|
897
1216
|
|
|
1217
|
+
/**
|
|
1218
|
+
* Returns the index of the first value strictly equal (`===`) to `value`, like `Array.prototype.indexOf`;
|
|
1219
|
+
* stops and closes the source at the first match.
|
|
1220
|
+
* @operation `Action`
|
|
1221
|
+
* @param iterable - the source `Iterable`
|
|
1222
|
+
* @param value - the value to look for; `NaN` is never found, use `includes` or `findIndex` for it
|
|
1223
|
+
* @returns the index of the first value equal to `value`, or `-1` if there is none
|
|
1224
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
|
|
1225
|
+
* @example
|
|
1226
|
+
* ```ts
|
|
1227
|
+
* Functions.indexOf([1, 2, 3, 2], 2); // 1
|
|
1228
|
+
* ```
|
|
1229
|
+
* @since 0.6.0
|
|
1230
|
+
*/
|
|
1231
|
+
declare function indexOf<T>(iterable: Iterable<T>, value: T): number;
|
|
1232
|
+
|
|
898
1233
|
/**
|
|
899
1234
|
* Options of `range`.
|
|
900
1235
|
* @since 0.1.0
|
|
@@ -921,6 +1256,39 @@ export declare interface IRangeOptions {
|
|
|
921
1256
|
*/
|
|
922
1257
|
export declare function isIterableLinq(value: unknown): value is IIterableLinq<unknown>;
|
|
923
1258
|
|
|
1259
|
+
/**
|
|
1260
|
+
* Joins the values of `iterable` in a string, like `Array.prototype.join`; reads the whole source.
|
|
1261
|
+
* `null` and `undefined` become empty strings, every other value is converted with its `toString`.
|
|
1262
|
+
* @operation `Action`
|
|
1263
|
+
* @param iterable - the source `Iterable`
|
|
1264
|
+
* @param separator - the string between two values; defaults to `,`
|
|
1265
|
+
* @returns the joined values, `''` when `iterable` is empty
|
|
1266
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
|
|
1267
|
+
* @example
|
|
1268
|
+
* ```ts
|
|
1269
|
+
* Functions.join([1, 2, 3]); // '1,2,3'
|
|
1270
|
+
* Functions.join(['a', 'b'], ' - '); // 'a - b'
|
|
1271
|
+
* ```
|
|
1272
|
+
* @since 0.7.0
|
|
1273
|
+
*/
|
|
1274
|
+
declare function join<T>(iterable: Iterable<T>, separator?: string): string;
|
|
1275
|
+
|
|
1276
|
+
/**
|
|
1277
|
+
* Returns the index of the last value strictly equal (`===`) to `value`, like `Array.prototype.lastIndexOf`;
|
|
1278
|
+
* reads the whole source.
|
|
1279
|
+
* @operation `Action`
|
|
1280
|
+
* @param iterable - the source `Iterable`
|
|
1281
|
+
* @param value - the value to look for; `NaN` is never found, use `findLastIndex` for it
|
|
1282
|
+
* @returns the index of the last value equal to `value`, or `-1` if there is none
|
|
1283
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
|
|
1284
|
+
* @example
|
|
1285
|
+
* ```ts
|
|
1286
|
+
* Functions.lastIndexOf([1, 2, 3, 2], 2); // 3
|
|
1287
|
+
* ```
|
|
1288
|
+
* @since 0.6.0
|
|
1289
|
+
*/
|
|
1290
|
+
declare function lastIndexOf<T>(iterable: Iterable<T>, value: T): number;
|
|
1291
|
+
|
|
924
1292
|
/**
|
|
925
1293
|
* Lazily transforms each value with `mapper`.
|
|
926
1294
|
* If `mapper` throws, the source is closed and the error propagates.
|
|
@@ -1145,6 +1513,77 @@ export declare function repeat<T>(value: T, count: number): IIterableLinq<T>;
|
|
|
1145
1513
|
*/
|
|
1146
1514
|
declare function repeat_2<T>(value: T, count: number): Iterable<T>;
|
|
1147
1515
|
|
|
1516
|
+
/**
|
|
1517
|
+
* Tells whether `iterable` and `other` have the same values in the same order.
|
|
1518
|
+
* It reads the two sources side by side, and stops and closes both at the first difference or when one ends before the other.
|
|
1519
|
+
* If `equals` throws, both sources are closed and the error propagates; if a source throws, the other one is closed.
|
|
1520
|
+
* @operation `Action`
|
|
1521
|
+
* @param iterable - the source `Iterable`
|
|
1522
|
+
* @param other - the `Iterable` to compare with
|
|
1523
|
+
* @param equals - called with a value of `iterable` and the value of `other` at the same position; defaults to `===`
|
|
1524
|
+
* @returns `true` if the two sources have the same number of values and every pair is equal
|
|
1525
|
+
* @throws Error if `iterable` or `other` is missing or does not implement `[Symbol.iterator]`, or if a provided `equals` is not a function
|
|
1526
|
+
* @example
|
|
1527
|
+
* ```ts
|
|
1528
|
+
* Functions.sequenceEqual([1, 2, 3], [1, 2, 3]); // true
|
|
1529
|
+
* Functions.sequenceEqual([1, 2], [1, 2, 3]); // false
|
|
1530
|
+
* Functions.sequenceEqual([{ id: 1 }], [{ id: 1 }], (a, b) => a.id === b.id); // true
|
|
1531
|
+
* ```
|
|
1532
|
+
* @since 0.7.0
|
|
1533
|
+
*/
|
|
1534
|
+
declare function sequenceEqual<T>(iterable: Iterable<T>, other: Iterable<T>, equals?: (a: T, b: T) => boolean): boolean;
|
|
1535
|
+
|
|
1536
|
+
/**
|
|
1537
|
+
* Returns the only value of `iterable`, `undefined` if it is empty; throws if it has more than one.
|
|
1538
|
+
* It stops and closes the source at the second value, so it also ends on an infinite source.
|
|
1539
|
+
* @operation `Action`
|
|
1540
|
+
* @param iterable - the source `Iterable`
|
|
1541
|
+
* @returns the only value, or `undefined` when `iterable` is empty
|
|
1542
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if it contains more than one value
|
|
1543
|
+
* @example
|
|
1544
|
+
* ```ts
|
|
1545
|
+
* Functions.single([5]); // 5
|
|
1546
|
+
* Functions.single([]); // undefined
|
|
1547
|
+
* Functions.single([1, 2]); // throws
|
|
1548
|
+
* ```
|
|
1549
|
+
* @since 0.7.0
|
|
1550
|
+
*/
|
|
1551
|
+
declare function single<T>(iterable: Iterable<T>): T | undefined;
|
|
1552
|
+
|
|
1553
|
+
/**
|
|
1554
|
+
* Returns the only value accepted by a type guard, narrowing its type, `undefined` if there is none; throws if there is more than one.
|
|
1555
|
+
* It stops and closes the source at the second match. If `predicate` throws, the source is closed and the error propagates.
|
|
1556
|
+
* @operation `Action`
|
|
1557
|
+
* @param iterable - the source `Iterable`
|
|
1558
|
+
* @param predicate - a type guard called with each value and its index
|
|
1559
|
+
* @returns the only value accepted by `predicate`, or `undefined` if there is none
|
|
1560
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, if `predicate` is not a function, or if more than one value satisfies it
|
|
1561
|
+
* @example
|
|
1562
|
+
* ```ts
|
|
1563
|
+
* const values: (number | string)[] = [1, 'two', 3];
|
|
1564
|
+
* Functions.single(values, (v): v is string => typeof v === 'string'); // string | undefined, 'two'
|
|
1565
|
+
* ```
|
|
1566
|
+
* @since 0.7.0
|
|
1567
|
+
*/
|
|
1568
|
+
declare function single<T, S extends T>(iterable: Iterable<T>, predicate: (value: T, index: number) => value is S): S | undefined;
|
|
1569
|
+
|
|
1570
|
+
/**
|
|
1571
|
+
* Returns the only value that satisfies `predicate`, `undefined` if there is none; throws if there is more than one.
|
|
1572
|
+
* It stops and closes the source at the second match. If `predicate` throws, the source is closed and the error propagates.
|
|
1573
|
+
* @operation `Action`
|
|
1574
|
+
* @param iterable - the source `Iterable`
|
|
1575
|
+
* @param predicate - called with each value and its index; `undefined` looks for the only value of `iterable`
|
|
1576
|
+
* @returns the only value that satisfies `predicate`, or `undefined` if there is none
|
|
1577
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, if a provided `predicate` is not a function, or if more than one value satisfies it
|
|
1578
|
+
* @example
|
|
1579
|
+
* ```ts
|
|
1580
|
+
* Functions.single([1, 5, 2], v => v > 4); // 5
|
|
1581
|
+
* Functions.single([5, 6], v => v > 4); // throws
|
|
1582
|
+
* ```
|
|
1583
|
+
* @since 0.7.0
|
|
1584
|
+
*/
|
|
1585
|
+
declare function single<T>(iterable: Iterable<T>, predicate: Predicate<T> | undefined): T | undefined;
|
|
1586
|
+
|
|
1148
1587
|
/**
|
|
1149
1588
|
* Lazily skips the first `count` values and yields the rest.
|
|
1150
1589
|
* @operation `Transformation`
|
|
@@ -1206,6 +1645,37 @@ declare function some<T>(iterable: Iterable<T>): boolean;
|
|
|
1206
1645
|
*/
|
|
1207
1646
|
declare function some<T>(iterable: Iterable<T>, predicate: Predicate<T> | undefined): boolean;
|
|
1208
1647
|
|
|
1648
|
+
/**
|
|
1649
|
+
* Sums the values of `iterable` with `+`; reads the whole source.
|
|
1650
|
+
* The values are not checked: `NaN` makes the result `NaN`.
|
|
1651
|
+
* @operation `Action`
|
|
1652
|
+
* @param iterable - the source `Iterable` of numbers
|
|
1653
|
+
* @returns the sum of the values, `0` when `iterable` is empty
|
|
1654
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
|
|
1655
|
+
* @example
|
|
1656
|
+
* ```ts
|
|
1657
|
+
* Functions.sum([1, 2, 3]); // 6
|
|
1658
|
+
* ```
|
|
1659
|
+
* @since 0.7.0
|
|
1660
|
+
*/
|
|
1661
|
+
declare function sum(iterable: Iterable<number>): number;
|
|
1662
|
+
|
|
1663
|
+
/**
|
|
1664
|
+
* Sums the numbers returned by `selector` for each value; reads the whole source.
|
|
1665
|
+
* If `selector` throws, the source is closed and the error propagates.
|
|
1666
|
+
* @operation `Action`
|
|
1667
|
+
* @param iterable - the source `Iterable`
|
|
1668
|
+
* @param selector - called with each value and its index, returns the number to add; `undefined` sums the values themselves
|
|
1669
|
+
* @returns the sum of the selected numbers, `0` when `iterable` is empty
|
|
1670
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if a provided `selector` is not a function
|
|
1671
|
+
* @example
|
|
1672
|
+
* ```ts
|
|
1673
|
+
* Functions.sum(['a', 'bb', 'ccc'], v => v.length); // 6
|
|
1674
|
+
* ```
|
|
1675
|
+
* @since 0.7.0
|
|
1676
|
+
*/
|
|
1677
|
+
declare function sum<T>(iterable: Iterable<T>, selector: Mapper<T, number> | undefined): number;
|
|
1678
|
+
|
|
1209
1679
|
/**
|
|
1210
1680
|
* Lazily yields the first `count` values, then closes the source.
|
|
1211
1681
|
* The source is never read past the `count`-th value, so `take` also ends an infinite source.
|