iterable-linq-utility 0.0.16 → 0.1.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 +847 -0
  3. package/dist/index.d.ts +847 -14
  4. package/dist/iterable-linq-utility.js +632 -0
  5. package/dist/iterable-linq-utility.umd.cjs +1 -0
  6. package/package.json +35 -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,847 @@
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.
438
+ * @operation `Action`
439
+ * @param neutralElement - the initial accumulator (the seed)
440
+ * @param reducer - called with the accumulator, each value and its index; returns the new accumulator
441
+ * @returns the final accumulator; `neutralElement` when the chain is empty
442
+ * @throws Error if `reducer` is not a function
443
+ * @example
444
+ * ```ts
445
+ * IterableLinq.from([1, 2, 3]).reduce(0, (acc, v) => acc + v); // 6
446
+ * ```
447
+ * @since 0.0.10
448
+ */
449
+ reduce<R>(neutralElement: R, reducer: Reducer<T, R>): R;
450
+ /**
451
+ * Runs the chain until a value satisfies `predicate`, then stops and closes the source.
452
+ * @operation `Action`
453
+ * @param predicate - called with each value and its index
454
+ * @returns `true` if at least one value satisfies `predicate`; `false` when the chain is empty
455
+ * @throws Error if `predicate` is not a function
456
+ * @example
457
+ * ```ts
458
+ * IterableLinq.from([1, 2, 3]).some(v => v > 2); // true
459
+ * ```
460
+ * @since 0.0.1
461
+ */
462
+ some(predicate: Predicate<T>): boolean;
463
+ /**
464
+ * Calls `tapper` on each value as it flows through the chain, without changing it.
465
+ * If `tapper` throws, the source is closed and the error propagates.
466
+ * `tapper` runs only when the chain runs, once per value and per run.
467
+ * @operation `Tap`
468
+ * @param tapper - called with each value and its index; returns `unit()`
469
+ * @returns a new chain with the same values
470
+ * @throws Error if `tapper` is not a function
471
+ * @example
472
+ * ```ts
473
+ * IterableLinq.from([1, 2])
474
+ * .tap(v => { console.log('read', v); return unit(); })
475
+ * .map(v => v * 10)
476
+ * .collectToArray(); // logs "read 1", "read 2"; returns [10, 20]
477
+ * ```
478
+ * @since 0.0.10
479
+ */
480
+ tap(tapper: Tapper<T>): IIterableLinq<T>;
481
+ /**
482
+ * Calls `tapper` with the upstream `Iterable` each time the chain starts a run, before the first value is read.
483
+ * @operation `Tap`
484
+ * @param tapper - called with the upstream `Iterable` (the index is always 0); returns `unit()`
485
+ * @returns a new chain with the same values
486
+ * @throws Error if `tapper` is not a function
487
+ * @example
488
+ * ```ts
489
+ * const chain = IterableLinq.from([1, 2]).tapChain(() => { console.log('run'); return unit(); });
490
+ * chain.collectToArray(); // logs "run"
491
+ * chain.collectToArray(); // logs "run" again
492
+ * ```
493
+ * @since 0.0.10
494
+ */
495
+ tapChain(tapper: Tapper<Iterable<T>>): IIterableLinq<T>;
496
+ /**
497
+ * Calls `chainCreationTapper` immediately with this chain, while the chain is being built. Nothing runs.
498
+ * @operation `Tap`
499
+ * @param chainCreationTapper - called once, now, with this chain; returns `unit()`
500
+ * @returns this same chain
501
+ * @throws Error if `chainCreationTapper` is not a function
502
+ * @example
503
+ * ```ts
504
+ * let evens: IIterableLinq<number> | undefined;
505
+ * IterableLinq.fromRange(10)
506
+ * .filter(v => v % 2 === 0)
507
+ * .tapChainCreation(chain => { evens = chain; return unit(); })
508
+ * .map(v => v * 10);
509
+ * evens?.collectToArray(); // [0, 2, 4, 6, 8]
510
+ * ```
511
+ * @since 0.0.10
512
+ */
513
+ tapChainCreation(chainCreationTapper: (chain: IIterableLinq<T>) => Unit): IIterableLinq<T>;
514
+ }
515
+
516
+ /**
517
+ * Options of `memoize`.
518
+ * @since 0.1.0
519
+ */
520
+ export declare interface IMemoizeOptions {
521
+ /** `true` (default): the cache fills as far as consumers read. `false`: the first read drains the whole source. */
522
+ allowPartialMemoization?: boolean;
523
+ }
524
+
525
+ /**
526
+ * Options of `range`.
527
+ * @since 0.1.0
528
+ */
529
+ export declare interface IRangeOptions {
530
+ /** Distance between two values; defaults to 1. Only its absolute value is used: the direction comes from `start` and `end`. */
531
+ step?: number;
532
+ /** Yields the same values in reverse order; defaults to `false`. */
533
+ reverse?: boolean;
534
+ }
535
+
536
+ /**
537
+ * Tells whether `value` is a chain created by this library.
538
+ * It checks a `Symbol.for` brand instead of `instanceof`, so it also works when two copies of the library are loaded.
539
+ * It is a brand check, not a validation: do not use it to decide whether untrusted input is safe to call.
540
+ * @param value - any value
541
+ * @returns `true` if `value` is an `IIterableLinq` chain
542
+ * @example
543
+ * ```ts
544
+ * isIterableLinq(IterableLinq.from([1, 2, 3])); // true
545
+ * isIterableLinq([1, 2, 3]); // false
546
+ * ```
547
+ * @since 0.1.0
548
+ */
549
+ export declare function isIterableLinq(value: unknown): value is IIterableLinq<unknown>;
550
+
551
+ /**
552
+ * Lazily transforms each value with `mapper`.
553
+ * If `mapper` throws, the source is closed and the error propagates.
554
+ * @operation `Transformation`
555
+ * @param iterable - the source `Iterable`
556
+ * @param mapper - called with each value and its index; returns the new value
557
+ * @returns a lazy, re-runnable `Iterable` of the mapped values
558
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `mapper` is not a function
559
+ * @example
560
+ * ```ts
561
+ * Array.from(Functions.map([1, 2, 3], v => v * 10)); // [10, 20, 30]
562
+ * ```
563
+ * @since 0.0.10
564
+ */
565
+ declare function map<T, R>(iterable: Iterable<T>, mapper: Mapper<T, R>): Iterable<R>;
566
+
567
+ /**
568
+ * Callback of `map` and `flatMap`: turns a value into a new one.
569
+ * @param value - the current value
570
+ * @param index - the position of `value` in the chain, starting from 0
571
+ * @returns the new value
572
+ * @since 0.0.13
573
+ */
574
+ export declare type Mapper<T, R> = (value: T, index: number) => R;
575
+
576
+ /**
577
+ * Reads `iterable` immediately and stores its values.
578
+ * @operation `Action`
579
+ * @param iterable - the source `Iterable`
580
+ * @returns an `Iterable` over the stored values; a materialized input is returned as is
581
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
582
+ * @example
583
+ * ```ts
584
+ * const stored = Functions.materialize(Functions.map([1, 2, 3], v => v * 10)); // runs now
585
+ * Array.from(stored); // [10, 20, 30], read from the stored values
586
+ * ```
587
+ * @since 0.0.10
588
+ */
589
+ declare function materialize<T>(iterable: Iterable<T>): Iterable<T>;
590
+
591
+ /**
592
+ * Returns the greatest value; the first one among equals. `null`/`undefined` never win against a defined value.
593
+ * @operation `Action`
594
+ * @param iterable - the source `Iterable`
595
+ * @param comparer - a compare function, a key, or a list of keys; defaults to `<`
596
+ * @returns the greatest value, or `undefined` when `iterable` is empty
597
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
598
+ * @example
599
+ * ```ts
600
+ * Functions.max([3, 1, 2]); // 3
601
+ * Functions.max([{ v: 1 }, { v: 3 }], 'v'); // { v: 3 }
602
+ * ```
603
+ * @since 0.0.10
604
+ */
605
+ declare function max<T>(iterable: Iterable<T>, comparer?: Comparer<T>): T | undefined;
606
+
607
+ /**
608
+ * Caches the values of `iterable` the first time they are read, so later iterations do not run the source again.
609
+ * With partial memoization, a consumer that stops early keeps the shared source open until another consumer finishes it.
610
+ * If the source throws, every later read past the cached values throws the same error.
611
+ * @operation `Transformation`
612
+ * @param iterable - the source `Iterable`
613
+ * @param options - `allowPartialMemoization: false` reads the whole source on the first read
614
+ * @returns a lazy `Iterable` backed by the cache
615
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
616
+ * @example
617
+ * ```ts
618
+ * const cached = Functions.memoize(Functions.map(readLines(), parse));
619
+ * Array.from(cached); // reads and parses the lines
620
+ * Array.from(cached); // same values, from the cache
621
+ * ```
622
+ * @since 0.0.10
623
+ */
624
+ declare function memoize<T>(iterable: Iterable<T>, options?: IMemoizeOptions): Iterable<T>;
625
+
626
+ /**
627
+ * Returns the smallest value; the first one among equals. `null`/`undefined` never win against a defined value.
628
+ * @operation `Action`
629
+ * @param iterable - the source `Iterable`
630
+ * @param comparer - a compare function, a key, or a list of keys; defaults to `<`
631
+ * @returns the smallest value, or `undefined` when `iterable` is empty
632
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
633
+ * @example
634
+ * ```ts
635
+ * Functions.min([3, 1, 2]); // 1
636
+ * Functions.min([{ v: 1 }, { v: 3 }], 'v'); // { v: 1 }
637
+ * ```
638
+ * @since 0.0.10
639
+ */
640
+ declare function min<T>(iterable: Iterable<T>, comparer?: Comparer<T>): T | undefined;
641
+
642
+ /**
643
+ * Replaces an existing method of every chain: a library method or one added with `extend`.
644
+ * Typical use: a library release adds a method with the same name as one of your extensions,
645
+ * so `extend` throws at start-up; switch that call to `override` to keep your version.
646
+ * Only the fluent method changes: `Functions` and the library internals keep the original behaviour.
647
+ * @param name - an existing chain method; `constructor` and `Object.prototype` members are rejected
648
+ * @param implementation - the new method; `this` is the chain, typed `IIterableLinq<unknown>`
649
+ * @throws Error if `name` is not a chain method (use `extend` to add it), is empty, or `implementation` is not a function
650
+ * @example
651
+ * ```ts
652
+ * // a library release added its own `chunk`: keep your version
653
+ * override('chunk', function (size: number) {
654
+ * return IterableLinq.from({ [Symbol.iterator]: () => chunks(this, size) });
655
+ * });
656
+ * ```
657
+ * @since 0.1.0
658
+ */
659
+ export declare function override<K extends Extract<keyof IIterableLinq<unknown>, string>>(name: K, implementation: ChainMethod): void;
660
+
661
+ /**
662
+ * Callback of `filter` and `some`: tests a value.
663
+ * @param value - the current value
664
+ * @param index - the position of `value` in the chain, starting from 0
665
+ * @returns `true` if `value` satisfies the condition
666
+ * @since 0.0.13
667
+ */
668
+ export declare type Predicate<T> = (value: T, index: number) => boolean;
669
+
670
+ /**
671
+ * Returns the numbers from 0 up to, but not including, `end`, computed as `index * step`.
672
+ * The direction follows the sign of `end`; `reverse` yields the same numbers backwards; a `NaN` bound gives an empty `Iterable`.
673
+ * @operation `Transformation`
674
+ * @param end - the bound, not included
675
+ * @param options - `step` (default 1, sign ignored) and `reverse` (default `false`)
676
+ * @returns a lazy, re-runnable `Iterable` of numbers
677
+ * @throws Error if `options` is not an object, or if `step` is 0, `NaN` or infinite
678
+ * @example
679
+ * ```ts
680
+ * Array.from(Functions.range(3)); // [0, 1, 2]
681
+ * Array.from(Functions.range(3, { reverse: true })); // [2, 1, 0]
682
+ * ```
683
+ * @since 0.0.10
684
+ */
685
+ declare function range(end: number, options?: IRangeOptions): Iterable<number>;
686
+
687
+ /**
688
+ * Returns the numbers from `start` up to, but not including, `end`, computed as `start + index * step`.
689
+ * The direction follows `start` and `end`; `reverse` yields the same numbers backwards; a `NaN` bound gives an empty `Iterable`.
690
+ * @operation `Transformation`
691
+ * @param start - the first value
692
+ * @param end - the bound, not included
693
+ * @param options - `step` (default 1, sign ignored) and `reverse` (default `false`)
694
+ * @returns a lazy, re-runnable `Iterable` of numbers
695
+ * @throws Error if `options` is not an object, or if `step` is 0, `NaN` or infinite
696
+ * @example
697
+ * ```ts
698
+ * Array.from(Functions.range(1, 7, { step: 2 })); // [1, 3, 5]
699
+ * Array.from(Functions.range(5, 0)); // [5, 4, 3, 2, 1]
700
+ * ```
701
+ * @since 0.0.10
702
+ */
703
+ declare function range(start: number, end: number, options?: IRangeOptions): Iterable<number>;
704
+
705
+ /**
706
+ * Accumulates the values of `iterable` into a single result.
707
+ * @operation `Action`
708
+ * @param iterable - the source `Iterable`
709
+ * @param neutralElement - the initial accumulator (the seed)
710
+ * @param reducer - called with the accumulator, each value and its index; returns the new accumulator
711
+ * @returns the final accumulator; `neutralElement` when `iterable` is empty
712
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `reducer` is not a function
713
+ * @example
714
+ * ```ts
715
+ * Functions.reduce([1, 2, 3], 0, (acc, v) => acc + v); // 6
716
+ * ```
717
+ * @since 0.0.10
718
+ */
719
+ declare function reduce<T, R>(iterable: Iterable<T>, neutralElement: R, reducer: Reducer<T, R>): R;
720
+
721
+ /**
722
+ * Callback of `reduce`: combines the accumulator with a value.
723
+ * @param acc - the accumulator: the seed for the first value, then the result of the previous call
724
+ * @param value - the current value
725
+ * @param index - the position of `value` in the chain, starting from 0
726
+ * @returns the new accumulator
727
+ * @since 0.0.13
728
+ */
729
+ export declare type Reducer<T, R> = (acc: R, value: T, index: number) => R;
730
+
731
+ /**
732
+ * Starts a chain that yields `value` `count` times.
733
+ * @param value - the value to repeat
734
+ * @param count - how many times; must be a non-negative integer
735
+ * @returns a chain of `count` values
736
+ * @throws Error if `count` is negative, not an integer, `NaN` or `Infinity`
737
+ * @example
738
+ * ```ts
739
+ * IterableLinq.repeat(5, 3).collectToArray(); // [5, 5, 5]
740
+ * ```
741
+ * @since 0.0.10
742
+ */
743
+ export declare function repeat<T>(value: T, count: number): IIterableLinq<T>;
744
+
745
+ /**
746
+ * Returns an `Iterable` that yields `value` `count` times.
747
+ * @operation `Transformation`
748
+ * @param value - the value to repeat
749
+ * @param count - how many times; must be a non-negative integer
750
+ * @returns a lazy, re-runnable `Iterable`
751
+ * @throws Error if `count` is negative, not an integer, `NaN` or `Infinity`
752
+ * @example
753
+ * ```ts
754
+ * Array.from(Functions.repeat('a', 3)); // ['a', 'a', 'a']
755
+ * ```
756
+ * @since 0.0.10
757
+ */
758
+ declare function repeat_2<T>(value: T, count: number): Iterable<T>;
759
+
760
+ /**
761
+ * Tells whether at least one value satisfies `predicate`; stops and closes the source at the first match.
762
+ * @operation `Action`
763
+ * @param iterable - the source `Iterable`
764
+ * @param predicate - called with each value and its index
765
+ * @returns `true` if a value satisfies `predicate`; `false` when `iterable` is empty
766
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `predicate` is not a function
767
+ * @example
768
+ * ```ts
769
+ * Functions.some([1, 2, 3], v => v > 2); // true
770
+ * ```
771
+ * @since 0.0.10
772
+ */
773
+ declare function some<T>(iterable: Iterable<T>, predicate: Predicate<T>): boolean;
774
+
775
+ /**
776
+ * Lazily calls `tapper` on each value as it flows through, without changing it.
777
+ * If `tapper` throws, the source is closed and the error propagates.
778
+ * @operation `Tap`
779
+ * @param iterable - the source `Iterable`
780
+ * @param tapper - called with each value and its index; returns `unit()`
781
+ * @returns a lazy, re-runnable `Iterable` of the same values
782
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `tapper` is not a function
783
+ * @example
784
+ * ```ts
785
+ * const logged = Functions.tap([1, 2], v => { console.log(v); return unit(); });
786
+ * Array.from(logged); // logs 1, 2; returns [1, 2]
787
+ * ```
788
+ * @since 0.0.10
789
+ */
790
+ declare function tap<T>(iterable: Iterable<T>, tapper: Tapper<T>): Iterable<T>;
791
+
792
+ /**
793
+ * Calls `tapper` with `iterable` each time a new iteration starts, before the first value is read.
794
+ * @operation `Tap`
795
+ * @param iterable - the source `Iterable`
796
+ * @param tapper - called with `iterable` (the index is always 0); returns `unit()`
797
+ * @returns a lazy, re-runnable `Iterable` of the same values
798
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `tapper` is not a function
799
+ * @example
800
+ * ```ts
801
+ * const tapped = Functions.tapChain([1, 2], () => { console.log('run'); return unit(); });
802
+ * Array.from(tapped); // logs "run"
803
+ * Array.from(tapped); // logs "run" again
804
+ * ```
805
+ * @since 0.0.10
806
+ */
807
+ declare function tapChain<T>(iterable: Iterable<T>, tapper: Tapper<Iterable<T>>): Iterable<T>;
808
+
809
+ /**
810
+ * Callback of `tap` and `tapChain`: observes a value without changing it.
811
+ * @param value - the current value (for `tapChain`, the upstream `Iterable`)
812
+ * @param index - the position of `value` in the chain, starting from 0 (for `tapChain`, always 0)
813
+ * @since 0.0.13
814
+ */
815
+ export declare type Tapper<T> = (value: T, index: number) => Unit;
816
+
817
+ /**
818
+ * The type with exactly one value, `unit()`: what an action or a tapper returns when it has nothing to return.
819
+ * It is nominal: no other value (numbers, strings, objects, `\{\}`) is assignable to `Unit`.
820
+ * @since 0.0.13
821
+ */
822
+ export declare class Unit {
823
+ /**
824
+ * The only value of the type. The same frozen object as `unit()`.
825
+ * @since 0.1.0
826
+ */
827
+ static readonly instance: Unit;
828
+ private readonly unitBrand;
829
+ private constructor();
830
+ private static createFrozen;
831
+ }
832
+
833
+ /**
834
+ * Returns the only value of `Unit`. Return it from callbacks that have nothing to return.
835
+ * @returns `Unit.instance`
836
+ * @example
837
+ * ```ts
838
+ * IterableLinq.from([1, 2]).forEach(v => {
839
+ * console.log(v);
840
+ * return unit();
841
+ * });
842
+ * ```
843
+ * @since 0.0.13
844
+ */
845
+ export declare function unit(): Unit;
846
+
847
+ export { }