UNPKG

typedash

Version:

modern, type-safe collection of utility functions

23 lines (21 loc) 656 B
//#region src/functions/sum/sum.ts /** * Implementation for all overloads. * @param array The array to iterate over. * @param mapper The function used to extract a numeric value from each element. * @returns The sum of all values in the array, or `0` if the array is empty or nil. */ function sum(array, mapper) { if (array == null) return 0; return array.map((value, index, array_) => { return mapper?.(value, index, array_) ?? value ?? 0; }).reduce((draft, value) => draft + value, 0); } //#endregion Object.defineProperty(exports, 'sum', { enumerable: true, get: function () { return sum; } }); //# sourceMappingURL=sum-CSt9AUZP.cjs.map