UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

33 lines (32 loc) 1.09 kB
const require_last = require("../../array/last.js"); const require_toArray = require("../_internal/toArray.js"); const require_isArrayLike = require("../predicate/isArrayLike.js"); //#region src/compat/array/last.ts /** * Returns the last element of an array. * * This function takes an array and returns the last element of the array. * If the array is empty, the function returns `undefined`. * * Unlike some implementations, this function is optimized for performance * by directly accessing the last index of the array. * * @template T - The type of elements in the array. * @param arr - The array from which to get the last element. * @returns The last element of the array, or `undefined` if the array is empty. * * @example * const arr = [1, 2, 3]; * const lastElement = last(arr); * // lastElement will be 3 * * const emptyArr: number[] = []; * const noElement = last(emptyArr); * // noElement will be undefined */ function last(array) { if (!require_isArrayLike.isArrayLike(array)) return; return require_last.last(require_toArray.toArray(array)); } //#endregion exports.last = last;