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