Comparing version 2.1.0 to 3.0.0
@@ -1,57 +0,40 @@ | ||
declare namespace pReduce { | ||
type ReducerFunction<ValueType, ReducedValueType = ValueType> = ( | ||
previousValue: ReducedValueType, | ||
currentValue: ValueType, | ||
index: number | ||
) => PromiseLike<ReducedValueType> | ReducedValueType; | ||
} | ||
export type ReducerFunction<ValueType, ReducedValueType = ValueType> = ( | ||
previousValue: ReducedValueType, | ||
currentValue: ValueType, | ||
index: number | ||
) => PromiseLike<ReducedValueType> | ReducedValueType; | ||
declare const pReduce: { | ||
/** | ||
Reduce a list of values using promises into a promise for a value. | ||
/** | ||
Reduce a list of values using promises into a promise for a value. | ||
@param input - Iterated over serially in the `reducer` function. | ||
@param reducer - Expected to return a value. If a `Promise` is returned, it's awaited before continuing with the next iteration. | ||
@param initialValue - Value to use as `previousValue` in the first `reducer` invocation. | ||
@returns A `Promise` that is fulfilled when all promises in `input` and ones returned from `reducer` are fulfilled, or rejects if any of the promises reject. The resolved value is the result of the reduction. | ||
@param input - Iterated over serially in the `reducer` function. | ||
@param reducer - Expected to return a value. If a `Promise` is returned, it's awaited before continuing with the next iteration. | ||
@param initialValue - Value to use as `previousValue` in the first `reducer` invocation. | ||
@returns A `Promise` that is fulfilled when all promises in `input` and ones returned from `reducer` are fulfilled, or rejects if any of the promises reject. The resolved value is the result of the reduction. | ||
@example | ||
``` | ||
import pReduce = require('p-reduce'); | ||
import humanInfo from 'human-info'; // Not a real module | ||
@example | ||
``` | ||
import pReduce from 'p-reduce'; | ||
import humanInfo from 'human-info'; // Not a real module | ||
(async () => { | ||
const names = [ | ||
getUser('sindresorhus').then(info => info.name), | ||
'Addy Osmani', | ||
'Pascal Hartig', | ||
'Stephen Sawchuk' | ||
]; | ||
const names = [ | ||
getUser('sindresorhus').then(info => info.name), | ||
'Addy Osmani', | ||
'Pascal Hartig', | ||
'Stephen Sawchuk' | ||
]; | ||
const totalAge = await pReduce(names, async (total, name) => { | ||
const info = await humanInfo(name); | ||
return total + info.age; | ||
}, 0); | ||
const totalAge = await pReduce(names, async (total, name) => { | ||
const info = await humanInfo(name); | ||
return total + info.age; | ||
}, 0); | ||
console.log(totalAge); | ||
//=> 125 | ||
})(); | ||
``` | ||
*/ | ||
<ValueType, ReducedValueType = ValueType>( | ||
input: Iterable<PromiseLike<ValueType> | ValueType>, | ||
reducer: pReduce.ReducerFunction<ValueType, ReducedValueType>, | ||
initialValue?: ReducedValueType | ||
): Promise<ReducedValueType>; | ||
// TODO: Remove this for the next major release, refactor the whole definition to: | ||
// declare function pReduce<ValueType, ReducedValueType = ValueType>( | ||
// input: Iterable<PromiseLike<ValueType> | ValueType>, | ||
// reducer: pReduce.ReducerFunction<ValueType, ReducedValueType>, | ||
// initialValue?: ReducedValueType | ||
// ): Promise<ReducedValueType>; | ||
// export = pReduce; | ||
default: typeof pReduce; | ||
}; | ||
export = pReduce; | ||
console.log(totalAge); | ||
//=> 125 | ||
``` | ||
*/ | ||
export default function pReduce<ValueType, ReducedValueType = ValueType>( | ||
input: Iterable<PromiseLike<ValueType> | ValueType>, | ||
reducer: ReducerFunction<ValueType, ReducedValueType>, | ||
initialValue?: ReducedValueType | ||
): Promise<ReducedValueType>; |
44
index.js
@@ -1,28 +0,24 @@ | ||
'use strict'; | ||
export default async function pReduce(iterable, reducer, initialValue) { | ||
return new Promise((resolve, reject) => { | ||
const iterator = iterable[Symbol.iterator](); | ||
let index = 0; | ||
const pReduce = (iterable, reducer, initialValue) => new Promise((resolve, reject) => { | ||
const iterator = iterable[Symbol.iterator](); | ||
let index = 0; | ||
const next = async total => { | ||
const element = iterator.next(); | ||
const next = async total => { | ||
const element = iterator.next(); | ||
if (element.done) { | ||
resolve(total); | ||
return; | ||
} | ||
if (element.done) { | ||
resolve(total); | ||
return; | ||
} | ||
try { | ||
const [resolvedTotal, resolvedValue] = await Promise.all([total, element.value]); | ||
next(reducer(resolvedTotal, resolvedValue, index++)); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}; | ||
try { | ||
const value = await Promise.all([total, element.value]); | ||
next(reducer(value[0], value[1], index++)); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}; | ||
next(initialValue); | ||
}); | ||
module.exports = pReduce; | ||
// TODO: Remove this for the next major release | ||
module.exports.default = pReduce; | ||
next(initialValue); | ||
}); | ||
} |
{ | ||
"name": "p-reduce", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Reduce a list of values using promises into a promise for a value", | ||
"license": "MIT", | ||
"repository": "sindresorhus/p-reduce", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=12" | ||
}, | ||
@@ -35,7 +38,7 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"delay": "^4.1.0", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
"ava": "^3.15.0", | ||
"delay": "^5.0.0", | ||
"tsd": "^0.14.0", | ||
"xo": "^0.38.2" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# p-reduce [![Build Status](https://travis-ci.org/sindresorhus/p-reduce.svg?branch=master)](https://travis-ci.org/sindresorhus/p-reduce) | ||
# p-reduce | ||
@@ -7,3 +7,2 @@ > Reduce a list of values using promises into a promise for a value | ||
## Install | ||
@@ -15,31 +14,27 @@ | ||
## Usage | ||
```js | ||
const pReduce = require('p-reduce'); | ||
const humanInfo = require('human-info'); // Not a real module | ||
import pReduce from 'p-reduce'; | ||
import humanInfo from 'human-info'; // Not a real module | ||
(async () => { | ||
const names = [ | ||
getUser('sindresorhus').then(info => info.name), | ||
'Addy Osmani', | ||
'Pascal Hartig', | ||
'Stephen Sawchuk' | ||
]; | ||
const names = [ | ||
getUser('sindresorhus').then(info => info.name), | ||
'Addy Osmani', | ||
'Pascal Hartig', | ||
'Stephen Sawchuk' | ||
]; | ||
const totalAge = await pReduce(names, async (total, name) => { | ||
const info = await humanInfo(name); | ||
return total + info.age; | ||
}, 0); | ||
const totalAge = await pReduce(names, async (total, name) => { | ||
const info = await humanInfo(name); | ||
return total + info.age; | ||
}, 0); | ||
console.log(totalAge); | ||
//=> 125 | ||
})(); | ||
console.log(totalAge); | ||
//=> 125 | ||
``` | ||
## API | ||
### pReduce(input, reducer, [initialValue]) | ||
### pReduce(input, reducer, initialValue?) | ||
@@ -66,3 +61,2 @@ Returns a `Promise` that is fulfilled when all promises in `input` and ones returned from `reducer` are fulfilled, or rejects if any of the promises reject. The fulfilled value is the result of the reduction. | ||
## Related | ||
@@ -75,5 +69,12 @@ | ||
--- | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-p-reduce?utm_source=npm-p-reduce&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77
Yes
5894
54