import { identityAsync } from '../util/identity.js'; import { wrapWithAbort } from './operators/withabort.js'; import { throwIfAborted } from '../aborterror.js'; import { MathOptions } from './mathoptions.js'; /** * Computes the sum of a sequence of values. * * @param {AsyncIterable} source A sequence of values to calculate the sum. * @param {MathOptions} [options] Optional options for providing a selector, thisArg and abort signal. * @returns {Promise} A promise containing the sum of the sequence of values. */ export async function sum( source: AsyncIterable, options?: MathOptions ): Promise; /** * Computes the sum of a sequence of values. * * @template T The type of values in the source sequence. * @param {AsyncIterable} source A sequence of values to calculate the sum. * @param {MathOptions} [options] Optional options for providing a selector, thisArg and abort signal. * @returns {Promise} A promise containing the sum of the sequence of values. */ export async function sum(source: AsyncIterable, options?: MathOptions): Promise; /** * Computes the sum of a sequence of values. * * @param {AsyncIterable} source A sequence of values to calculate the sum. * @param {MathOptions} [options] Optional options for providing a selector, thisArg and abort signal. * @returns {Promise} A promise containing the sum of the sequence of values. */ export async function sum(source: AsyncIterable, options?: MathOptions): Promise { const { ['selector']: selector = identityAsync as any, ['signal']: signal, ['thisArg']: thisArg, } = options || {}; throwIfAborted(signal); let value = 0; for await (const item of wrapWithAbort(source, signal)) { value += await selector.call(thisArg, item, signal); } return value; }