( collection: C, reducer: (reduction: V | R, value: V, key: K, iter: C) => R, reduction: V | R | undefined, context: unknown, useFirst: boolean, reverse: boolean )
| 2 | import assertNotInfinite from './utils/assertNotInfinite'; |
| 3 | |
| 4 | export function reduce<K, V, R, C extends Collection<K, V>>( |
| 5 | collection: C, |
| 6 | reducer: (reduction: V | R, value: V, key: K, iter: C) => R, |
| 7 | reduction: V | R | undefined, |
| 8 | context: unknown, |
| 9 | useFirst: boolean, |
| 10 | reverse: boolean |
| 11 | ): V | R | undefined { |
| 12 | // @ts-expect-error Migrate to CollectionImpl in v6 |
| 13 | assertNotInfinite(collection.size); |
| 14 | // @ts-expect-error Migrate to CollectionImpl in v6 |
| 15 | collection.__iterate((v: V, k: K, c: C) => { |
| 16 | if (useFirst) { |
| 17 | useFirst = false; |
| 18 | reduction = v; |
| 19 | } else { |
| 20 | // `reduction` has already been seeded here (either with the provided |
| 21 | // initial value or with the first iterated value), so it is never the |
| 22 | // `undefined` placeholder — only a `V` or a `R`. |
| 23 | reduction = reducer.call(context, reduction!, v, k, c); |
| 24 | } |
| 25 | }, reverse); |
| 26 | return reduction; |
| 27 | } |
| 28 | |
| 29 | export function keyMapper<K, V>(v: V, k: K): K { |
| 30 | return k; |
no test coverage detected