iterable-linq-utility 0.9.0 → 0.11.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 +486 -4
- package/dist/index.d.ts +486 -4
- package/dist/iterable-linq-utility.js +612 -254
- package/dist/iterable-linq-utility.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -86,6 +86,22 @@ declare function average<T>(iterable: Iterable<T>, selector: Mapper<T, number> |
|
|
|
86
86
|
*/
|
|
87
87
|
export declare type ChainMethod = (this: IIterableLinq<unknown>, ...args: any[]) => unknown;
|
|
88
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Lazily yields arrays of `size` values; the last array has the remaining values and can be shorter.
|
|
91
|
+
* Each array is new, and is yielded once its values have been read, so `chunk` works with infinite sources.
|
|
92
|
+
* @operation `Transformation`
|
|
93
|
+
* @param iterable - the source `Iterable`
|
|
94
|
+
* @param size - how many values in each array; must be a positive integer
|
|
95
|
+
* @returns a lazy, re-runnable `Iterable` of arrays of at most `size` values
|
|
96
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `size` is not a positive integer (`0`, fractions, `NaN` and `Infinity` included)
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* Array.from(Functions.chunk([1, 2, 3, 4, 5], 2)); // [[1, 2], [3, 4], [5]]
|
|
100
|
+
* ```
|
|
101
|
+
* @since 0.10.0
|
|
102
|
+
*/
|
|
103
|
+
declare function chunk<T>(iterable: Iterable<T>, size: number): Iterable<T[]>;
|
|
104
|
+
|
|
89
105
|
/**
|
|
90
106
|
* Collects the values of `iterable` into an `Array`.
|
|
91
107
|
* @operation `Action`
|
|
@@ -100,6 +116,55 @@ export declare type ChainMethod = (this: IIterableLinq<unknown>, ...args: any[])
|
|
|
100
116
|
*/
|
|
101
117
|
declare function collectToArray<T>(iterable: Iterable<T>): T[];
|
|
102
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Collects the values of `iterable` into a `Map`, with the key returned by `keySelector`.
|
|
121
|
+
* A later value with the same key (`SameValueZero`, as in `Map`) replaces the earlier one.
|
|
122
|
+
* If `keySelector` throws, the source is closed and the error propagates.
|
|
123
|
+
* @operation `Action`
|
|
124
|
+
* @param iterable - the source `Iterable`
|
|
125
|
+
* @param keySelector - called with each value and its index; returns the key of the value
|
|
126
|
+
* @returns a `Map` from each key to the last value with that key; an empty `Map` when `iterable` is empty
|
|
127
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `keySelector` is not a function
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* Functions.collectToMap([{ id: 1, name: 'a' }, { id: 2, name: 'b' }], v => v.id); // Map { 1 => { id: 1, name: 'a' }, 2 => { id: 2, name: 'b' } }
|
|
131
|
+
* ```
|
|
132
|
+
* @since 0.10.0
|
|
133
|
+
*/
|
|
134
|
+
declare function collectToMap<T, K>(iterable: Iterable<T>, keySelector: Mapper<T, K>): Map<K, T>;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Collects the values of `iterable` into a `Map`, with the key returned by `keySelector` and the value returned by `valueSelector`.
|
|
138
|
+
* A later value with the same key (`SameValueZero`, as in `Map`) replaces the earlier one.
|
|
139
|
+
* If `keySelector` or `valueSelector` throws, the source is closed and the error propagates.
|
|
140
|
+
* @operation `Action`
|
|
141
|
+
* @param iterable - the source `Iterable`
|
|
142
|
+
* @param keySelector - called with each value and its index; returns the key of the value
|
|
143
|
+
* @param valueSelector - called with each value and its index; returns the value to store; `undefined` stores the value itself
|
|
144
|
+
* @returns a `Map` from each key to the value selected from the last value with that key; an empty `Map` when `iterable` is empty
|
|
145
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, if `keySelector` is not a function, or if a provided `valueSelector` is not a function
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* Functions.collectToMap([{ id: 1, name: 'a' }, { id: 2, name: 'b' }], v => v.id, v => v.name); // Map { 1 => 'a', 2 => 'b' }
|
|
149
|
+
* ```
|
|
150
|
+
* @since 0.10.0
|
|
151
|
+
*/
|
|
152
|
+
declare function collectToMap<T, K, V>(iterable: Iterable<T>, keySelector: Mapper<T, K>, valueSelector: Mapper<T, V> | undefined): Map<K, V>;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Collects the values of `iterable` into a `Set`: a value equal to an earlier one (`SameValueZero`, as in `Set`) is left out.
|
|
156
|
+
* @operation `Action`
|
|
157
|
+
* @param iterable - the source `Iterable`
|
|
158
|
+
* @returns the distinct values, in the order of their first occurrence; an empty `Set` when `iterable` is empty
|
|
159
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* Functions.collectToSet([1, 2, 1, 3]); // Set { 1, 2, 3 }
|
|
163
|
+
* ```
|
|
164
|
+
* @since 0.10.0
|
|
165
|
+
*/
|
|
166
|
+
declare function collectToSet<T>(iterable: Iterable<T>): Set<T>;
|
|
167
|
+
|
|
103
168
|
/**
|
|
104
169
|
* How `min` and `max` compare two values. One of:
|
|
105
170
|
* - a compare function `(a, b) => number`: negative if `a` comes before `b`, 0 if they are equal, positive if `a` comes after `b`;
|
|
@@ -166,6 +231,22 @@ declare function count<T>(iterable: Iterable<T>): number;
|
|
|
166
231
|
*/
|
|
167
232
|
declare function count<T>(iterable: Iterable<T>, predicate: Predicate<T> | undefined): number;
|
|
168
233
|
|
|
234
|
+
/**
|
|
235
|
+
* Lazily yields the values of `iterable`, or only `value` when `iterable` is empty.
|
|
236
|
+
* @operation `Transformation`
|
|
237
|
+
* @param iterable - the source `Iterable`
|
|
238
|
+
* @param value - the value yielded when `iterable` has no values
|
|
239
|
+
* @returns a lazy, re-runnable `Iterable` of the values, or of `value` alone
|
|
240
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* Array.from(Functions.defaultIfEmpty([1, 2], 0)); // [1, 2]
|
|
244
|
+
* Array.from(Functions.defaultIfEmpty([], 0)); // [0]
|
|
245
|
+
* ```
|
|
246
|
+
* @since 0.10.0
|
|
247
|
+
*/
|
|
248
|
+
declare function defaultIfEmpty<T>(iterable: Iterable<T>, value: T): Iterable<T>;
|
|
249
|
+
|
|
169
250
|
/**
|
|
170
251
|
* Lazily yields the first value for each distinct value or selected key, in source order.
|
|
171
252
|
* Keys use `SameValueZero`, like `Set`; original values are preserved.
|
|
@@ -207,6 +288,20 @@ export declare function empty<T>(): IIterableLinq<T>;
|
|
|
207
288
|
*/
|
|
208
289
|
declare function empty_2<T>(): Iterable<T>;
|
|
209
290
|
|
|
291
|
+
/**
|
|
292
|
+
* Lazily yields `[index, value]` pairs, like `Array.prototype.entries`.
|
|
293
|
+
* @operation `Transformation`
|
|
294
|
+
* @param iterable - the source `Iterable`
|
|
295
|
+
* @returns a lazy, re-runnable `Iterable` of pairs of the index, from 0, and the value
|
|
296
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
|
|
297
|
+
* @example
|
|
298
|
+
* ```ts
|
|
299
|
+
* Array.from(Functions.entries(['a', 'b'])); // [[0, 'a'], [1, 'b']]
|
|
300
|
+
* ```
|
|
301
|
+
* @since 0.10.0
|
|
302
|
+
*/
|
|
303
|
+
declare function entries<T>(iterable: Iterable<T>): Iterable<[number, T]>;
|
|
304
|
+
|
|
210
305
|
/**
|
|
211
306
|
* Tells whether every value satisfies `predicate`; stops and closes the source at the first rejected value.
|
|
212
307
|
* @operation `Action`
|
|
@@ -224,10 +319,12 @@ declare function every<T>(iterable: Iterable<T>, predicate: Predicate<T>): boole
|
|
|
224
319
|
|
|
225
320
|
/**
|
|
226
321
|
* Adds a method to every chain, including the chains created before the call.
|
|
227
|
-
* Declare the method first by augmenting `IIterableLinq`, then register it
|
|
228
|
-
*
|
|
322
|
+
* Declare the method first by augmenting `IIterableLinq`, then register it at application start-up.
|
|
323
|
+
* Registering a name added by an earlier `extend` again replaces its implementation, so a module that runs twice
|
|
324
|
+
* (hot module replacement, a test runner that re-imports it) does not throw: the last registration wins.
|
|
325
|
+
* @param name - the method name; a new name or one added by an earlier `extend`, not a library method or an `Object.prototype` member
|
|
229
326
|
* @param implementation - the method; `this` is the chain, typed `IIterableLinq<unknown>`
|
|
230
|
-
* @throws Error if `name`
|
|
327
|
+
* @throws Error if `name` is a library method (use `override` to replace it), an `Object.prototype` member or a name used by the chain instances, is empty, or `implementation` is not a function
|
|
231
328
|
* @example
|
|
232
329
|
* ```ts
|
|
233
330
|
* declare module 'iterable-linq-utility' {
|
|
@@ -372,6 +469,34 @@ declare function findLast<T>(iterable: Iterable<T>, predicate: Predicate<T>): T
|
|
|
372
469
|
*/
|
|
373
470
|
declare function findLastIndex<T>(iterable: Iterable<T>, predicate: Predicate<T>): number;
|
|
374
471
|
|
|
472
|
+
/**
|
|
473
|
+
* Lazily flattens the nested iterables of `iterable` up to `depth` levels, like `Array.prototype.flat` for any `Iterable`.
|
|
474
|
+
* Strings, primitive or `String` objects, are not flattened. Nested arrays are read by index: their `[Symbol.iterator]` is not called.
|
|
475
|
+
* If a nested iterable throws, the iterables that contain it and the source are closed, and the error propagates.
|
|
476
|
+
* @operation `Transformation`
|
|
477
|
+
* @param iterable - the source `Iterable`
|
|
478
|
+
* @param depth - how many levels to flatten, `1` by default; a non-negative integer or `Infinity`
|
|
479
|
+
* @returns a lazy, re-runnable `Iterable` of the flattened values
|
|
480
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `depth` is not a non-negative integer or `Infinity`
|
|
481
|
+
* @example
|
|
482
|
+
* ```ts
|
|
483
|
+
* Array.from(Functions.flat([1, [2, [3]], new Set([4])])); // [1, 2, [3], 4]
|
|
484
|
+
* Array.from(Functions.flat([1, [2, [3]]], Infinity)); // [1, 2, 3]
|
|
485
|
+
* ```
|
|
486
|
+
* @since 0.10.0
|
|
487
|
+
*/
|
|
488
|
+
declare function flat<T, D extends number = 1>(iterable: Iterable<T>, depth?: D): Iterable<FlatIterable<T, D>>;
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* The type of the values of `flat` with `Depth` levels: like `FlatArray`, for any `Iterable` except strings.
|
|
492
|
+
* A `Depth` of type `number` (for example `Infinity`) gives a wide type, as `FlatArray` does.
|
|
493
|
+
* @since 0.10.0
|
|
494
|
+
*/
|
|
495
|
+
export declare type FlatIterable<T, Depth extends number> = {
|
|
496
|
+
done: T;
|
|
497
|
+
recur: T extends string | String ? T : T extends Iterable<infer U> ? FlatIterable<U, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][Depth]> : T;
|
|
498
|
+
}[Depth extends 0 ? 'done' : 'recur'];
|
|
499
|
+
|
|
375
500
|
/**
|
|
376
501
|
* Lazily maps each value to an `Iterable` and flattens the results.
|
|
377
502
|
* Each inner `Iterable` is read completely before the next value is mapped.
|
|
@@ -441,6 +566,47 @@ declare function forEachAsync<T>(iterable: Iterable<T>, action: AsyncAction<T>):
|
|
|
441
566
|
*/
|
|
442
567
|
export declare function from<T>(iterable: Iterable<T>): IIterableLinq<T>;
|
|
443
568
|
|
|
569
|
+
/**
|
|
570
|
+
* Starts a chain over the properties of `object`: its entries, its keys, its values or its property descriptors.
|
|
571
|
+
* With the default options it yields what `Object.entries` returns: the own, enumerable, string keys.
|
|
572
|
+
* - `options.yield`: `'entries'` (default) `[key, value]`, `'keys'`, `'values'`, or `'descriptors'` `[key, descriptor, owner]` without calling the getters.
|
|
573
|
+
* - `options.inherited` also reads the prototype chain, up to `Object.prototype` excluded; a key is yielded once, from the nearest object that has it, like `for…in`.
|
|
574
|
+
* - `options.nonEnumerable` also reads the non-enumerable properties, `options.symbols` the symbol keys.
|
|
575
|
+
* - Each run of the chain reads the object again: the keys of an object when the iteration reaches it, a value when it is yielded.
|
|
576
|
+
* @param object - the object to read
|
|
577
|
+
* @param options - `yield`, `inherited`, `nonEnumerable` and `symbols`
|
|
578
|
+
* @returns a chain of the properties of `object`
|
|
579
|
+
* @throws Error if `object` is not an object or a function, `options` is not an object, `yield` is not one of its values, or a flag is not a boolean
|
|
580
|
+
* @example
|
|
581
|
+
* ```ts
|
|
582
|
+
* IterableLinq.fromObject({ a: 1, b: 2 }).collectToArray(); // [['a', 1], ['b', 2]]
|
|
583
|
+
* IterableLinq.fromObject({ a: 1, b: 2 }, { yield: 'keys' }).collectToArray(); // ['a', 'b']
|
|
584
|
+
* IterableLinq.fromObject({ a: 1, b: 2 }, { yield: 'values' }).collectToArray(); // [1, 2]
|
|
585
|
+
* ```
|
|
586
|
+
* @since 0.11.0
|
|
587
|
+
*/
|
|
588
|
+
export declare function fromObject<O extends object, const Options extends IObjectOptions = IObjectDefaultOptions>(object: O, options?: Options): IIterableLinq<ObjectItem<O, Options>>;
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Returns the properties of `object`: its entries, its keys, its values or its property descriptors.
|
|
592
|
+
* With the default options it yields what `Object.entries` returns: the own, enumerable, string keys.
|
|
593
|
+
* - `inherited` also reads the prototype chain, up to `Object.prototype` excluded; a key is yielded once, from the nearest object that has it, like `for…in`.
|
|
594
|
+
* - `nonEnumerable` also reads the non-enumerable properties, `symbols` the symbol keys.
|
|
595
|
+
* - The keys of an object are read when the iteration reaches it, a value when it is yielded: each run reads the object again.
|
|
596
|
+
* @operation `Transformation`
|
|
597
|
+
* @param object - the object to read
|
|
598
|
+
* @param options - `yield` (`'entries'`, `'keys'`, `'values'` or `'descriptors'`), `inherited`, `nonEnumerable` and `symbols`
|
|
599
|
+
* @returns a lazy, re-runnable `Iterable` of the properties of `object`
|
|
600
|
+
* @throws Error if `object` is not an object or a function, `options` is not an object, `yield` is not one of its values, or a flag is not a boolean
|
|
601
|
+
* @example
|
|
602
|
+
* ```ts
|
|
603
|
+
* Array.from(Functions.fromObject({ a: 1, b: 2 })); // [['a', 1], ['b', 2]]
|
|
604
|
+
* Array.from(Functions.fromObject({ a: 1, b: 2 }, { yield: 'keys' })); // ['a', 'b']
|
|
605
|
+
* ```
|
|
606
|
+
* @since 0.11.0
|
|
607
|
+
*/
|
|
608
|
+
declare function fromObject_2<O extends object, const Options extends IObjectOptions = IObjectDefaultOptions>(object: O, options?: Options): Iterable<ObjectItem<O, Options>>;
|
|
609
|
+
|
|
444
610
|
/**
|
|
445
611
|
* Starts a chain of numbers from 0 up to, but not including, `end`.
|
|
446
612
|
* - `options.step` is the distance between two values (default 1); its sign is ignored, the direction comes from the sign of `end`.
|
|
@@ -487,20 +653,27 @@ declare namespace Functions {
|
|
|
487
653
|
append,
|
|
488
654
|
at,
|
|
489
655
|
average,
|
|
656
|
+
chunk,
|
|
490
657
|
collectToArray,
|
|
658
|
+
collectToMap,
|
|
659
|
+
collectToSet,
|
|
491
660
|
concat,
|
|
492
661
|
count,
|
|
662
|
+
defaultIfEmpty,
|
|
493
663
|
distinct,
|
|
494
664
|
empty_2 as empty,
|
|
665
|
+
entries,
|
|
495
666
|
every,
|
|
496
667
|
filter,
|
|
497
668
|
find,
|
|
498
669
|
findIndex,
|
|
499
670
|
findLast,
|
|
500
671
|
findLastIndex,
|
|
672
|
+
flat,
|
|
501
673
|
flatMap,
|
|
502
674
|
forEach,
|
|
503
675
|
forEachAsync,
|
|
676
|
+
fromObject_2 as fromObject,
|
|
504
677
|
includes,
|
|
505
678
|
indexOf,
|
|
506
679
|
join,
|
|
@@ -514,6 +687,7 @@ declare namespace Functions {
|
|
|
514
687
|
prepend,
|
|
515
688
|
range,
|
|
516
689
|
reduce,
|
|
690
|
+
reduceRight,
|
|
517
691
|
repeat_2 as repeat,
|
|
518
692
|
reverse,
|
|
519
693
|
sequenceEqual,
|
|
@@ -528,7 +702,9 @@ declare namespace Functions {
|
|
|
528
702
|
takeLast,
|
|
529
703
|
takeWhile,
|
|
530
704
|
tap,
|
|
531
|
-
tapChain
|
|
705
|
+
tapChain,
|
|
706
|
+
withValue as with,
|
|
707
|
+
zip
|
|
532
708
|
}
|
|
533
709
|
}
|
|
534
710
|
export { Functions }
|
|
@@ -623,6 +799,20 @@ export declare interface IIterableLinqBase<T> {
|
|
|
623
799
|
* @since 0.7.0
|
|
624
800
|
*/
|
|
625
801
|
average(selector: Mapper<T, number> | undefined): number | undefined;
|
|
802
|
+
/**
|
|
803
|
+
* Lazily yields arrays of `size` values; the last array has the remaining values and can be shorter.
|
|
804
|
+
* Each array is new, and is yielded once its values have been read, so `chunk` works with infinite chains.
|
|
805
|
+
* @operation `Transformation`
|
|
806
|
+
* @param size - how many values in each array; must be a positive integer
|
|
807
|
+
* @returns a lazy, re-runnable chain of arrays of at most `size` values
|
|
808
|
+
* @throws Error if `size` is not a positive integer (`0`, fractions, `NaN` and `Infinity` included)
|
|
809
|
+
* @example
|
|
810
|
+
* ```ts
|
|
811
|
+
* IterableLinq.from([1, 2, 3, 4, 5]).chunk(2).collectToArray(); // [[1, 2], [3, 4], [5]]
|
|
812
|
+
* ```
|
|
813
|
+
* @since 0.10.0
|
|
814
|
+
*/
|
|
815
|
+
chunk(size: number): IIterableLinq<T[]>;
|
|
626
816
|
/**
|
|
627
817
|
* Runs the chain and collects its values into an `Array`.
|
|
628
818
|
* @operation `Action`
|
|
@@ -634,6 +824,48 @@ export declare interface IIterableLinqBase<T> {
|
|
|
634
824
|
* @since 0.0.1
|
|
635
825
|
*/
|
|
636
826
|
collectToArray(): T[];
|
|
827
|
+
/**
|
|
828
|
+
* Runs the chain and collects its values into a `Map`, with the key returned by `keySelector`.
|
|
829
|
+
* A later value with the same key (`SameValueZero`, as in `Map`) replaces the earlier one.
|
|
830
|
+
* If `keySelector` throws, the source is closed and the error propagates.
|
|
831
|
+
* @operation `Action`
|
|
832
|
+
* @param keySelector - called with each value and its index; returns the key of the value
|
|
833
|
+
* @returns a `Map` from each key to the last value with that key; an empty `Map` when the chain is empty
|
|
834
|
+
* @throws Error if `keySelector` is not a function
|
|
835
|
+
* @example
|
|
836
|
+
* ```ts
|
|
837
|
+
* IterableLinq.from([{ id: 1, name: 'a' }, { id: 2, name: 'b' }]).collectToMap(v => v.id); // Map { 1 => { id: 1, name: 'a' }, 2 => { id: 2, name: 'b' } }
|
|
838
|
+
* ```
|
|
839
|
+
* @since 0.10.0
|
|
840
|
+
*/
|
|
841
|
+
collectToMap<K>(keySelector: Mapper<T, K>): Map<K, T>;
|
|
842
|
+
/**
|
|
843
|
+
* Runs the chain and collects its values into a `Map`, with the key returned by `keySelector` and the value returned by `valueSelector`.
|
|
844
|
+
* A later value with the same key (`SameValueZero`, as in `Map`) replaces the earlier one.
|
|
845
|
+
* If `keySelector` or `valueSelector` throws, the source is closed and the error propagates.
|
|
846
|
+
* @operation `Action`
|
|
847
|
+
* @param keySelector - called with each value and its index; returns the key of the value
|
|
848
|
+
* @param valueSelector - called with each value and its index; returns the value to store; `undefined` stores the value itself
|
|
849
|
+
* @returns a `Map` from each key to the value selected from the last value with that key; an empty `Map` when the chain is empty
|
|
850
|
+
* @throws Error if `keySelector` is not a function, or if a provided `valueSelector` is not a function
|
|
851
|
+
* @example
|
|
852
|
+
* ```ts
|
|
853
|
+
* IterableLinq.from([{ id: 1, name: 'a' }, { id: 2, name: 'b' }]).collectToMap(v => v.id, v => v.name); // Map { 1 => 'a', 2 => 'b' }
|
|
854
|
+
* ```
|
|
855
|
+
* @since 0.10.0
|
|
856
|
+
*/
|
|
857
|
+
collectToMap<K, V>(keySelector: Mapper<T, K>, valueSelector: Mapper<T, V> | undefined): Map<K, V>;
|
|
858
|
+
/**
|
|
859
|
+
* Runs the chain and collects its values into a `Set`: a value equal to an earlier one (`SameValueZero`, as in `Set`) is left out.
|
|
860
|
+
* @operation `Action`
|
|
861
|
+
* @returns the distinct values, in the order of their first occurrence; an empty `Set` when the chain is empty
|
|
862
|
+
* @example
|
|
863
|
+
* ```ts
|
|
864
|
+
* IterableLinq.from([1, 2, 1, 3]).collectToSet(); // Set { 1, 2, 3 }
|
|
865
|
+
* ```
|
|
866
|
+
* @since 0.10.0
|
|
867
|
+
*/
|
|
868
|
+
collectToSet(): Set<T>;
|
|
637
869
|
/**
|
|
638
870
|
* Yields the values of the chain, then the values of each iterable in `others`, in order.
|
|
639
871
|
* Each iterable is opened only when the previous one ends, so the iterables after an infinite chain are never read.
|
|
@@ -674,6 +906,19 @@ export declare interface IIterableLinqBase<T> {
|
|
|
674
906
|
* @since 0.5.0
|
|
675
907
|
*/
|
|
676
908
|
count(predicate: Predicate<T> | undefined): number;
|
|
909
|
+
/**
|
|
910
|
+
* Lazily yields the values of the chain, or only `value` when the chain is empty.
|
|
911
|
+
* @operation `Transformation`
|
|
912
|
+
* @param value - the value yielded when the chain has no values
|
|
913
|
+
* @returns a lazy, re-runnable chain of the values, or of `value` alone
|
|
914
|
+
* @example
|
|
915
|
+
* ```ts
|
|
916
|
+
* IterableLinq.from([1, 2]).defaultIfEmpty(0).collectToArray(); // [1, 2]
|
|
917
|
+
* IterableLinq.empty<number>().defaultIfEmpty(0).collectToArray(); // [0]
|
|
918
|
+
* ```
|
|
919
|
+
* @since 0.10.0
|
|
920
|
+
*/
|
|
921
|
+
defaultIfEmpty(value: T): IIterableLinq<T>;
|
|
677
922
|
/**
|
|
678
923
|
* Yields the first value for each distinct value or selected key, in source order.
|
|
679
924
|
* Keys use `SameValueZero`, like `Set`; original values are preserved.
|
|
@@ -690,6 +935,17 @@ export declare interface IIterableLinqBase<T> {
|
|
|
690
935
|
* @since 0.5.0
|
|
691
936
|
*/
|
|
692
937
|
distinct<K>(keySelector?: Mapper<T, K>): IIterableLinq<T>;
|
|
938
|
+
/**
|
|
939
|
+
* Lazily yields `[index, value]` pairs, like `Array.prototype.entries`.
|
|
940
|
+
* @operation `Transformation`
|
|
941
|
+
* @returns a lazy, re-runnable chain of pairs of the index, from 0, and the value
|
|
942
|
+
* @example
|
|
943
|
+
* ```ts
|
|
944
|
+
* IterableLinq.from(['a', 'b']).entries().collectToArray(); // [[0, 'a'], [1, 'b']]
|
|
945
|
+
* ```
|
|
946
|
+
* @since 0.10.0
|
|
947
|
+
*/
|
|
948
|
+
entries(): IIterableLinq<[number, T]>;
|
|
693
949
|
/**
|
|
694
950
|
* Tells whether every value satisfies `predicate`; stops and closes the source at the first rejected value.
|
|
695
951
|
* @operation `Action`
|
|
@@ -815,6 +1071,22 @@ export declare interface IIterableLinqBase<T> {
|
|
|
815
1071
|
* @since 0.6.0
|
|
816
1072
|
*/
|
|
817
1073
|
findLastIndex(predicate: Predicate<T>): number;
|
|
1074
|
+
/**
|
|
1075
|
+
* Lazily flattens the nested iterables of the chain up to `depth` levels, like `Array.prototype.flat` for any `Iterable`.
|
|
1076
|
+
* Strings, primitive or `String` objects, are not flattened. Nested arrays are read by index: their `[Symbol.iterator]` is not called.
|
|
1077
|
+
* If a nested iterable throws, the iterables that contain it and the source are closed, and the error propagates.
|
|
1078
|
+
* @operation `Transformation`
|
|
1079
|
+
* @param depth - how many levels to flatten, `1` by default; a non-negative integer or `Infinity`
|
|
1080
|
+
* @returns a lazy, re-runnable chain of the flattened values
|
|
1081
|
+
* @throws Error if `depth` is not a non-negative integer or `Infinity`
|
|
1082
|
+
* @example
|
|
1083
|
+
* ```ts
|
|
1084
|
+
* IterableLinq.from([1, [2, [3]], new Set([4])]).flat().collectToArray(); // [1, 2, [3], 4]
|
|
1085
|
+
* IterableLinq.from([1, [2, [3]]]).flat(Infinity).collectToArray(); // [1, 2, 3]
|
|
1086
|
+
* ```
|
|
1087
|
+
* @since 0.10.0
|
|
1088
|
+
*/
|
|
1089
|
+
flat<D extends number = 1>(depth?: D): IIterableLinq<FlatIterable<T, D>>;
|
|
818
1090
|
/**
|
|
819
1091
|
* Maps each value to an `Iterable` and flattens the results into one chain.
|
|
820
1092
|
* Each inner `Iterable` is read completely before the next value of the chain is mapped.
|
|
@@ -1033,6 +1305,35 @@ export declare interface IIterableLinqBase<T> {
|
|
|
1033
1305
|
* @since 0.0.10
|
|
1034
1306
|
*/
|
|
1035
1307
|
reduce<R>(neutralElement: R, reducer: Reducer<T, R>): R;
|
|
1308
|
+
/**
|
|
1309
|
+
* Runs the chain and accumulates its values into a single result, from the last value to the first, starting from the last value.
|
|
1310
|
+
* The whole chain runs before `reducer` is called.
|
|
1311
|
+
* @operation `Action`
|
|
1312
|
+
* @param reducer - called with the accumulator, each value from the second-to-last one back to the first, and its index in the chain; returns the new accumulator
|
|
1313
|
+
* @returns the final accumulator; the only value when the chain has one value, without calling `reducer`
|
|
1314
|
+
* @throws Error if the chain is empty or if `reducer` is not a function
|
|
1315
|
+
* @example
|
|
1316
|
+
* ```ts
|
|
1317
|
+
* IterableLinq.from(['a', 'b', 'c']).reduceRight((acc, v) => acc + v); // 'cba'
|
|
1318
|
+
* ```
|
|
1319
|
+
* @since 0.10.0
|
|
1320
|
+
*/
|
|
1321
|
+
reduceRight(reducer: Reducer<T, T>): T;
|
|
1322
|
+
/**
|
|
1323
|
+
* Runs the chain and accumulates its values into a single result, from the last value to the first.
|
|
1324
|
+
* The whole chain runs before `reducer` is called.
|
|
1325
|
+
* @operation `Action`
|
|
1326
|
+
* @param neutralElement - the initial accumulator (the seed)
|
|
1327
|
+
* @param reducer - called with the accumulator, each value from the last one back to the first, and its index in the chain; returns the new accumulator
|
|
1328
|
+
* @returns the final accumulator; `neutralElement` when the chain is empty
|
|
1329
|
+
* @throws Error if `reducer` is not a function
|
|
1330
|
+
* @example
|
|
1331
|
+
* ```ts
|
|
1332
|
+
* IterableLinq.from([1, 2, 3]).reduceRight('', (acc, v) => acc + v); // '321'
|
|
1333
|
+
* ```
|
|
1334
|
+
* @since 0.10.0
|
|
1335
|
+
*/
|
|
1336
|
+
reduceRight<R>(neutralElement: R, reducer: Reducer<T, R>): R;
|
|
1036
1337
|
/**
|
|
1037
1338
|
* Lazily yields the values in reverse order. Unlike `Array.prototype.reverse`, the source is not changed.
|
|
1038
1339
|
* The whole chain runs before the first value is yielded, so `reverse` does not end on an infinite chain.
|
|
@@ -1327,6 +1628,40 @@ export declare interface IIterableLinqBase<T> {
|
|
|
1327
1628
|
* @since 0.0.10
|
|
1328
1629
|
*/
|
|
1329
1630
|
tapChainCreation(chainCreationTapper: (chain: IIterableLinq<T>) => Unit): IIterableLinq<T>;
|
|
1631
|
+
/**
|
|
1632
|
+
* Lazily yields the values of the chain, with `value` in place of the value at `index`, like `Array.prototype.with`; a negative index counts from the end.
|
|
1633
|
+
* A non-negative index yields the values as they are read. A negative index yields each value once `-index` more values have been read,
|
|
1634
|
+
* keeping only those `-index` values.
|
|
1635
|
+
* @operation `Transformation`
|
|
1636
|
+
* @param index - an integer; `-1` is the last value
|
|
1637
|
+
* @param value - the value yielded in place of the value at `index`
|
|
1638
|
+
* @returns a lazy, re-runnable chain of the values, with `value` at `index`
|
|
1639
|
+
* @throws Error if `index` is not an integer (fractions, `NaN` and `Infinity` included); when the chain ends, if it has no value at `index`
|
|
1640
|
+
* @example
|
|
1641
|
+
* ```ts
|
|
1642
|
+
* IterableLinq.from([1, 2, 3]).with(1, 20).collectToArray(); // [1, 20, 3]
|
|
1643
|
+
* IterableLinq.from([1, 2, 3]).with(-1, 30).collectToArray(); // [1, 2, 30]
|
|
1644
|
+
* ```
|
|
1645
|
+
* @since 0.10.0
|
|
1646
|
+
*/
|
|
1647
|
+
with(index: number, value: T): IIterableLinq<T>;
|
|
1648
|
+
/**
|
|
1649
|
+
* Lazily yields tuples of the values at the same position in the chain and in each of `others`.
|
|
1650
|
+
* It stops at the end of the shortest iterable and closes the others; if an iterable throws, the others are closed and the error propagates.
|
|
1651
|
+
* @operation `Transformation`
|
|
1652
|
+
* @param others - the iterables read side by side with the chain
|
|
1653
|
+
* @returns a lazy, re-runnable chain of tuples, as many as the values of the shortest iterable
|
|
1654
|
+
* @throws Error if a value of `others` is missing or does not implement `[Symbol.iterator]`
|
|
1655
|
+
* @example
|
|
1656
|
+
* ```ts
|
|
1657
|
+
* IterableLinq.from([1, 2, 3]).zip(['a', 'b']).collectToArray(); // [[1, 'a'], [2, 'b']]
|
|
1658
|
+
* IterableLinq.from([1, 2]).zip(['a', 'b'], [true, false]).collectToArray(); // [[1, 'a', true], [2, 'b', false]]
|
|
1659
|
+
* ```
|
|
1660
|
+
* @since 0.10.0
|
|
1661
|
+
*/
|
|
1662
|
+
zip<U extends unknown[]>(...others: {
|
|
1663
|
+
[K in keyof U]: Iterable<U[K]>;
|
|
1664
|
+
}): IIterableLinq<[T, ...U]>;
|
|
1330
1665
|
}
|
|
1331
1666
|
|
|
1332
1667
|
/**
|
|
@@ -1370,6 +1705,32 @@ declare function includes<T>(iterable: Iterable<T>, value: T): boolean;
|
|
|
1370
1705
|
*/
|
|
1371
1706
|
declare function indexOf<T>(iterable: Iterable<T>, value: T): number;
|
|
1372
1707
|
|
|
1708
|
+
/**
|
|
1709
|
+
* The options of `fromObject` when none are given: the type of the values follows `Object.entries`.
|
|
1710
|
+
* @since 0.11.0
|
|
1711
|
+
*/
|
|
1712
|
+
export declare interface IObjectDefaultOptions extends IObjectOptions {
|
|
1713
|
+
yield?: 'entries';
|
|
1714
|
+
inherited?: false;
|
|
1715
|
+
nonEnumerable?: false;
|
|
1716
|
+
symbols?: false;
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
/**
|
|
1720
|
+
* Options of `fromObject`. The defaults read what `Object.keys` reads: the own, enumerable, string keys.
|
|
1721
|
+
* @since 0.11.0
|
|
1722
|
+
*/
|
|
1723
|
+
export declare interface IObjectOptions {
|
|
1724
|
+
/** `'entries'` (default) yields `[key, value]`, `'keys'` the keys, `'values'` the values, `'descriptors'` `[key, descriptor, owner]` without calling the getters. */
|
|
1725
|
+
yield?: ObjectYield;
|
|
1726
|
+
/** Also reads the properties of the prototype chain, up to `Object.prototype` excluded; defaults to `false`. */
|
|
1727
|
+
inherited?: boolean;
|
|
1728
|
+
/** Also reads the non-enumerable properties; defaults to `false`. */
|
|
1729
|
+
nonEnumerable?: boolean;
|
|
1730
|
+
/** Also reads the symbol keys; defaults to `false`. */
|
|
1731
|
+
symbols?: boolean;
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1373
1734
|
/**
|
|
1374
1735
|
* Options of `range`.
|
|
1375
1736
|
* @since 0.1.0
|
|
@@ -1520,6 +1881,39 @@ declare function memoize<T>(iterable: Iterable<T>, options?: IMemoizeOptions): I
|
|
|
1520
1881
|
*/
|
|
1521
1882
|
declare function min<T>(iterable: Iterable<T>, comparer?: Comparer<T>): T | undefined;
|
|
1522
1883
|
|
|
1884
|
+
/**
|
|
1885
|
+
* The type of the values of `fromObject(object, options)`, chosen by `options.yield` (default `'entries'`).
|
|
1886
|
+
* @since 0.11.0
|
|
1887
|
+
*/
|
|
1888
|
+
export declare type ObjectItem<O, Options extends IObjectOptions> = ObjectItemOf<O, Options, Options extends {
|
|
1889
|
+
yield: infer Y;
|
|
1890
|
+
} ? Y : Options extends {
|
|
1891
|
+
yield?: infer Y;
|
|
1892
|
+
} ? Y | undefined : undefined>;
|
|
1893
|
+
|
|
1894
|
+
/** Distributes over `Y`, so a `yield` typed as a union gives the union of the items; `undefined` gives the entries. */
|
|
1895
|
+
declare type ObjectItemOf<O, Options extends IObjectOptions, Y> = Y extends 'keys' ? ObjectKey<O, Options> : Y extends 'values' ? ObjectValue<O, Options> : Y extends 'descriptors' ? [ObjectKey<O, Options>, PropertyDescriptor, object] : [ObjectKey<O, Options>, ObjectValue<O, Options>];
|
|
1896
|
+
|
|
1897
|
+
/**
|
|
1898
|
+
* The type of the keys `fromObject` yields: the keys of `O` as strings, as they are at runtime,
|
|
1899
|
+
* or `string` (and `symbol`) when the options read keys that `O` does not declare.
|
|
1900
|
+
* @since 0.11.0
|
|
1901
|
+
*/
|
|
1902
|
+
export declare type ObjectKey<O, Options extends IObjectOptions> = ReadsWide<Options> extends true ? string | (ReadsSymbols<Options> extends true ? symbol : never) : `${Extract<keyof O, string | number>}` | (ReadsSymbols<Options> extends true ? Extract<keyof O, symbol> : never);
|
|
1903
|
+
|
|
1904
|
+
/**
|
|
1905
|
+
* The type of the values `fromObject` yields: the values of the keys of `O` it reads, or `unknown`
|
|
1906
|
+
* when the options read keys that `O` does not declare.
|
|
1907
|
+
* @since 0.11.0
|
|
1908
|
+
*/
|
|
1909
|
+
export declare type ObjectValue<O, Options extends IObjectOptions> = ReadsWide<Options> extends true ? unknown : O[Extract<keyof O, string | number> | (ReadsSymbols<Options> extends true ? Extract<keyof O, symbol> : never)];
|
|
1910
|
+
|
|
1911
|
+
/**
|
|
1912
|
+
* What `fromObject` yields for each property.
|
|
1913
|
+
* @since 0.11.0
|
|
1914
|
+
*/
|
|
1915
|
+
export declare type ObjectYield = 'entries' | 'keys' | 'values' | 'descriptors';
|
|
1916
|
+
|
|
1523
1917
|
/**
|
|
1524
1918
|
* Replaces an existing method of every chain: a library method or one added with `extend`.
|
|
1525
1919
|
* Typical use: a library release adds a method with the same name as one of your extensions,
|
|
@@ -1599,6 +1993,21 @@ declare function range(end: number, options?: IRangeOptions): Iterable<number>;
|
|
|
1599
1993
|
*/
|
|
1600
1994
|
declare function range(start: number, end: number, options?: IRangeOptions): Iterable<number>;
|
|
1601
1995
|
|
|
1996
|
+
declare type ReadsSymbols<Options extends IObjectOptions> = Options extends {
|
|
1997
|
+
yield?: unknown;
|
|
1998
|
+
inherited?: unknown;
|
|
1999
|
+
nonEnumerable?: unknown;
|
|
2000
|
+
symbols?: false;
|
|
2001
|
+
} ? false : true;
|
|
2002
|
+
|
|
2003
|
+
/** `true` when the options may read keys that are not in `keyof O`: inherited or non-enumerable ones. */
|
|
2004
|
+
declare type ReadsWide<Options extends IObjectOptions> = Options extends {
|
|
2005
|
+
yield?: unknown;
|
|
2006
|
+
inherited?: false;
|
|
2007
|
+
nonEnumerable?: false;
|
|
2008
|
+
symbols?: unknown;
|
|
2009
|
+
} ? false : true;
|
|
2010
|
+
|
|
1602
2011
|
/**
|
|
1603
2012
|
* Accumulates the values of `iterable` into a single result, starting from the first value.
|
|
1604
2013
|
* @operation `Action`
|
|
@@ -1640,6 +2049,39 @@ declare function reduce<T, R>(iterable: Iterable<T>, neutralElement: R, reducer:
|
|
|
1640
2049
|
*/
|
|
1641
2050
|
export declare type Reducer<T, R> = (acc: R, value: T, index: number) => R;
|
|
1642
2051
|
|
|
2052
|
+
/**
|
|
2053
|
+
* Accumulates the values of `iterable` into a single result, from the last value to the first, starting from the last value.
|
|
2054
|
+
* The whole source is read before `reducer` is called.
|
|
2055
|
+
* @operation `Action`
|
|
2056
|
+
* @param iterable - the source `Iterable`
|
|
2057
|
+
* @param reducer - called with the accumulator, each value from the second-to-last one back to the first, and its index in the source; returns the new accumulator
|
|
2058
|
+
* @returns the final accumulator; the only value when `iterable` has one value, without calling `reducer`
|
|
2059
|
+
* @throws Error if `iterable` is missing, does not implement `[Symbol.iterator]` or is empty, or if `reducer` is not a function
|
|
2060
|
+
* @example
|
|
2061
|
+
* ```ts
|
|
2062
|
+
* Functions.reduceRight(['a', 'b', 'c'], (acc, v) => acc + v); // 'cba'
|
|
2063
|
+
* ```
|
|
2064
|
+
* @since 0.10.0
|
|
2065
|
+
*/
|
|
2066
|
+
declare function reduceRight<T>(iterable: Iterable<T>, reducer: Reducer<T, T>): T;
|
|
2067
|
+
|
|
2068
|
+
/**
|
|
2069
|
+
* Accumulates the values of `iterable` into a single result, from the last value to the first.
|
|
2070
|
+
* The whole source is read before `reducer` is called.
|
|
2071
|
+
* @operation `Action`
|
|
2072
|
+
* @param iterable - the source `Iterable`
|
|
2073
|
+
* @param neutralElement - the initial accumulator (the seed)
|
|
2074
|
+
* @param reducer - called with the accumulator, each value from the last one back to the first, and its index in the source; returns the new accumulator
|
|
2075
|
+
* @returns the final accumulator; `neutralElement` when `iterable` is empty
|
|
2076
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `reducer` is not a function
|
|
2077
|
+
* @example
|
|
2078
|
+
* ```ts
|
|
2079
|
+
* Functions.reduceRight([1, 2, 3], '', (acc, v) => acc + v); // '321'
|
|
2080
|
+
* ```
|
|
2081
|
+
* @since 0.10.0
|
|
2082
|
+
*/
|
|
2083
|
+
declare function reduceRight<T, R>(iterable: Iterable<T>, neutralElement: R, reducer: Reducer<T, R>): R;
|
|
2084
|
+
|
|
1643
2085
|
/**
|
|
1644
2086
|
* Starts a chain that yields `value` `count` times.
|
|
1645
2087
|
* @param value - the value to repeat
|
|
@@ -2023,4 +2465,44 @@ export declare class Unit {
|
|
|
2023
2465
|
*/
|
|
2024
2466
|
export declare function unit(): Unit;
|
|
2025
2467
|
|
|
2468
|
+
/**
|
|
2469
|
+
* Lazily yields the values of `iterable`, with `value` in place of the value at `index`, like `Array.prototype.with`; a negative index counts from the end.
|
|
2470
|
+
* A non-negative index yields the values as they are read. A negative index yields each value once `-index` more values have been read,
|
|
2471
|
+
* keeping only those `-index` values.
|
|
2472
|
+
* Exported as `with`, a reserved word that cannot name a function declaration.
|
|
2473
|
+
* @operation `Transformation`
|
|
2474
|
+
* @param iterable - the source `Iterable`
|
|
2475
|
+
* @param index - an integer; `-1` is the last value
|
|
2476
|
+
* @param value - the value yielded in place of the value at `index`
|
|
2477
|
+
* @returns a lazy, re-runnable `Iterable` of the values, with `value` at `index`
|
|
2478
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `index` is not an integer (fractions, `NaN` and `Infinity` included);
|
|
2479
|
+
* when the source ends, if it has no value at `index`
|
|
2480
|
+
* @example
|
|
2481
|
+
* ```ts
|
|
2482
|
+
* Array.from(Functions.with([1, 2, 3], 1, 20)); // [1, 20, 3]
|
|
2483
|
+
* Array.from(Functions.with([1, 2, 3], -1, 30)); // [1, 2, 30]
|
|
2484
|
+
* ```
|
|
2485
|
+
* @since 0.10.0
|
|
2486
|
+
*/
|
|
2487
|
+
declare function withValue<T>(iterable: Iterable<T>, index: number, value: T): Iterable<T>;
|
|
2488
|
+
|
|
2489
|
+
/**
|
|
2490
|
+
* Lazily yields tuples of the values at the same position in `iterable` and in each of `others`.
|
|
2491
|
+
* It stops at the end of the shortest iterable and closes the others; if an iterable throws, the others are closed and the error propagates.
|
|
2492
|
+
* @operation `Transformation`
|
|
2493
|
+
* @param iterable - the source `Iterable`
|
|
2494
|
+
* @param others - the iterables read side by side with the source
|
|
2495
|
+
* @returns a lazy, re-runnable `Iterable` of tuples, as many as the values of the shortest iterable
|
|
2496
|
+
* @throws Error if `iterable` or a value of `others` is missing or does not implement `[Symbol.iterator]`
|
|
2497
|
+
* @example
|
|
2498
|
+
* ```ts
|
|
2499
|
+
* Array.from(Functions.zip([1, 2, 3], ['a', 'b'])); // [[1, 'a'], [2, 'b']]
|
|
2500
|
+
* Array.from(Functions.zip([1, 2], ['a', 'b'], [true, false])); // [[1, 'a', true], [2, 'b', false]]
|
|
2501
|
+
* ```
|
|
2502
|
+
* @since 0.10.0
|
|
2503
|
+
*/
|
|
2504
|
+
declare function zip<T, U extends unknown[]>(iterable: Iterable<T>, ...others: {
|
|
2505
|
+
[K in keyof U]: Iterable<U[K]>;
|
|
2506
|
+
}): Iterable<[T, ...U]>;
|
|
2507
|
+
|
|
2026
2508
|
export { }
|