UNPKG

typedash

Version:

modern, type-safe collection of utility functions

17 lines (16 loc) 570 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 export { sum as t }; //# sourceMappingURL=sum-C9-v-uQo.js.map