UNPKG

typedash

Version:

modern, type-safe collection of utility functions

35 lines (32 loc) 1.16 kB
import { M as Maybe } from '../Maybe-D6dwMjD9.js'; /** * Computes the sum of all values in array. If array is empty or nil, `0` is returned. * `null` or `undefined` values are treated as `0`. * @param array The array to iterate over. * @returns The sum of all values in the array, or `0` if the array is empty or nil. * @example * ```ts * sum([1, 2, 3]); // 6 * sum([]); // 0 * sum(null); // 0 * sum([1, 2, null, 3, undefined, 4]); // 10 * ``` */ declare function sum(array: Maybe<ReadonlyArray<Maybe<number>>>): number; /** * Computes the sum of all values in array. If array is empty or nil, `0` is returned. * @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. * @example * ```ts * sum([ * { value: 1 }, * { value: 2 }, * { value: 3 } * ], (element) => element.value); // 6 * ``` */ declare function sum<T>(array: Maybe<readonly T[]>, mapper: ArrayIterator<T>): number; type ArrayIterator<T> = (value: T, index: number, array: readonly T[]) => number; export { sum };