-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.ts
63 lines (56 loc) · 1.48 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @packageDocumentation
*
* Reduce the values of an (async)iterable to a single value.
*
* @example
*
* ```javascript
* import reduce from 'it-reduce'
*
* // This can also be an iterator, generator, etc
* const values = [0, 1, 2, 3, 4]
*
* const result = reduce(values, (acc, curr, index) => acc + curr, 0)
*
* console.info(result) // 10
* ```
*
* Async sources must be awaited:
*
* ```javascript
* import reduce from 'it-reduce'
*
* const values = async function * () {
* yield * [0, 1, 2, 3, 4]
* }
*
* const result = await reduce(values(), (acc, curr, index) => acc + curr, 0)
*
* console.info(result) // 10
* ```
*/
function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> {
return thing[Symbol.asyncIterator] != null
}
/**
* Reduces the values yielded by an (async) iterable
*/
function reduce <T, V> (source: Iterable<T>, func: (acc: V, curr: T, index: number) => V, init: V): V
function reduce <T, V> (source: Iterable<T> | AsyncIterable<T>, func: (acc: V, curr: T, index: number) => V, init: V): Promise<V>
function reduce <T, V> (source: Iterable<T> | AsyncIterable<T>, func: (acc: V, curr: T, index: number) => V, init: V): Promise<V> | V {
let index = 0
if (isAsyncIterable(source)) {
return (async function () {
for await (const val of source) {
init = func(init, val, index++)
}
return init
})()
}
for (const val of source) {
init = func(init, val, index++)
}
return init
}
export default reduce