Comparing version 1.0.0 to 1.1.0
30
index.js
import isAsyncIterable from "is-async-iterable"; | ||
/** | ||
* The reduce() method applies a function against an accumulator and each element | ||
* in the async iterable to reduce it to a single promise. | ||
* @param {Function} reducer Function to execute on each element in the async iterable, taking four arguments: | ||
* accumulator - The accumulator accumulates the callback's return values; it is | ||
* the accumulated value previously returned in the last invocation of the | ||
* callback, or initialValue, if supplied (see below). | ||
* | ||
* currentValue - The current element being processed in the async iterable. | ||
* | ||
* currentIndex - The index of the current element being processed in the async | ||
* iterable. Starts at index 0, if an initialValue is provided, and at index 1 | ||
* otherwise. | ||
* | ||
* data - The async iterable reduce was called upon. | ||
* | ||
* @param {any} accumulator Value to use as the first argument to the first call of | ||
* the callback. If no initial value is supplied, the first element in the async iterable will | ||
* be used. Calling reduce on an empty async iterable without an initial value is an error. | ||
* @param {AsyncIterable} data The async iterable to reduce | ||
* @return {Promise} The promise value that results from the reduction. | ||
*/ | ||
export default async function reduce(reducer, accumulator, data) { | ||
// ? console.log({ reducer, accumulator, data }); | ||
if (typeof reducer !== "function") { | ||
@@ -8,6 +31,2 @@ throw new TypeError("reducer argument must be a function."); | ||
if (typeof data === "undefined") { | ||
return reduce.bind(null, reducer, accumulator); | ||
} | ||
if (!isAsyncIterable(data)) { | ||
@@ -24,1 +43,4 @@ throw new TypeError("data argument must be an iterable or async-iterable."); | ||
} | ||
reduce.partial = (reducer, accumulator) => | ||
reduce.bind(null, reducer, accumulator); |
{ | ||
"name": "ai-reduce", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Reduce for async iterables.", | ||
@@ -5,0 +5,0 @@ "repository": "parro-it/ai-reduce", |
@@ -8,7 +8,11 @@ # ai-reduce | ||
background details relevant to understanding what this module does | ||
The reduce() method applies a function against an accumulator and each element | ||
in the array (from left to right) to reduce it to a single value. | ||
## Async iterable fun | ||
__This module is part of [Async iterable fun](https://github.com/parro-it/ai-fun), a complete toolset of modules to work with async iterables.__ | ||
**This module is part of | ||
[Async iterable fun](https://github.com/parro-it/ai-fun), a complete toolset of | ||
modules to work with async iterables.** | ||
## Usage | ||
@@ -19,5 +23,5 @@ | ||
```js | ||
const aiReduce = require('ai-reduce'); | ||
const aiReduce = require("ai-reduce"); | ||
console.log({aiReduce}); | ||
console.log({ aiReduce }); | ||
``` | ||
@@ -28,2 +32,3 @@ | ||
``` | ||
``` | ||
@@ -33,2 +38,40 @@ | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
### index | ||
The reduce() method applies a function against an accumulator and each element | ||
in the async iterable to reduce it to a single promise. | ||
**Parameters** | ||
* `reducer` | ||
**[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** | ||
Function to execute on each element in the async iterable, taking four | ||
arguments: | ||
``` | ||
accumulator - The accumulator accumulates the callback's return values; it is | ||
the accumulated value previously returned in the last invocation of the | ||
callback, or initialValue, if supplied (see below). | ||
currentValue - The current element being processed in the async iterable. | ||
currentIndex - The index of the current element being processed in the async | ||
iterable. Starts at index 0, if an initialValue is provided, and at index 1 | ||
otherwise. | ||
data - The async iterable reduce was called upon. | ||
``` | ||
* `accumulator` **any** Value to use as the first argument to the first call of | ||
the callback. If no initial value is supplied, the first element in the async | ||
iterable will be used. Calling reduce on an empty async iterable without an | ||
initial value is an error. | ||
* `data` **AsyncIterable** The async iterable to reduce | ||
Returns | ||
**[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** | ||
The promise value that results from the reduction. | ||
## Install | ||
@@ -44,9 +87,7 @@ | ||
- [`noffle/common-readme`](https://github.com/noffle/common-readme) | ||
- [`parro-it/ai-fun`](https://github.com/parro-it/ai-fun) | ||
* [`noffle/common-readme`](https://github.com/noffle/common-readme) | ||
* [`parro-it/ai-fun`](https://github.com/parro-it/ai-fun) | ||
## License | ||
MIT | ||
6423
41
89