iterable-linq-utility 0.6.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 +281 -0
- package/dist/index.d.ts +281 -0
- package/dist/iterable-linq-utility.js +256 -166
- package/dist/iterable-linq-utility.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -33,6 +33,37 @@ export declare type AsyncAction<T> = (value: T, index: number) => Promise<Unit>;
|
|
|
33
33
|
*/
|
|
34
34
|
declare function at<T>(iterable: Iterable<T>, index: number): T | undefined;
|
|
35
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
|
+
|
|
36
67
|
/**
|
|
37
68
|
* Implementation of a method added with `extend` or `override`. `this` is the chain the method is called on.
|
|
38
69
|
* @since 0.1.0
|
|
@@ -421,6 +452,7 @@ export declare function fromRange(start: number, end: number, options?: IRangeOp
|
|
|
421
452
|
declare namespace Functions {
|
|
422
453
|
export {
|
|
423
454
|
at,
|
|
455
|
+
average,
|
|
424
456
|
collectToArray,
|
|
425
457
|
count,
|
|
426
458
|
distinct,
|
|
@@ -436,6 +468,7 @@ declare namespace Functions {
|
|
|
436
468
|
forEachAsync,
|
|
437
469
|
includes,
|
|
438
470
|
indexOf,
|
|
471
|
+
join,
|
|
439
472
|
lastIndexOf,
|
|
440
473
|
map,
|
|
441
474
|
materialize,
|
|
@@ -446,9 +479,12 @@ declare namespace Functions {
|
|
|
446
479
|
range,
|
|
447
480
|
reduce,
|
|
448
481
|
repeat_2 as repeat,
|
|
482
|
+
sequenceEqual,
|
|
483
|
+
single,
|
|
449
484
|
skip,
|
|
450
485
|
skipWhile,
|
|
451
486
|
some,
|
|
487
|
+
sum,
|
|
452
488
|
take,
|
|
453
489
|
takeWhile,
|
|
454
490
|
tap,
|
|
@@ -508,6 +544,32 @@ export declare interface IIterableLinqBase<T> {
|
|
|
508
544
|
* @since 0.6.0
|
|
509
545
|
*/
|
|
510
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;
|
|
511
573
|
/**
|
|
512
574
|
* Runs the chain and collects its values into an `Array`.
|
|
513
575
|
* @operation `Action`
|
|
@@ -762,6 +824,20 @@ export declare interface IIterableLinqBase<T> {
|
|
|
762
824
|
* @since 0.6.0
|
|
763
825
|
*/
|
|
764
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;
|
|
765
841
|
/**
|
|
766
842
|
* Returns the index of the last value strictly equal (`===`) to `value`, like `Array.prototype.lastIndexOf`;
|
|
767
843
|
* runs the whole chain.
|
|
@@ -876,6 +952,66 @@ export declare interface IIterableLinqBase<T> {
|
|
|
876
952
|
* @since 0.0.10
|
|
877
953
|
*/
|
|
878
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;
|
|
879
1015
|
/**
|
|
880
1016
|
* Lazily skips the first `count` values and yields the rest.
|
|
881
1017
|
* @operation `Transformation`
|
|
@@ -928,6 +1064,32 @@ export declare interface IIterableLinqBase<T> {
|
|
|
928
1064
|
* @since 0.0.1
|
|
929
1065
|
*/
|
|
930
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;
|
|
931
1093
|
/**
|
|
932
1094
|
* Yields the first `count` values, then closes the source.
|
|
933
1095
|
* The source is never read past the `count`-th value, so `take` also ends an infinite chain.
|
|
@@ -1094,6 +1256,23 @@ export declare interface IRangeOptions {
|
|
|
1094
1256
|
*/
|
|
1095
1257
|
export declare function isIterableLinq(value: unknown): value is IIterableLinq<unknown>;
|
|
1096
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
|
+
|
|
1097
1276
|
/**
|
|
1098
1277
|
* Returns the index of the last value strictly equal (`===`) to `value`, like `Array.prototype.lastIndexOf`;
|
|
1099
1278
|
* reads the whole source.
|
|
@@ -1334,6 +1513,77 @@ export declare function repeat<T>(value: T, count: number): IIterableLinq<T>;
|
|
|
1334
1513
|
*/
|
|
1335
1514
|
declare function repeat_2<T>(value: T, count: number): Iterable<T>;
|
|
1336
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
|
+
|
|
1337
1587
|
/**
|
|
1338
1588
|
* Lazily skips the first `count` values and yields the rest.
|
|
1339
1589
|
* @operation `Transformation`
|
|
@@ -1395,6 +1645,37 @@ declare function some<T>(iterable: Iterable<T>): boolean;
|
|
|
1395
1645
|
*/
|
|
1396
1646
|
declare function some<T>(iterable: Iterable<T>, predicate: Predicate<T> | undefined): boolean;
|
|
1397
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
|
+
|
|
1398
1679
|
/**
|
|
1399
1680
|
* Lazily yields the first `count` values, then closes the source.
|
|
1400
1681
|
* The source is never read past the `count`-th value, so `take` also ends an infinite source.
|