Get the last item in an array or a default value.
last([1, 2, 3, 4]);// 4 last([], 0);// 0
npx atmx add helper last
Copy and paste the following method into @/utils/helpers/undefined.ts:
@/utils/helpers/undefined.ts
/*** Get the last item in an array or a default value.** @example* last([1, 2, 3, 4])* // 4** last([], 0)* // 0*/export function last<T>(array: readonly T[]): T | undefined; export function last<T, U>(array: readonly T[], defaultValue: U): T | U; export function last(array: readonly unknown[], defaultValue?: unknown) {return array?.length > 0 ? array[array.length - 1] : defaultValue;}