UNPKG

757 BJavaScriptView Raw
1import { identity } from '../util/identity';
2/**
3 * Computes the average of the iterable sequence.
4 *
5 * @export
6 * @param {Iterable<any>} source The source iterable sequence to compute the average.
7 * @param {MathOptions<any>} [options] The options for calculating the average.
8 * @returns {number} The computed average for the iterable sequence.
9 */
10export function average(source, options) {
11 const { ['selector']: selector = identity, ['thisArg']: thisArg } = options || {};
12 let sum = 0;
13 let count = 0;
14 for (const item of source) {
15 sum += selector.call(thisArg, item);
16 count++;
17 }
18 if (count === 0) {
19 throw new Error('Empty collection');
20 }
21 return sum / count;
22}
23
24//# sourceMappingURL=average.mjs.map