# array-reducer

> Reduces an array. Supports sync and promise/async reducer functions

Latest version **1.0.3** (published 2019-12-06) · GNU license · 0 weekly downloads

## Install

```sh
npm install array-reducer
pnpm add array-reducer
yarn add array-reducer
bun add array-reducer
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2019-12-06 |
| First published | 2019-10-31 |
| Weekly downloads | 0 |
| License | GNU |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 39.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | @flejz |
| Maintainers | flejz |
| Keywords | javascript, array, reduce, async, await, promise |

## Links

- npm: https://www.npmjs.com/package/array-reducer
- Repository: https://github.com/flejz/array-reducer
- Homepage: https://github.com/flejz/array-reducer#readme
- Issues: https://github.com/flejz/array-reducer/issues
- npm.io page: https://npm.io/package/array-reducer

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2019-12-06
- 1.0.2 — 2019-10-31
- 1.0.1 — 2019-10-31
- 1.0.0 — 2019-10-31

## README

# array-reducer

![code coverage][shield-coverage]
![file size][shield-size]
![version][shield-release]

[shield-coverage]: https://img.shields.io/badge/coverage-100%25-success
[shield-size]: https://img.shields.io/github/size/flejz/array-reducer/src/index.js
[shield-release]: https://img.shields.io/github/package-json/v/flejz/array-reducer

Never carry anymore about async reduce an array.
Install with:

```sh
npm i array-reducer
```

Start requiring and then make the magic happen

```javascript
const reducer = require('array-reducer')
```

You might need 3 arguments:

```javascript
reducer(array, fn, acc)
```

* `array` might be an array of whatever you want
* `fn` is your reducer function. Here you can go async or sync, doesn't matter
* `acc` is the default accumulator value for your reducer function `fn`

## Samples

You might use reducers the way you want withouth carying if the reducer function shall fetch something from the *database*, make an external *api call* or a simple sum.

### async/await

Make things meaningful, avoid promise/callback hell!

```javascript
const reducer = require('array-reducer')

const arr = [1, 2, 3, 4]
const sum = async (n, m) => n + m

const result = await reducer(arr, sum, 0)
console.log(result) // 10
```

### promise

Connect in a promise chain

```javascript
const reducer = require('array-reducer')

const arr = [1, 2, 3, 4]
const sum = (n, m) => new Promise(resolve => resolve(n + m))

reducer(arr, sum, 3).then((result) => {
  console.log(result)  // 13
})
```

### standard/sync

Go simple

```javascript
const reducer = require('array-reducer')

const arr = [1, 2, 3, 4]
const sum = (n, m) => n + m

const result = reducer(arr, sum, 5)
console.log(result) // 15
```

## Go beyond! Go fancy!

It's not only about numbers or aggregations, we can go further.

```javascript
const reducer = require('array-reducer')

const arr = [['drink', 'beer'], ['eat', 'hotdog']]
const toObject = (obj, [prop, value]) => ({
  ...obj,
  [prop]: value,
})

const result = reducer(arr, toObject, {})
console.log(result) // { drink: 'beer', eat: 'hotdog' }

```

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