@johnhenry/iteration 0.0.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/MIT-LICENSE.txt +20 -0
- package/dist/abort.d.ts +26 -0
- package/dist/abort.d.ts.map +1 -0
- package/dist/abort.js +69 -0
- package/dist/abort.js.map +1 -0
- package/dist/assertions/eventualequal.d.ts +9 -0
- package/dist/assertions/eventualequal.d.ts.map +1 -0
- package/dist/assertions/eventualequal.js +30 -0
- package/dist/assertions/eventualequal.js.map +1 -0
- package/dist/async-channel.d.ts +101 -0
- package/dist/async-channel.d.ts.map +1 -0
- package/dist/async-channel.js +208 -0
- package/dist/async-channel.js.map +1 -0
- package/dist/channel-decorators.d.ts +23 -0
- package/dist/channel-decorators.d.ts.map +1 -0
- package/dist/channel-decorators.js +31 -0
- package/dist/channel-decorators.js.map +1 -0
- package/dist/concurrency.d.ts +43 -0
- package/dist/concurrency.d.ts.map +1 -0
- package/dist/concurrency.js +145 -0
- package/dist/concurrency.js.map +1 -0
- package/dist/consumers.d.ts +199 -0
- package/dist/consumers.d.ts.map +1 -0
- package/dist/consumers.js +349 -0
- package/dist/consumers.js.map +1 -0
- package/dist/count.d.ts +37 -0
- package/dist/count.d.ts.map +1 -0
- package/dist/count.js +70 -0
- package/dist/count.js.map +1 -0
- package/dist/empty-iterator.d.ts +15 -0
- package/dist/empty-iterator.d.ts.map +1 -0
- package/dist/empty-iterator.js +16 -0
- package/dist/empty-iterator.js.map +1 -0
- package/dist/eventualequal.d.ts +2 -0
- package/dist/eventualequal.d.ts.map +1 -0
- package/dist/eventualequal.js +54 -0
- package/dist/eventualequal.js.map +1 -0
- package/dist/exhaust.d.ts +7 -0
- package/dist/exhaust.d.ts.map +1 -0
- package/dist/exhaust.js +22 -0
- package/dist/exhaust.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/is-iterator.d.ts +5 -0
- package/dist/is-iterator.d.ts.map +1 -0
- package/dist/is-iterator.js +5 -0
- package/dist/is-iterator.js.map +1 -0
- package/dist/iterator-tools.d.ts +164 -0
- package/dist/iterator-tools.d.ts.map +1 -0
- package/dist/iterator-tools.js +231 -0
- package/dist/iterator-tools.js.map +1 -0
- package/dist/itertools.d.ts +237 -0
- package/dist/itertools.d.ts.map +1 -0
- package/dist/itertools.js +471 -0
- package/dist/itertools.js.map +1 -0
- package/dist/tee.d.ts +18 -0
- package/dist/tee.d.ts.map +1 -0
- package/dist/tee.js +118 -0
- package/dist/tee.js.map +1 -0
- package/dist/transduce.d.ts +79 -0
- package/dist/transduce.d.ts.map +1 -0
- package/dist/transduce.js +37 -0
- package/dist/transduce.js.map +1 -0
- package/dist/transducers.d.ts +123 -0
- package/dist/transducers.d.ts.map +1 -0
- package/dist/transducers.js +212 -0
- package/dist/transducers.js.map +1 -0
- package/package.json +122 -0
- package/readme.md +761 -0
- package/src/abort.ts +80 -0
- package/src/assertions/eventualequal.ts +36 -0
- package/src/async-channel.ts +228 -0
- package/src/channel-decorators.ts +49 -0
- package/src/concurrency.ts +168 -0
- package/src/consumers.ts +425 -0
- package/src/count.ts +72 -0
- package/src/empty-iterator.ts +17 -0
- package/src/eventualequal.ts +54 -0
- package/src/exhaust.ts +26 -0
- package/src/index.ts +17 -0
- package/src/is-iterator.ts +9 -0
- package/src/iterator-tools.ts +290 -0
- package/src/itertools.ts +518 -0
- package/src/tee.ts +136 -0
- package/src/transduce.ts +161 -0
- package/src/transducers.ts +256 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded-concurrency helpers: mapConcurrentAsync (parallel map over an
|
|
3
|
+
* (a)sync iterable with a concurrency limit) and prefetchAsync (eager
|
|
4
|
+
* read-ahead into a bounded buffer). Both propagate cancellation/early
|
|
5
|
+
* consumer exit upstream via iterator.return().
|
|
6
|
+
*/
|
|
7
|
+
import { throwIfAborted } from "./abort.js";
|
|
8
|
+
const abortErrorOf = (signal) => signal.reason ?? new DOMException("This operation was aborted", "AbortError");
|
|
9
|
+
const getIterator = (iterable) => Symbol.asyncIterator in Object(iterable)
|
|
10
|
+
? iterable[Symbol.asyncIterator]()
|
|
11
|
+
: iterable[Symbol.iterator]();
|
|
12
|
+
/**
|
|
13
|
+
* Map `fn` over `iterable` with up to `concurrency` invocations in flight
|
|
14
|
+
* at once. With `ordered: true` (the default) results are yielded in input
|
|
15
|
+
* order; with `ordered: false` they are yielded in completion order.
|
|
16
|
+
*
|
|
17
|
+
* - Backpressure: at most `concurrency` results are held; the source is
|
|
18
|
+
* only pulled while there is spare in-flight capacity.
|
|
19
|
+
* - Cancellation: an optional `signal` aborts promptly (rejecting with the
|
|
20
|
+
* signal's reason, an "AbortError" DOMException by default) even while
|
|
21
|
+
* awaiting a slow `fn` or source; the source iterator is closed via
|
|
22
|
+
* `return()`.
|
|
23
|
+
* - Early exit / error: breaking out of the loop, or an `fn` rejection,
|
|
24
|
+
* also closes the source via `return()`.
|
|
25
|
+
* @kind function
|
|
26
|
+
* @name mapConcurrentAsync
|
|
27
|
+
*/
|
|
28
|
+
export const mapConcurrentAsync = async function* (fn, iterable, { concurrency, ordered = true, signal }) {
|
|
29
|
+
if (!(typeof concurrency === "number" && concurrency >= 1)) {
|
|
30
|
+
throw new RangeError("mapConcurrentAsync: concurrency must be >= 1");
|
|
31
|
+
}
|
|
32
|
+
throwIfAborted(signal);
|
|
33
|
+
const it = getIterator(iterable);
|
|
34
|
+
let onAbort;
|
|
35
|
+
const aborted = signal
|
|
36
|
+
? new Promise((_, reject) => {
|
|
37
|
+
onAbort = () => reject(abortErrorOf(signal));
|
|
38
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
39
|
+
})
|
|
40
|
+
: undefined;
|
|
41
|
+
aborted?.catch(() => { });
|
|
42
|
+
const race = (promise) => (aborted ? Promise.race([promise, aborted]) : promise);
|
|
43
|
+
let sourceDone = false;
|
|
44
|
+
try {
|
|
45
|
+
if (ordered) {
|
|
46
|
+
const queue = [];
|
|
47
|
+
while (true) {
|
|
48
|
+
while (!sourceDone && queue.length < concurrency) {
|
|
49
|
+
const r = await race(Promise.resolve(it.next()));
|
|
50
|
+
if (r.done) {
|
|
51
|
+
sourceDone = true;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
const task = (async () => fn(r.value))();
|
|
55
|
+
task.catch(() => { }); // handled when shifted; avoid unhandledRejection
|
|
56
|
+
queue.push(task);
|
|
57
|
+
}
|
|
58
|
+
if (queue.length === 0) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
yield await race(queue.shift());
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const pending = new Set();
|
|
66
|
+
const add = (value) => {
|
|
67
|
+
let self;
|
|
68
|
+
self = (async () => {
|
|
69
|
+
const out = await fn(value);
|
|
70
|
+
return [self, out];
|
|
71
|
+
})();
|
|
72
|
+
self.catch(() => { }); // handled via the race below
|
|
73
|
+
pending.add(self);
|
|
74
|
+
};
|
|
75
|
+
while (true) {
|
|
76
|
+
while (!sourceDone && pending.size < concurrency) {
|
|
77
|
+
const r = await race(Promise.resolve(it.next()));
|
|
78
|
+
if (r.done) {
|
|
79
|
+
sourceDone = true;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
add(r.value);
|
|
83
|
+
}
|
|
84
|
+
if (pending.size === 0) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const [task, out] = await race(Promise.race(pending));
|
|
88
|
+
pending.delete(task);
|
|
89
|
+
yield out;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
if (signal && onAbort) {
|
|
95
|
+
signal.removeEventListener("abort", onAbort);
|
|
96
|
+
}
|
|
97
|
+
if (!sourceDone && it.return) {
|
|
98
|
+
// Close the source on abort, error, or early consumer exit --
|
|
99
|
+
// fire-and-forget so a hung source can't block the rejection.
|
|
100
|
+
Promise.resolve(it.return()).catch(() => { });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Eagerly pull up to `n` items ahead of the consumer into a bounded
|
|
106
|
+
* buffer, so a slow consumer overlaps with a slow producer. `n <= 0`
|
|
107
|
+
* degenerates to plain iteration. Early consumer exit (or an error)
|
|
108
|
+
* closes the source via `return()`.
|
|
109
|
+
* @kind function
|
|
110
|
+
* @name prefetchAsync
|
|
111
|
+
*/
|
|
112
|
+
export const prefetchAsync = async function* (n, iterable) {
|
|
113
|
+
if (n <= 0) {
|
|
114
|
+
yield* iterable;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const it = getIterator(iterable);
|
|
118
|
+
const buffer = [];
|
|
119
|
+
let done = false;
|
|
120
|
+
const pull = () => {
|
|
121
|
+
const p = Promise.resolve(it.next());
|
|
122
|
+
p.catch(() => { }); // handled when shifted
|
|
123
|
+
buffer.push(p);
|
|
124
|
+
};
|
|
125
|
+
try {
|
|
126
|
+
for (let i = 0; i < n; i++) {
|
|
127
|
+
pull();
|
|
128
|
+
}
|
|
129
|
+
while (true) {
|
|
130
|
+
const r = await buffer.shift();
|
|
131
|
+
if (r.done) {
|
|
132
|
+
done = true;
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
pull(); // keep n reads in flight while the consumer works
|
|
136
|
+
yield r.value;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
if (!done && it.return) {
|
|
141
|
+
Promise.resolve(it.return()).catch(() => { });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
//# sourceMappingURL=concurrency.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"concurrency.js","sourceRoot":"","sources":["../src/concurrency.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAsB,cAAc,EAAE,MAAM,YAAY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EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal consumer operations: resolve an iterable to a single value (or a
|
|
3
|
+
* Promise of one), rather than returning another iterable. Two families:
|
|
4
|
+
*
|
|
5
|
+
* - Iterator-Helper parity (some/every/find/forEach/fold): native ES2025
|
|
6
|
+
* Iterator Helpers give sync code `.some()`/`.every()`/`.find()`/
|
|
7
|
+
* `.forEach()`/`.reduce()` for free via Iterator.from(iterable); there is
|
|
8
|
+
* no shipped async equivalent (AsyncIterator helpers are TC39 Stage 2),
|
|
9
|
+
* so the async half here is hand-rolled.
|
|
10
|
+
* - Summary/aggregate consumers (first/last/nth/quantify/min/max): not part
|
|
11
|
+
* of core Python itertools, but a near-universal expectation for any
|
|
12
|
+
* itertools-adjacent library (see itertools-ts's "summary" namespace);
|
|
13
|
+
* `quantify` specifically is a named recipe from Python's own itertools
|
|
14
|
+
* docs, not an invented name.
|
|
15
|
+
*
|
|
16
|
+
* House conventions:
|
|
17
|
+
* - Async variants `await` their callback's return value (predicate/fn/keyFn
|
|
18
|
+
* may be async); sync variants do not, matching native Iterator Helpers'
|
|
19
|
+
* own non-awaiting behavior and every other sync callback in this library
|
|
20
|
+
* (groupBySync's keyFn, uniqueSync's keyFn, etc.).
|
|
21
|
+
* - Required-callback functions put the callback first (matching
|
|
22
|
+
* itertools.ts: takeWhileSync(predicate, iterable)). Optional-callback
|
|
23
|
+
* functions put `iterable` first (matching Python's own builtin order:
|
|
24
|
+
* min(iterable, key=..., default=...)).
|
|
25
|
+
* - find/first/last/nth/min/max return `undefined` (or a caller-supplied
|
|
26
|
+
* `defaultValue`) on an empty iterable/no match, rather than throwing --
|
|
27
|
+
* consistent with Array.prototype.find, deliberately diverging from
|
|
28
|
+
* Python's own min()/max() (which throw without a default).
|
|
29
|
+
*
|
|
30
|
+
* `fold` is named deliberately -- NOT `reduce` -- to avoid colliding with
|
|
31
|
+
* reduceSync/reduceAsync in iterator-tools.ts, which are an internal
|
|
32
|
+
* streaming primitive (yield each emitted item) rather than a terminal
|
|
33
|
+
* reduce-to-one-value.
|
|
34
|
+
*/
|
|
35
|
+
import { type SignalOptions } from "./abort.ts";
|
|
36
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
37
|
+
export type AnyAsyncIterable<T> = AsyncIterable<T> | Iterable<T>;
|
|
38
|
+
/**
|
|
39
|
+
* True if any item satisfies predicate. Short-circuits on the first match.
|
|
40
|
+
* @kind function
|
|
41
|
+
* @name someSync
|
|
42
|
+
*/
|
|
43
|
+
export declare const someSync: <T>(predicate: (item: T) => boolean, iterable: Iterable<T>) => boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Asynchronous dual of someSync. Short-circuits (a `return` inside
|
|
46
|
+
* `for await...of` closes the underlying async iterator).
|
|
47
|
+
* @kind function
|
|
48
|
+
* @name someAsync
|
|
49
|
+
*/
|
|
50
|
+
export declare const someAsync: <T>(predicate: (item: T) => MaybePromise<boolean>, iterable: AnyAsyncIterable<T>, { signal }?: SignalOptions) => Promise<boolean>;
|
|
51
|
+
/**
|
|
52
|
+
* True only if every item satisfies predicate. Short-circuits on the first
|
|
53
|
+
* failure.
|
|
54
|
+
* @kind function
|
|
55
|
+
* @name everySync
|
|
56
|
+
*/
|
|
57
|
+
export declare const everySync: <T>(predicate: (item: T) => boolean, iterable: Iterable<T>) => boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Asynchronous dual of everySync.
|
|
60
|
+
* @kind function
|
|
61
|
+
* @name everyAsync
|
|
62
|
+
*/
|
|
63
|
+
export declare const everyAsync: <T>(predicate: (item: T) => MaybePromise<boolean>, iterable: AnyAsyncIterable<T>, { signal }?: SignalOptions) => Promise<boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* The first item satisfying predicate, or undefined. Short-circuits.
|
|
66
|
+
* @kind function
|
|
67
|
+
* @name findSync
|
|
68
|
+
*/
|
|
69
|
+
export declare const findSync: <T>(predicate: (item: T) => boolean, iterable: Iterable<T>) => T | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* Asynchronous dual of findSync.
|
|
72
|
+
* @kind function
|
|
73
|
+
* @name findAsync
|
|
74
|
+
*/
|
|
75
|
+
export declare const findAsync: <T>(predicate: (item: T) => MaybePromise<boolean>, iterable: AnyAsyncIterable<T>, { signal }?: SignalOptions) => Promise<T | undefined>;
|
|
76
|
+
/**
|
|
77
|
+
* Call fn once per item, for side effects. Returns undefined.
|
|
78
|
+
* @kind function
|
|
79
|
+
* @name forEachSync
|
|
80
|
+
*/
|
|
81
|
+
export declare const forEachSync: <T>(fn: (item: T) => unknown, iterable: Iterable<T>) => void;
|
|
82
|
+
/**
|
|
83
|
+
* Asynchronous dual of forEachSync. Distinct from `run` (iterator-tools.ts)
|
|
84
|
+
* -- `run` is framed around "rendering a program" (reversed argument order,
|
|
85
|
+
* `render` defaults to console.log); this is the itertools-parity-family
|
|
86
|
+
* addition with the family's usual (fn, iterable) argument order.
|
|
87
|
+
* @kind function
|
|
88
|
+
* @name forEachAsync
|
|
89
|
+
* @see run
|
|
90
|
+
*/
|
|
91
|
+
export declare const forEachAsync: <T>(fn: (item: T) => unknown, iterable: AnyAsyncIterable<T>, { signal }?: SignalOptions) => Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Reduce iterable to a single value, given an explicit initial value
|
|
94
|
+
* (unlike Array.prototype.reduce, `init` is required -- avoids replicating
|
|
95
|
+
* reduce's "throws on empty array with no initial value" edge case).
|
|
96
|
+
* Named `fold`, not `reduce`, to avoid colliding with reduceSync/reduceAsync
|
|
97
|
+
* (iterator-tools.ts), which are an internal streaming primitive, not a
|
|
98
|
+
* terminal reduce-to-one-value.
|
|
99
|
+
* @kind function
|
|
100
|
+
* @name foldSync
|
|
101
|
+
*/
|
|
102
|
+
export declare const foldSync: <T, Acc>(fn: (acc: Acc, item: T) => Acc, init: Acc, iterable: Iterable<T>) => Acc;
|
|
103
|
+
/**
|
|
104
|
+
* Asynchronous dual of foldSync.
|
|
105
|
+
* @kind function
|
|
106
|
+
* @name foldAsync
|
|
107
|
+
*/
|
|
108
|
+
export declare const foldAsync: <T, Acc>(fn: (acc: Acc, item: T) => MaybePromise<Acc>, init: Acc, iterable: AnyAsyncIterable<T>, { signal }?: SignalOptions) => Promise<Acc>;
|
|
109
|
+
/**
|
|
110
|
+
* The first item, or defaultValue if the iterable is empty.
|
|
111
|
+
* @kind function
|
|
112
|
+
* @name firstSync
|
|
113
|
+
*/
|
|
114
|
+
export declare const firstSync: <T, D = undefined>(iterable: Iterable<T>, defaultValue?: D) => T | D;
|
|
115
|
+
/**
|
|
116
|
+
* Asynchronous dual of firstSync.
|
|
117
|
+
* @kind function
|
|
118
|
+
* @name firstAsync
|
|
119
|
+
*/
|
|
120
|
+
export declare const firstAsync: <T, D = undefined>(iterable: AsyncIterable<T>, defaultValue?: D, { signal }?: SignalOptions) => Promise<T | D>;
|
|
121
|
+
/**
|
|
122
|
+
* The last item, or defaultValue if the iterable is empty. Necessarily
|
|
123
|
+
* consumes the entire iterable -- no way around it for a generic source.
|
|
124
|
+
* @kind function
|
|
125
|
+
* @name lastSync
|
|
126
|
+
*/
|
|
127
|
+
export declare const lastSync: <T, D = undefined>(iterable: Iterable<T>, defaultValue?: D) => T | D;
|
|
128
|
+
/**
|
|
129
|
+
* Asynchronous dual of lastSync.
|
|
130
|
+
* @kind function
|
|
131
|
+
* @name lastAsync
|
|
132
|
+
*/
|
|
133
|
+
export declare const lastAsync: <T, D = undefined>(iterable: AnyAsyncIterable<T>, defaultValue?: D, { signal }?: SignalOptions) => Promise<T | D>;
|
|
134
|
+
/**
|
|
135
|
+
* The item at position n (0-indexed), or defaultValue if out of range.
|
|
136
|
+
* Reuses isliceSync rather than duplicating positional-skip logic.
|
|
137
|
+
* @kind function
|
|
138
|
+
* @name nthSync
|
|
139
|
+
*/
|
|
140
|
+
export declare const nthSync: <T, D = undefined>(iterable: Iterable<T>, n: number, defaultValue?: D) => T | D;
|
|
141
|
+
/**
|
|
142
|
+
* Asynchronous dual of nthSync.
|
|
143
|
+
* @kind function
|
|
144
|
+
* @name nthAsync
|
|
145
|
+
*/
|
|
146
|
+
export declare const nthAsync: <T, D = undefined>(iterable: AsyncIterable<T>, n: number, defaultValue?: D, { signal }?: SignalOptions) => Promise<T | D>;
|
|
147
|
+
/**
|
|
148
|
+
* Count items satisfying predicate (default: count all items). Named after
|
|
149
|
+
* the `quantify(iterable, pred=bool)` recipe documented in Python's own
|
|
150
|
+
* itertools docs -- not part of core itertools, but not an invented name
|
|
151
|
+
* either. Deliberately not named `count`/`countSync`, which already mirror
|
|
152
|
+
* Python's itertools.count (an integer *sequence generator*, unrelated).
|
|
153
|
+
* @kind function
|
|
154
|
+
* @name quantifySync
|
|
155
|
+
*/
|
|
156
|
+
export declare const quantifySync: <T>(iterable: Iterable<T>, predicate?: (item: T) => boolean) => number;
|
|
157
|
+
/**
|
|
158
|
+
* Asynchronous dual of quantifySync.
|
|
159
|
+
* @kind function
|
|
160
|
+
* @name quantifyAsync
|
|
161
|
+
*/
|
|
162
|
+
export declare const quantifyAsync: <T>(iterable: AnyAsyncIterable<T>, predicate?: (item: T) => MaybePromise<boolean>, { signal }?: SignalOptions) => Promise<number>;
|
|
163
|
+
/**
|
|
164
|
+
* The item with the minimum key (default: the item itself), or defaultValue
|
|
165
|
+
* if the iterable is empty. Necessarily consumes the entire iterable.
|
|
166
|
+
* Mirrors Python's builtin min(iterable, key=..., default=...), but
|
|
167
|
+
* returns undefined rather than throwing when empty and no default is given.
|
|
168
|
+
* A NaN key anywhere in the iterable poisons the result to NaN, matching
|
|
169
|
+
* `Math.min`'s own NaN-poisoning convention (rather than silently dropping
|
|
170
|
+
* out of every comparison and returning an unrelated wrong item).
|
|
171
|
+
* @kind function
|
|
172
|
+
* @name minSync
|
|
173
|
+
*/
|
|
174
|
+
export declare const minSync: <T, D = undefined>(iterable: Iterable<T>, keyFn?: (item: T) => unknown, defaultValue?: D) => T | D;
|
|
175
|
+
/**
|
|
176
|
+
* Asynchronous dual of minSync; keyFn may be async. Same NaN-poisoning
|
|
177
|
+
* convention as minSync.
|
|
178
|
+
* @kind function
|
|
179
|
+
* @name minAsync
|
|
180
|
+
*/
|
|
181
|
+
export declare const minAsync: <T, D = undefined>(iterable: AnyAsyncIterable<T>, keyFn?: (item: T) => unknown, defaultValue?: D, { signal }?: SignalOptions) => Promise<T | D>;
|
|
182
|
+
/**
|
|
183
|
+
* The item with the maximum key (default: the item itself), or defaultValue
|
|
184
|
+
* if the iterable is empty. Mirrors Python's builtin
|
|
185
|
+
* max(iterable, key=..., default=...); see minSync for the
|
|
186
|
+
* undefined-vs-throw divergence and the NaN-poisoning convention.
|
|
187
|
+
* @kind function
|
|
188
|
+
* @name maxSync
|
|
189
|
+
*/
|
|
190
|
+
export declare const maxSync: <T, D = undefined>(iterable: Iterable<T>, keyFn?: (item: T) => unknown, defaultValue?: D) => T | D;
|
|
191
|
+
/**
|
|
192
|
+
* Asynchronous dual of maxSync; keyFn may be async. Same NaN-poisoning
|
|
193
|
+
* convention as minSync.
|
|
194
|
+
* @kind function
|
|
195
|
+
* @name maxAsync
|
|
196
|
+
*/
|
|
197
|
+
export declare const maxAsync: <T, D = undefined>(iterable: AnyAsyncIterable<T>, keyFn?: (item: T) => unknown, defaultValue?: D, { signal }?: SignalOptions) => Promise<T | D>;
|
|
198
|
+
export {};
|
|
199
|
+
//# sourceMappingURL=consumers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consumers.d.ts","sourceRoot":"","sources":["../src/consumers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,EAAa,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAG3D,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,C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