# @fightingdreamer/iter-reduce

> Missing reduce over iterable.

Latest version **0.1.5** (published 2024-06-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @fightingdreamer/iter-reduce
pnpm add @fightingdreamer/iter-reduce
yarn add @fightingdreamer/iter-reduce
bun add @fightingdreamer/iter-reduce
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2024-06-22 |
| First published | 2023-12-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Patryk Stępień |
| Keywords | iter |

## Links

- npm: https://www.npmjs.com/package/@fightingdreamer/iter-reduce
- Homepage: https://github.com/fightingdreamer/npm-iter-reduce
- npm.io page: https://npm.io/package/@fightingdreamer/iter-reduce

## Recent versions

- 0.1.5 (latest) — 2024-06-22
- 0.1.4 — 2024-06-22
- 0.1.3 — 2023-12-12
- 0.1.2 — 2023-12-11
- 0.1.1 — 2023-12-05
- 0.1.0 — 2023-12-05

## README

# iter-reduce

## Description

Missing reduce function for iterator based input, similar to Array.reduce.

Second implementation can better handle cases of input with length 0 or 1.

## Install

```bash
bun add @fightingdreamer/iter-reduce
```

## Usage

```js
import {reduceWithInitial} from '@fightingdreamer/iter-reduce'

const vector = [1,2,3][Symbol.iterator]()
const result = reduceWithInitial(vector, (r, v) => r + v, 3)
const expect = 9
console.assert(result == expect)
```

```js
import {reduceWithDefault} from '@fightingdreamer/iter-reduce'

const vector = [1][Symbol.iterator]()
const result = reduceWithDefault(vector, (r, v) => Math.min(r, v), 0)
const expect = 1
console.assert(result == expect)
```

## Usage (node / commonjs)

```js
const {reduceWithInitial} = require('@fightingdreamer/iter-reduce')

const vector = [1,2,3][Symbol.iterator]()
const result = reduceWithInitial(vector, (r, v) => r + v, 3)
const expect = 9
assert(result == expect)
```

```js
const {reduceWithDefault} = require('@fightingdreamer/iter-reduce')

const vector = [1][Symbol.iterator]()
const result = reduceWithDefault(vector, (r, v) => Math.min(r, v), 0)
const expect = 1
assert(result == expect)
```

## Functions

```js
function reduceWithInitial<T, R>(
  iterable: Iterable<T>,
  callbackFn: (accumulator: R, currentValue: T, currentIndex: number) => R,
  initialValue: R,
): R
```

For inputs of any length will call callbackFn on every item,
using initialValue as accumulator on first call.

This function is best suited for merging input into one item, or
creating collection from input reusing already collected items.

```js
function reduceWithDefault<T, D, R>(
  iterable: Iterable<T>,
  callbackFn: (accumulator: T | R, currentValue: T, currentIndex: number) => R,
  defaultValue: D,
): T | D | R {
```

For inputs of length >= 2 will call callbackFn on every item except first one,
using first item as accumulator on first call.

For inputs of length == 1 will not call callbackFn and return first item.

For inputs of length == 0 will not call callbackFn and return defaultValue item.

This function is best suited for cases when initial value can easily distort result,
eg. finding `Math.min` from `Iterable<number>`.

---
_Source: https://npm.io/package/@fightingdreamer/iter-reduce · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
