iterable-linq-utility 0.0.16 → 0.2.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.
Files changed (40) hide show
  1. package/README.md +72 -0
  2. package/dist/index.d.cts +875 -0
  3. package/dist/index.d.ts +875 -14
  4. package/dist/iterable-linq-utility.js +641 -0
  5. package/dist/iterable-linq-utility.umd.cjs +1 -0
  6. package/package.json +42 -24
  7. package/dist/collections/index.d.ts +0 -2
  8. package/dist/collections/linkedList.d.ts +0 -24
  9. package/dist/functions/collectToArray.d.ts +0 -1
  10. package/dist/functions/empty.d.ts +0 -1
  11. package/dist/functions/filter.d.ts +0 -9
  12. package/dist/functions/flatMap.d.ts +0 -8
  13. package/dist/functions/forEach.d.ts +0 -3
  14. package/dist/functions/index.d.ts +0 -23
  15. package/dist/functions/map.d.ts +0 -9
  16. package/dist/functions/materialize.d.ts +0 -7
  17. package/dist/functions/max.d.ts +0 -9
  18. package/dist/functions/memoize.d.ts +0 -11
  19. package/dist/functions/min.d.ts +0 -9
  20. package/dist/functions/range.d.ts +0 -7
  21. package/dist/functions/reduce.d.ts +0 -10
  22. package/dist/functions/repeat.d.ts +0 -1
  23. package/dist/functions/some.d.ts +0 -9
  24. package/dist/functions/tap.d.ts +0 -9
  25. package/dist/functions/tapChain.d.ts +0 -9
  26. package/dist/interable-linq-utility.js +0 -708
  27. package/dist/interable-linq-utility.umd.cjs +0 -1
  28. package/dist/linqIterable.d.ts +0 -45
  29. package/dist/types/action.d.ts +0 -3
  30. package/dist/types/comparer.d.ts +0 -4
  31. package/dist/types/index.d.ts +0 -7
  32. package/dist/types/mapper.d.ts +0 -1
  33. package/dist/types/predicate.d.ts +0 -1
  34. package/dist/types/reducer.d.ts +0 -1
  35. package/dist/types/tapper.d.ts +0 -2
  36. package/dist/types/unit.d.ts +0 -3
  37. package/dist/utils/index.d.ts +0 -4
  38. package/dist/utils/iteratorResults.d.ts +0 -4
  39. package/dist/utils/utils.d.ts +0 -56
  40. package/dist/utils/validations.d.ts +0 -5
