Returns the greatest value from a list.
max([2, 3, 5]); // => 5max([{ num: 1 }, { num: 2 }], (x) => x.num); // => { num: 2 }
npx atmx add helper max
Copy and paste the following method into @/utils/helpers/undefined.ts:
@/utils/helpers/undefined.ts
/*** Returns the greatest value from a list.** @example* max([2, 3, 5]) // => 5* max([{ num: 1 }, { num: 2 }], x => x.num) // => { num: 2 }*/export function max(array: readonly [number, ...number[]]): number;export function max(array: readonly number[]): number | null;export function max<T>(array: readonly T[],getter: (item: T) => number,): T | null;export function max<T>(array: readonly T[],getter?: (item: T) => number,): T | null {if (!array || (array.length ?? 0) === 0) {return null;} const get = getter ?? ((v: any) => v); return array.reduce((a, b) => (get(a) > get(b) ? a : b));}