typedash
Version:
modern, type-safe collection of utility functions
36 lines (35 loc) • 1.25 kB
text/typescript
import { t as Maybe } from "./Maybe-BVpZiDsE.cjs";
//#region src/functions/sum/sum.d.ts
/**
* 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<readonly 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;
//#endregion
export { sum as t };
//# sourceMappingURL=sum-D-3DSFvm.d.cts.map