@@ -0,0 +1,875 @@
1
+ /**
2
+ * Callback of `forEach`: a side effect run on each value. It returns `unit()` because it has nothing to return.
3
+ * @param value - the current value
4
+ * @param index - the position of `value` in the chain, starting from 0
5
+ * @since 0.0.13
6
+ */
7
+ export declare type Action<T> = (value: T, index: number) => Unit;
8
+
9
+ /**
10
+ * Callback of `forEachAsync`: an async side effect run on each value.
11
+ * @param value - the current value
12
+ * @param index - the position of `value` in the chain, starting from 0
13
+ * @returns a promise of `unit()`; a rejection stops `forEachAsync`
14
+ * @since 0.0.13
15
+ */
16
+ export declare type AsyncAction<T> = (value: T, index: number) => Promise<Unit>;
17
+
18
+ /**
19
+ * Implementation of a method added with `extend` or `override`. `this` is the chain the method is called on.
20
+ * @since 0.1.0
21
+ */
22
+ export declare type ChainMethod = (this: IIterableLinq<unknown>, ...args: any[]) => unknown;
23
+
24
+ /**
25
+ * Collects the values of `iterable` into an `Array`.
26
+ * @operation `Action`
27
+ * @param iterable - the source `Iterable`
28
+ * @returns the values, in order; an empty array when `iterable` is empty
29
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
30
+ * @example
31
+ * ```ts
32
+ * Functions.collectToArray(new Set([1, 2, 3])); // [1, 2, 3]
33
+ * ```
34
+ * @since 0.0.10
35
+ */
36
+ declare function collectToArray<T>(iterable: Iterable<T>): T[];
37
+
38
+ /**
39
+ * How `min` and `max` compare two values. One of:
40
+ * - a compare function `(a, b) => number`: negative if `a` comes before `b`, 0 if they are equal, positive if `a` comes after `b`;
41
+ * - a key: the values are compared by that property, with `<`;
42
+ * - a list of keys: the values are compared by the first key, ties are broken by the next one, and so on.
43
+ * @example
44
+ * ```ts
45
+ * IterableLinq.from(people).max((a, b) => a.age - b.age);
46
+ * IterableLinq.from(people).max('age');
47
+ * IterableLinq.from(people).min(['lastName', 'firstName']);
48
+ * ```
49
+ * @since 0.0.13
50
+ */
51
+ export declare type Comparer<T> = ComparerFunction<T> | ComparingProps<T>;
52
+
53
+ declare type ComparerFunction<T> = (a: T, b: T) => number;
54
+
55
+ declare type ComparingProps<T> = keyof T | Array<keyof T>;
56
+
57
+ /**
58
+ * Starts a chain with no values.
59
+ * @returns an empty chain
60
+ * @example
61
+ * ```ts
62
+ * IterableLinq.empty<number>().collectToArray(); // []
63
+ * ```
64
+ * @since 0.0.10
65
+ */
66
+ export declare function empty<T>(): IIterableLinq<T>;
67
+
68
+ /**
69
+ * Returns an `Iterable` with no values.
70
+ * @operation `Transformation`
71
+ * @returns an empty, re-runnable `Iterable`
72
+ * @example
73
+ * ```ts
74
+ * Array.from(Functions.empty<number>()); // []
75
+ * ```
76
+ * @since 0.0.10
77
+ */
78
+ declare function empty_2<T>(): Iterable<T>;
79
+
80
+ /**
81
+ * Adds a method to every chain, including the chains created before the call.
82
+ * Declare the method first by augmenting `IIterableLinq`, then register it once, at application start-up.
83
+ * @param name - the method name; it must not exist yet (library methods, earlier extensions, `Object.prototype` members)
84
+ * @param implementation - the method; `this` is the chain, typed `IIterableLinq<unknown>`
85
+ * @throws Error if `name` already exists (use `override` to replace it), is empty, or `implementation` is not a function
86
+ * @example
87
+ * ```ts
88
+ * declare module 'iterable-linq-utility' {
89
+ * interface IIterableLinq<T> { chunk(size: number): IIterableLinq<T[]>; }
90
+ * }
91
+ * extend('chunk', function (size: number) {
92
+ * // `this` is the chain the method is called on; chunks() is your generator function
93
+ * return IterableLinq.from({ [Symbol.iterator]: () => chunks(this, size) });
94
+ * });
95
+ * IterableLinq.from([1, 2, 3]).chunk(2).collectToArray(); // [[1, 2], [3]]
96
+ * ```
97
+ * @since 0.1.0
98
+ */
99
+ export declare function extend<K extends Extract<keyof IIterableLinq<unknown>, string>>(name: K, implementation: ChainMethod): void;
100
+
101
+ /**
102
+ * Lazily keeps only the values that satisfy `predicate`.
103
+ * If `predicate` throws, the source is closed and the error propagates.
104
+ * @operation `Transformation`
105
+ * @param iterable - the source `Iterable`
106
+ * @param predicate - called with each value and its index; return `true` to keep the value
107
+ * @returns a lazy, re-runnable `Iterable` of the kept values
108
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
109
+ * @example
110
+ * ```ts
111
+ * Array.from(Functions.filter([1, 2, 3, 4], v => v % 2 === 0)); // [2, 4]
112
+ * ```
113
+ * @since 0.0.10
114
+ */
115
+ declare function filter<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
116
+
117
+ /**
118
+ * Lazily maps each value to an `Iterable` and flattens the results.
119
+ * Each inner `Iterable` is read completely before the next value is mapped.
120
+ * Inner arrays are read by index, as in `Array.prototype.flatMap`: their `[Symbol.iterator]` is not called.
121
+ * If `mapper` or an inner `Iterable` throws, the source is closed and the error propagates.
122
+ * @operation `Transformation`
123
+ * @param iterable - the source `Iterable`
124
+ * @param mapper - called with each value and its index; returns the `Iterable` to flatten
125
+ * @returns a lazy, re-runnable `Iterable` of the flattened values
126
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `mapper` is not a function
127
+ * @example
128
+ * ```ts
129
+ * Array.from(Functions.flatMap([1, 2], v => [v, v * 10])); // [1, 10, 2, 20]
130
+ * ```
131
+ * @since 0.0.11
132
+ */
133
+ declare function flatMap<T, R>(iterable: Iterable<T>, mapper: Mapper<T, Iterable<R>>): Iterable<R>;
134
+
135
+ /**
136
+ * Calls `action` on each value of `iterable`. If `action` throws, the source is closed and the error propagates.
137
+ * @operation `Action`
138
+ * @param iterable - the source `Iterable`
139
+ * @param action - called with each value and its index; returns `unit()`
140
+ * @returns `unit()`
141
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `action` is not a function
142
+ * @example
143
+ * ```ts
144
+ * Functions.forEach([1, 2], v => {
145
+ * console.log(v); // 1, 2
146
+ * return unit();
147
+ * });
148
+ * ```
149
+ * @since 0.0.11
150
+ */
151
+ declare function forEach<T>(iterable: Iterable<T>, action: Action<T>): Unit;
152
+
153
+ /**
154
+ * Calls the async `action` on each value of `iterable`, sequentially: each action starts after the previous one has settled.
155
+ * The first rejection stops the iteration and closes the source. Works on infinite sources.
156
+ * @operation `Action`
157
+ * @param iterable - the source `Iterable`
158
+ * @param action - called with each value and its index; returns a promise of `unit()`
159
+ * @returns a promise resolved with `unit()` after the last action, or rejected with the first error
160
+ * @throws Error, as a rejection of the returned promise, if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `action` is not a function
161
+ * @example
162
+ * ```ts
163
+ * await Functions.forEachAsync(['a.txt', 'b.txt'], async file => {
164
+ * await upload(file); // 'b.txt' starts after 'a.txt' has finished
165
+ * return unit();
166
+ * });
167
+ * ```
168
+ * @since 0.0.11
169
+ */
170
+ declare function forEachAsync<T>(iterable: Iterable<T>, action: AsyncAction<T>): Promise<Unit>;
171
+
172
+ /**
173
+ * Starts a chain over any `Iterable` (array, string, Set, Map, generator…). The source is not copied.
174
+ * @param iterable - the source of the chain
175
+ * @returns a chain over `iterable`; each run of the chain iterates `iterable` again
176
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
177
+ * @example
178
+ * ```ts
179
+ * IterableLinq.from([1, 2, 3]).map(v => v * 2).collectToArray(); // [2, 4, 6]
180
+ * IterableLinq.from('abc').collectToArray(); // ['a', 'b', 'c']
181
+ * ```
182
+ * @since 0.0.1
183
+ */
184
+ export declare function from<T>(iterable: Iterable<T>): IIterableLinq<T>;
185
+
186
+ /**
187
+ * Starts a chain of numbers from 0 up to, but not including, `end`.
188
+ * - `options.step` is the distance between two values (default 1); its sign is ignored, the direction comes from the sign of `end`.
189
+ * - `options.reverse` yields the same numbers in reverse order.
190
+ * - Values are computed as `index * step`, so decimal steps do not accumulate rounding errors.
191
+ * - A `NaN` bound gives an empty chain.
192
+ * @param end - the bound, not included
193
+ * @param options - `step` and `reverse`
194
+ * @returns a chain of numbers
195
+ * @throws Error if `options` is not an object, or if `step` is 0, `NaN` or infinite
196
+ * @example
197
+ * ```ts
198
+ * IterableLinq.fromRange(3).collectToArray(); // [0, 1, 2]
199
+ * IterableLinq.fromRange(3, { reverse: true }).collectToArray(); // [2, 1, 0]
200
+ * IterableLinq.fromRange(-3).collectToArray(); // [0, -1, -2]
201
+ * ```
202
+ * @since 0.0.10
203
+ */
204
+ export declare function fromRange(end: number, options?: IRangeOptions): IIterableLinq<number>;
205
+
206
+ /**
207
+ * Starts a chain of numbers from `start` up to, but not including, `end`.
208
+ * - `options.step` is the distance between two values (default 1); its sign is ignored, the direction comes from `start` and `end`.
209
+ * - `options.reverse` yields the same numbers in reverse order.
210
+ * - Values are computed as `start + index * step`, so decimal steps do not accumulate rounding errors.
211
+ * - A `NaN` bound gives an empty chain.
212
+ * @param start - the first value
213
+ * @param end - the bound, not included
214
+ * @param options - `step` and `reverse`
215
+ * @returns a chain of numbers
216
+ * @throws Error if `options` is not an object, or if `step` is 0, `NaN` or infinite
217
+ * @example
218
+ * ```ts
219
+ * IterableLinq.fromRange(1, 7, { step: 2 }).collectToArray(); // [1, 3, 5]
220
+ * IterableLinq.fromRange(5, 0).collectToArray(); // [5, 4, 3, 2, 1]
221
+ * IterableLinq.fromRange(0, 1, { step: 0.25 }).collectToArray(); // [0, 0.25, 0.5, 0.75]
222
+ * ```
223
+ * @since 0.0.10
224
+ */
225
+ export declare function fromRange(start: number, end: number, options?: IRangeOptions): IIterableLinq<number>;
226
+
227
+ declare namespace Functions {
228
+ export {
229
+ collectToArray,
230
+ empty_2 as empty,
231
+ filter,
232
+ flatMap,
233
+ forEach,
234
+ forEachAsync,
235
+ map,
236
+ materialize,
237
+ max,
238
+ memoize,
239
+ getMemoizeDefaultOptions,
240
+ min,
241
+ range,
242
+ reduce,
243
+ repeat_2 as repeat,
244
+ some,
245
+ tap,
246
+ tapChain
247
+ }
248
+ }
249
+ export { Functions }
250
+
251
+ /**
252
+ * Returns the options `memoize` uses when none are given.
253
+ * @returns a new object: `{ allowPartialMemoization: true }`
254
+ * @since 0.0.16
255
+ */
256
+ declare function getMemoizeDefaultOptions(): IMemoizeOptions;
257
+
258
+ /**
259
+ * Fluent wrapper over an `Iterable`: every call builds a lazy, re-runnable operations chain.
260
+ * Transformations and taps return a new `IIterableLinq`; actions run the chain and return a result.
261
+ * Create chains with `from`, `fromRange`, `repeat` and `empty`; recognise them with `isIterableLinq`.
262
+ *
263
+ * Augment this interface (not `IIterableLinqBase`) to declare the methods you add with `extend`.
264
+ * @since 0.0.10
265
+ */
266
+ export declare interface IIterableLinq<T> extends IIterableLinqBase<T> {
267
+ }
268
+
269
+ /**
270
+ * The operations provided by the library on every chain. See `IIterableLinq`.
271
+ * @since 0.1.0
272
+ */
273
+ export declare interface IIterableLinqBase<T> {
274
+ /**
275
+ * Starts a new run of the chain. Each call iterates the source again, so a chain can be consumed many times.
276
+ * @returns a new iterator over the values of the chain
277
+ * @example
278
+ * ```ts
279
+ * const chain = IterableLinq.from([1, 2, 3]).map(v => v * 10);
280
+ * [...chain]; // [10, 20, 30]
281
+ * for (const value of chain) console.log(value); // 10, 20, 30
282
+ * ```
283
+ * @since 0.0.1
284
+ */
285
+ [Symbol.iterator](): Iterator<T, any, undefined>;
286
+ /**
287
+ * Runs the chain and collects its values into an `Array`.
288
+ * @operation `Action`
289
+ * @returns the values of the chain, in order; an empty array when the chain is empty
290
+ * @example
291
+ * ```ts
292
+ * IterableLinq.from([1, 2, 3]).map(v => v * 10).collectToArray(); // [10, 20, 30]
293
+ * ```
294
+ * @since 0.0.1
295
+ */
296
+ collectToArray(): T[];
297
+ /**
298
+ * Keeps only the values that satisfy `predicate`.
299
+ * If `predicate` throws, the source is closed and the error propagates.
300
+ * @operation `Transformation`
301
+ * @param predicate - called with each value and its index; return `true` to keep the value
302
+ * @returns a new chain with the kept values
303
+ * @throws Error if `predicate` is not a function
304
+ * @example
305
+ * ```ts
306
+ * IterableLinq.from([1, 2, 3, 4]).filter(v => v % 2 === 0).collectToArray(); // [2, 4]
307
+ * ```
308
+ * @since 0.0.1
309
+ */
310
+ filter(predicate: Predicate<T>): IIterableLinq<T>;
311
+ /**
312
+ * Maps each value to an `Iterable` and flattens the results into one chain.
313
+ * Each inner `Iterable` is read completely before the next value of the chain is mapped.
314
+ * Inner arrays are read by index, as in `Array.prototype.flatMap`: their `[Symbol.iterator]` is not called.
315
+ * If `mapper` or an inner `Iterable` throws, the source is closed and the error propagates.
316
+ * @operation `Transformation`
317
+ * @param mapper - called with each value and its index; returns the `Iterable` to flatten
318
+ * @returns a new chain with the flattened values
319
+ * @throws Error if `mapper` is not a function
320
+ * @example
321
+ * ```ts
322
+ * IterableLinq.from([1, 2]).flatMap(v => [v, v * 10]).collectToArray(); // [1, 10, 2, 20]
323
+ * ```
324
+ * @since 0.0.11
325
+ */
326
+ flatMap<R>(mapper: Mapper<T, Iterable<R>>): IIterableLinq<R>;
327
+ /**
328
+ * Runs the chain and calls `action` on each value.
329
+ * If `action` throws, the source is closed and the error propagates.
330
+ * @operation `Action`
331
+ * @param action - called with each value and its index; returns `unit()`
332
+ * @returns `unit()`
333
+ * @throws Error if `action` is not a function
334
+ * @example
335
+ * ```ts
336
+ * IterableLinq.from([1, 2]).forEach(v => {
337
+ * console.log(v); // 1, 2
338
+ * return unit();
339
+ * });
340
+ * ```
341
+ * @since 0.0.11
342
+ */
343
+ forEach(action: Action<T>): Unit;
344
+ /**
345
+ * Runs the chain and calls the async `action` on each value.
346
+ * The actions run sequentially: each one starts after the previous one has settled.
347
+ * The first rejection stops the iteration and closes the source. Works on infinite sources.
348
+ * @operation `Action`
349
+ * @param action - called with each value and its index; returns a promise of `unit()`
350
+ * @returns a promise resolved with `unit()` after the last action, or rejected with the first error
351
+ * @throws Error, as a rejection of the returned promise, if `action` is not a function
352
+ * @example
353
+ * ```ts
354
+ * await IterableLinq.from(['a.txt', 'b.txt']).forEachAsync(async file => {
355
+ * await upload(file); // 'b.txt' starts after 'a.txt' has finished
356
+ * return unit();
357
+ * });
358
+ * ```
359
+ * @since 0.0.11
360
+ */
361
+ forEachAsync(action: AsyncAction<T>): Promise<Unit>;
362
+ /**
363
+ * Transforms each value with `mapper`.
364
+ * If `mapper` throws, the source is closed and the error propagates.
365
+ * @operation `Transformation`
366
+ * @param mapper - called with each value and its index; returns the new value
367
+ * @returns a new chain with the mapped values
368
+ * @throws Error if `mapper` is not a function
369
+ * @example
370
+ * ```ts
371
+ * IterableLinq.from([1, 2, 3, 4]).map(v => v * 10).collectToArray(); // [10, 20, 30, 40]
372
+ * ```
373
+ * @since 0.0.1
374
+ */
375
+ map<R>(mapper: Mapper<T, R>): IIterableLinq<R>;
376
+ /**
377
+ * Runs the chain immediately and stores its values, so later chains start from the stored values
378
+ * instead of running the source again. Materializing a materialized chain does not copy the values again.
379
+ * @operation `Action`
380
+ * @returns a new chain over the stored values
381
+ * @example
382
+ * ```ts
383
+ * const stored = IterableLinq.fromRange(1_000_000).filter(isPrime).materialize(); // runs now
384
+ * stored.max(); // reads the stored values, does not run filter again
385
+ * ```
386
+ * @since 0.0.1
387
+ */
388
+ materialize(): IIterableLinq<T>;
389
+ /**
390
+ * Runs the chain and returns its greatest value. Among equal values the first one wins;
391
+ * `null` and `undefined` never win against a defined value.
392
+ * @operation `Action`
393
+ * @param comparer - a compare function, a key, or a list of keys to compare by; defaults to `<`
394
+ * @returns the greatest value, or `undefined` when the chain is empty
395
+ * @example
396
+ * ```ts
397
+ * IterableLinq.from([3, 1, 2]).max(); // 3
398
+ * IterableLinq.from([{ v: 1 }, { v: 3 }]).max('v'); // { v: 3 }
399
+ * IterableLinq.empty<number>().max(); // undefined
400
+ * ```
401
+ * @since 0.0.1
402
+ */
403
+ max(comparer?: Comparer<T>): T | undefined;
404
+ /**
405
+ * Caches the values the first time they are read, so later runs do not run the chain again.
406
+ * With partial memoization (the default) the cache fills as far as consumers read;
407
+ * a consumer that stops early keeps the source open until another consumer finishes it.
408
+ * If the source throws, every later read past the cached values throws the same error.
409
+ * @operation `Transformation`
410
+ * @param options - `allowPartialMemoization: false` reads the whole source on the first read
411
+ * @returns a new chain backed by the cache
412
+ * @example
413
+ * ```ts
414
+ * const cached = IterableLinq.from(readLines()).map(parse).memoize();
415
+ * cached.collectToArray(); // reads and parses the lines
416
+ * cached.collectToArray(); // same values, from the cache
417
+ * ```
418
+ * @since 0.0.1
419
+ */
420
+ memoize(options?: IMemoizeOptions): IIterableLinq<T>;
421
+ /**
422
+ * Runs the chain and returns its smallest value. Among equal values the first one wins;
423
+ * `null` and `undefined` never win against a defined value.
424
+ * @operation `Action`
425
+ * @param comparer - a compare function, a key, or a list of keys to compare by; defaults to `<`
426
+ * @returns the smallest value, or `undefined` when the chain is empty
427
+ * @example
428
+ * ```ts
429
+ * IterableLinq.from([3, 1, 2]).min(); // 1
430
+ * IterableLinq.from([{ v: 1 }, { v: 3 }]).min('v'); // { v: 1 }
431
+ * IterableLinq.empty<number>().min(); // undefined
432
+ * ```
433
+ * @since 0.0.8
434
+ */
435
+ min(comparer?: Comparer<T>): T | undefined;
436
+ /**
437
+ * Runs the chain and accumulates its values into a single result, starting from the first value.
438
+ * @operation `Action`
439
+ * @param reducer - called with the accumulator, each value from the second one and its index (starting at 1); returns the new accumulator
440
+ * @returns the final accumulator; the only value when the chain has one value, without calling `reducer`
441
+ * @throws Error if the chain is empty or if `reducer` is not a function
442
+ * @example
443
+ * ```ts
444
+ * IterableLinq.from([3, 7, 2]).reduce((acc, v) => (v > acc ? v : acc)); // 7
445
+ * ```
446
+ * @since 0.2.0
447
+ */
448
+ reduce(reducer: Reducer<T, T>): T;
449
+ /**
450
+ * Runs the chain and accumulates its values into a single result.
451
+ * @operation `Action`
452
+ * @param neutralElement - the initial accumulator (the seed)
453
+ * @param reducer - called with the accumulator, each value and its index; returns the new accumulator
454
+ * @returns the final accumulator; `neutralElement` when the chain is empty
455
+ * @throws Error if `reducer` is not a function
456
+ * @example
457
+ * ```ts
458
+ * IterableLinq.from([1, 2, 3]).reduce(0, (acc, v) => acc + v); // 6
459
+ * ```
460
+ * @since 0.0.10
461
+ */
462
+ reduce<R>(neutralElement: R, reducer: Reducer<T, R>): R;
463
+ /**
464
+ * Runs the chain until a value satisfies `predicate`, then stops and closes the source.
465
+ * @operation `Action`
466
+ * @param predicate - called with each value and its index
467
+ * @returns `true` if at least one value satisfies `predicate`; `false` when the chain is empty
468
+ * @throws Error if `predicate` is not a function
469
+ * @example
470
+ * ```ts
471
+ * IterableLinq.from([1, 2, 3]).some(v => v > 2); // true
472
+ * ```
473
+ * @since 0.0.1
474
+ */
475
+ some(predicate: Predicate<T>): boolean;
476
+ /**
477
+ * Calls `tapper` on each value as it flows through the chain, without changing it.
478
+ * If `tapper` throws, the source is closed and the error propagates.
479
+ * `tapper` runs only when the chain runs, once per value and per run.
480
+ * @operation `Tap`
481
+ * @param tapper - called with each value and its index; returns `unit()`
482
+ * @returns a new chain with the same values
483
+ * @throws Error if `tapper` is not a function
484
+ * @example
485
+ * ```ts
486
+ * IterableLinq.from([1, 2])
487
+ * .tap(v => { console.log('read', v); return unit(); })
488
+ * .map(v => v * 10)
489
+ * .collectToArray(); // logs "read 1", "read 2"; returns [10, 20]
490
+ * ```
491
+ * @since 0.0.10
492
+ */
493
+ tap(tapper: Tapper<T>): IIterableLinq<T>;
494
+ /**
495
+ * Calls `tapper` with the upstream `Iterable` each time the chain starts a run, before the first value is read.
496
+ * @operation `Tap`
497
+ * @param tapper - called with the upstream `Iterable` (the index is always 0); returns `unit()`
498
+ * @returns a new chain with the same values
499
+ * @throws Error if `tapper` is not a function
500
+ * @example
501
+ * ```ts
502
+ * const chain = IterableLinq.from([1, 2]).tapChain(() => { console.log('run'); return unit(); });
503
+ * chain.collectToArray(); // logs "run"
504
+ * chain.collectToArray(); // logs "run" again
505
+ * ```
506
+ * @since 0.0.10
507
+ */
508
+ tapChain(tapper: Tapper<Iterable<T>>): IIterableLinq<T>;
509
+ /**
510
+ * Calls `chainCreationTapper` immediately with this chain, while the chain is being built. Nothing runs.
511
+ * @operation `Tap`
512
+ * @param chainCreationTapper - called once, now, with this chain; returns `unit()`
513
+ * @returns this same chain
514
+ * @throws Error if `chainCreationTapper` is not a function
515
+ * @example
516
+ * ```ts
517
+ * let evens: IIterableLinq<number> | undefined;
518
+ * IterableLinq.fromRange(10)
519
+ * .filter(v => v % 2 === 0)
520
+ * .tapChainCreation(chain => { evens = chain; return unit(); })
521
+ * .map(v => v * 10);
522
+ * evens?.collectToArray(); // [0, 2, 4, 6, 8]
523
+ * ```
524
+ * @since 0.0.10
525
+ */
526
+ tapChainCreation(chainCreationTapper: (chain: IIterableLinq<T>) => Unit): IIterableLinq<T>;
527
+ }
528
+
529
+ /**
530
+ * Options of `memoize`.
531
+ * @since 0.1.0
532
+ */
533
+ export declare interface IMemoizeOptions {
534
+ /** `true` (default): the cache fills as far as consumers read. `false`: the first read drains the whole source. */
535
+ allowPartialMemoization?: boolean;
536
+ }
537
+
538
+ /**
539
+ * Options of `range`.
540
+ * @since 0.1.0
541
+ */
542
+ export declare interface IRangeOptions {
543
+ /** Distance between two values; defaults to 1. Only its absolute value is used: the direction comes from `start` and `end`. */
544
+ step?: number;
545
+ /** Yields the same values in reverse order; defaults to `false`. */
546
+ reverse?: boolean;
547
+ }
548
+
549
+ /**
550
+ * Tells whether `value` is a chain created by this library.
551
+ * It checks a `Symbol.for` brand instead of `instanceof`, so it also works when two copies of the library are loaded.
552
+ * It is a brand check, not a validation: do not use it to decide whether untrusted input is safe to call.
553
+ * @param value - any value
554
+ * @returns `true` if `value` is an `IIterableLinq` chain
555
+ * @example
556
+ * ```ts
557
+ * isIterableLinq(IterableLinq.from([1, 2, 3])); // true
558
+ * isIterableLinq([1, 2, 3]); // false
559
+ * ```
560
+ * @since 0.1.0
561
+ */
562
+ export declare function isIterableLinq(value: unknown): value is IIterableLinq<unknown>;
563
+
564
+ /**
565
+ * Lazily transforms each value with `mapper`.
566
+ * If `mapper` throws, the source is closed and the error propagates.
567
+ * @operation `Transformation`
568
+ * @param iterable - the source `Iterable`
569
+ * @param mapper - called with each value and its index; returns the new value
570
+ * @returns a lazy, re-runnable `Iterable` of the mapped values
571
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `mapper` is not a function
572
+ * @example
573
+ * ```ts
574
+ * Array.from(Functions.map([1, 2, 3], v => v * 10)); // [10, 20, 30]
575
+ * ```
576
+ * @since 0.0.10
577
+ */
578
+ declare function map<T, R>(iterable: Iterable<T>, mapper: Mapper<T, R>): Iterable<R>;
579
+
580
+ /**
581
+ * Callback of `map` and `flatMap`: turns a value into a new one.
582
+ * @param value - the current value
583
+ * @param index - the position of `value` in the chain, starting from 0
584
+ * @returns the new value
585
+ * @since 0.0.13
586
+ */
587
+ export declare type Mapper<T, R> = (value: T, index: number) => R;
588
+
589
+ /**
590
+ * Reads `iterable` immediately and stores its values.
591
+ * @operation `Action`
592
+ * @param iterable - the source `Iterable`
593
+ * @returns an `Iterable` over the stored values; a materialized input is returned as is
594
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
595
+ * @example
596
+ * ```ts
597
+ * const stored = Functions.materialize(Functions.map([1, 2, 3], v => v * 10)); // runs now
598
+ * Array.from(stored); // [10, 20, 30], read from the stored values
599
+ * ```
600
+ * @since 0.0.10
601
+ */
602
+ declare function materialize<T>(iterable: Iterable<T>): Iterable<T>;
603
+
604
+ /**
605
+ * Returns the greatest value; the first one among equals. `null`/`undefined` never win against a defined value.
606
+ * @operation `Action`
607
+ * @param iterable - the source `Iterable`
608
+ * @param comparer - a compare function, a key, or a list of keys; defaults to `<`
609
+ * @returns the greatest value, or `undefined` when `iterable` is empty
610
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
611
+ * @example
612
+ * ```ts
613
+ * Functions.max([3, 1, 2]); // 3
614
+ * Functions.max([{ v: 1 }, { v: 3 }], 'v'); // { v: 3 }
615
+ * ```
616
+ * @since 0.0.10
617
+ */
618
+ declare function max<T>(iterable: Iterable<T>, comparer?: Comparer<T>): T | undefined;
619
+
620
+ /**
621
+ * Caches the values of `iterable` the first time they are read, so later iterations do not run the source again.
622
+ * With partial memoization, a consumer that stops early keeps the shared source open until another consumer finishes it.
623
+ * If the source throws, every later read past the cached values throws the same error.
624
+ * @operation `Transformation`
625
+ * @param iterable - the source `Iterable`
626
+ * @param options - `allowPartialMemoization: false` reads the whole source on the first read
627
+ * @returns a lazy `Iterable` backed by the cache
628
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
629
+ * @example
630
+ * ```ts
631
+ * const cached = Functions.memoize(Functions.map(readLines(), parse));
632
+ * Array.from(cached); // reads and parses the lines
633
+ * Array.from(cached); // same values, from the cache
634
+ * ```
635
+ * @since 0.0.10
636
+ */
637
+ declare function memoize<T>(iterable: Iterable<T>, options?: IMemoizeOptions): Iterable<T>;
638
+
639
+ /**
640
+ * Returns the smallest value; the first one among equals. `null`/`undefined` never win against a defined value.
641
+ * @operation `Action`
642
+ * @param iterable - the source `Iterable`
643
+ * @param comparer - a compare function, a key, or a list of keys; defaults to `<`
644
+ * @returns the smallest value, or `undefined` when `iterable` is empty
645
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
646
+ * @example
647
+ * ```ts
648
+ * Functions.min([3, 1, 2]); // 1
649
+ * Functions.min([{ v: 1 }, { v: 3 }], 'v'); // { v: 1 }
650
+ * ```
651
+ * @since 0.0.10
652
+ */
653
+ declare function min<T>(iterable: Iterable<T>, comparer?: Comparer<T>): T | undefined;
654
+
655
+ /**
656
+ * Replaces an existing method of every chain: a library method or one added with `extend`.
657
+ * Typical use: a library release adds a method with the same name as one of your extensions,
658
+ * so `extend` throws at start-up; switch that call to `override` to keep your version.
659
+ * Only the fluent method changes: `Functions` and the library internals keep the original behaviour.
660
+ * @param name - an existing chain method; `constructor` and `Object.prototype` members are rejected
661
+ * @param implementation - the new method; `this` is the chain, typed `IIterableLinq<unknown>`
662
+ * @throws Error if `name` is not a chain method (use `extend` to add it), is empty, or `implementation` is not a function
663
+ * @example
664
+ * ```ts
665
+ * // a library release added its own `chunk`: keep your version
666
+ * override('chunk', function (size: number) {
667
+ * return IterableLinq.from({ [Symbol.iterator]: () => chunks(this, size) });
668
+ * });
669
+ * ```
670
+ * @since 0.1.0
671
+ */
672
+ export declare function override<K extends Extract<keyof IIterableLinq<unknown>, string>>(name: K, implementation: ChainMethod): void;
673
+
674
+ /**
675
+ * Callback of `filter` and `some`: tests a value.
676
+ * @param value - the current value
677
+ * @param index - the position of `value` in the chain, starting from 0
678
+ * @returns `true` if `value` satisfies the condition
679
+ * @since 0.0.13
680
+ */
681
+ export declare type Predicate<T> = (value: T, index: number) => boolean;
682
+
683
+ /**
684
+ * Returns the numbers from 0 up to, but not including, `end`, computed as `index * step`.
685
+ * The direction follows the sign of `end`; `reverse` yields the same numbers backwards; a `NaN` bound gives an empty `Iterable`.
686
+ * @operation `Transformation`
687
+ * @param end - the bound, not included
688
+ * @param options - `step` (default 1, sign ignored) and `reverse` (default `false`)
689
+ * @returns a lazy, re-runnable `Iterable` of numbers
690
+ * @throws Error if `options` is not an object, or if `step` is 0, `NaN` or infinite
691
+ * @example
692
+ * ```ts
693
+ * Array.from(Functions.range(3)); // [0, 1, 2]
694
+ * Array.from(Functions.range(3, { reverse: true })); // [2, 1, 0]
695
+ * ```
696
+ * @since 0.0.10
697
+ */
698
+ declare function range(end: number, options?: IRangeOptions): Iterable<number>;
699
+
700
+ /**
701
+ * Returns the numbers from `start` up to, but not including, `end`, computed as `start + index * step`.
702
+ * The direction follows `start` and `end`; `reverse` yields the same numbers backwards; a `NaN` bound gives an empty `Iterable`.
703
+ * @operation `Transformation`
704
+ * @param start - the first value
705
+ * @param end - the bound, not included
706
+ * @param options - `step` (default 1, sign ignored) and `reverse` (default `false`)
707
+ * @returns a lazy, re-runnable `Iterable` of numbers
708
+ * @throws Error if `options` is not an object, or if `step` is 0, `NaN` or infinite
709
+ * @example
710
+ * ```ts
711
+ * Array.from(Functions.range(1, 7, { step: 2 })); // [1, 3, 5]
712
+ * Array.from(Functions.range(5, 0)); // [5, 4, 3, 2, 1]
713
+ * ```
714
+ * @since 0.0.10
715
+ */
716
+ declare function range(start: number, end: number, options?: IRangeOptions): Iterable<number>;
717
+
718
+ /**
719
+ * Accumulates the values of `iterable` into a single result, starting from the first value.
720
+ * @operation `Action`
721
+ * @param iterable - the source `Iterable`
722
+ * @param reducer - called with the accumulator, each value from the second one and its index (starting at 1); returns the new accumulator
723
+ * @returns the final accumulator; the only value when `iterable` has one value, without calling `reducer`
724
+ * @throws Error if `iterable` is missing, does not implement `[Symbol.iterator]` or is empty, or if `reducer` is not a function
725
+ * @example
726
+ * ```ts
727
+ * Functions.reduce([3, 7, 2], (acc, v) => (v > acc ? v : acc)); // 7
728
+ * ```
729
+ * @since 0.2.0
730
+ */
731
+ declare function reduce<T>(iterable: Iterable<T>, reducer: Reducer<T, T>): T;
732
+
733
+ /**
734
+ * Accumulates the values of `iterable` into a single result.
735
+ * @operation `Action`
736
+ * @param iterable - the source `Iterable`
737
+ * @param neutralElement - the initial accumulator (the seed)
738
+ * @param reducer - called with the accumulator, each value and its index; returns the new accumulator
739
+ * @returns the final accumulator; `neutralElement` when `iterable` is empty
740
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `reducer` is not a function
741
+ * @example
742
+ * ```ts
743
+ * Functions.reduce([1, 2, 3], 0, (acc, v) => acc + v); // 6
744
+ * ```
745
+ * @since 0.0.10
746
+ */
747
+ declare function reduce<T, R>(iterable: Iterable<T>, neutralElement: R, reducer: Reducer<T, R>): R;
748
+
749
+ /**
750
+ * Callback of `reduce`: combines the accumulator with a value.
751
+ * @param acc - the accumulator: the seed for the first value, then the result of the previous call
752
+ * @param value - the current value
753
+ * @param index - the position of `value` in the chain, starting from 0
754
+ * @returns the new accumulator
755
+ * @since 0.0.13
756
+ */
757
+ export declare type Reducer<T, R> = (acc: R, value: T, index: number) => R;
758
+
759
+ /**
760
+ * Starts a chain that yields `value` `count` times.
761
+ * @param value - the value to repeat
762
+ * @param count - how many times; must be a non-negative integer
763
+ * @returns a chain of `count` values
764
+ * @throws Error if `count` is negative, not an integer, `NaN` or `Infinity`
765
+ * @example
766
+ * ```ts
767
+ * IterableLinq.repeat(5, 3).collectToArray(); // [5, 5, 5]
768
+ * ```
769
+ * @since 0.0.10
770
+ */
771
+ export declare function repeat<T>(value: T, count: number): IIterableLinq<T>;
772
+
773
+ /**
774
+ * Returns an `Iterable` that yields `value` `count` times.
775
+ * @operation `Transformation`
776
+ * @param value - the value to repeat
777
+ * @param count - how many times; must be a non-negative integer
778
+ * @returns a lazy, re-runnable `Iterable`
779
+ * @throws Error if `count` is negative, not an integer, `NaN` or `Infinity`
780
+ * @example
781
+ * ```ts
782
+ * Array.from(Functions.repeat('a', 3)); // ['a', 'a', 'a']
783
+ * ```
784
+ * @since 0.0.10
785
+ */
786
+ declare function repeat_2<T>(value: T, count: number): Iterable<T>;
787
+
788
+ /**
789
+ * Tells whether at least one value satisfies `predicate`; stops and closes the source at the first match.
790
+ * @operation `Action`
791
+ * @param iterable - the source `Iterable`
792
+ * @param predicate - called with each value and its index
793
+ * @returns `true` if a value satisfies `predicate`; `false` when `iterable` is empty
794
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
795
+ * @example
796
+ * ```ts
797
+ * Functions.some([1, 2, 3], v => v > 2); // true
798
+ * ```
799
+ * @since 0.0.10
800
+ */
801
+ declare function some<T>(iterable: Iterable<T>, predicate: Predicate<T>): boolean;
802
+
803
+ /**
804
+ * Lazily calls `tapper` on each value as it flows through, without changing it.
805
+ * If `tapper` throws, the source is closed and the error propagates.
806
+ * @operation `Tap`
807
+ * @param iterable - the source `Iterable`
808
+ * @param tapper - called with each value and its index; returns `unit()`
809
+ * @returns a lazy, re-runnable `Iterable` of the same values
810
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `tapper` is not a function
811
+ * @example
812
+ * ```ts
813
+ * const logged = Functions.tap([1, 2], v => { console.log(v); return unit(); });
814
+ * Array.from(logged); // logs 1, 2; returns [1, 2]
815
+ * ```
816
+ * @since 0.0.10
817
+ */
818
+ declare function tap<T>(iterable: Iterable<T>, tapper: Tapper<T>): Iterable<T>;
819
+
820
+ /**
821
+ * Calls `tapper` with `iterable` each time a new iteration starts, before the first value is read.
822
+ * @operation `Tap`
823
+ * @param iterable - the source `Iterable`
824
+ * @param tapper - called with `iterable` (the index is always 0); returns `unit()`
825
+ * @returns a lazy, re-runnable `Iterable` of the same values
826
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `tapper` is not a function
827
+ * @example
828
+ * ```ts
829
+ * const tapped = Functions.tapChain([1, 2], () => { console.log('run'); return unit(); });
830
+ * Array.from(tapped); // logs "run"
831
+ * Array.from(tapped); // logs "run" again
832
+ * ```
833
+ * @since 0.0.10
834
+ */
835
+ declare function tapChain<T>(iterable: Iterable<T>, tapper: Tapper<Iterable<T>>): Iterable<T>;
836
+
837
+ /**
838
+ * Callback of `tap` and `tapChain`: observes a value without changing it.
839
+ * @param value - the current value (for `tapChain`, the upstream `Iterable`)
840
+ * @param index - the position of `value` in the chain, starting from 0 (for `tapChain`, always 0)
841
+ * @since 0.0.13
842
+ */
843
+ export declare type Tapper<T> = (value: T, index: number) => Unit;
844
+
845
+ /**
846
+ * The type with exactly one value, `unit()`: what an action or a tapper returns when it has nothing to return.
847
+ * It is nominal: no other value (numbers, strings, objects, `\{\}`) is assignable to `Unit`.
848
+ * @since 0.0.13
849
+ */
850
+ export declare class Unit {
851
+ /**
852
+ * The only value of the type. The same frozen object as `unit()`.
853
+ * @since 0.1.0
854
+ */
855
+ static readonly instance: Unit;
856
+ private readonly unitBrand;
857
+ private constructor();
858
+ private static createFrozen;
859
+ }
860
+
861
+ /**
862
+ * Returns the only value of `Unit`. Return it from callbacks that have nothing to return.
863
+ * @returns `Unit.instance`
864
+ * @example
865
+ * ```ts
866
+ * IterableLinq.from([1, 2]).forEach(v => {
867
+ * console.log(v);
868
+ * return unit();
869
+ * });
870
+ * ```
871
+ * @since 0.0.13
872
+ */
873
+ export declare function unit(): Unit;
874
+
875
+ export { }