import {findAbsoluteValueOrDefault} from '../internal/array/find'; import type {PlainObject} from '../models'; // #region Functions /** * Get the last items matching the given value * * @param array Array to search in * @param callback Callback to get an item's value for matching * @param value Value to match against * @returns Last item that matches the value, or `undefined` if no match is found * * @example * ```typescript * last( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * item => item.value, * 10, * ); // => {id: 3, value: 10} * ``` */ export function last unknown>( array: Item[], callback: Callback, value: ReturnType, ): Item | undefined; /** * Get the first item matching the given value by key * * @param array Array to search in * @param key Key to get an item's value for matching * @param value Value to match against * @returns Last item that matches the value, or `undefined` if no match is found * * @example * ```typescript * last( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * 'value', * 10, * ); // => {id: 3, value: 10} * ``` */ export function last( array: Item[], key: ItemKey, value: Item[ItemKey], ): Item | undefined; /** * Get the last item matching the filter * * @param array Array to search in * @param filter Filter callback to match items * @returns Last item that matches the filter, or `undefined` if no match is found * * @example * ```typescript * last( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * item => item.value === 10, * ); // => {id: 3, value: 10} * ``` */ export function last( array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, ): Item | undefined; /** * Get the last item from an array * * @param array Array to get from * @returns Last item from the array, or `undefined` if the array is empty * * @example * ```typescript * last([1, 2, 3]); // => 3 * ``` */ export function last(array: Item[]): Item | undefined; export function last(array: unknown[], ...parameters: unknown[]): unknown { return findAbsoluteValueOrDefault(array, parameters, undefined, false, true); } last.default = lastOrDefault; /** * Get the last item matching the given value * * _Available as `lastOrDefault` and `last.default`_ * * @param array Array to search in * @param defaultValue Default value to return if no match is found * @param callback Callback to get an item's value for matching * @param value Value to match against * @returns Last item that matches the value, or the default value if no match is found * * @example * ```typescript * lastOrDefault( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * {id: -1, value: 30}, * item => item.value, * 30, * ); // => {id: -1, value: 30} * ``` */ export function lastOrDefault< Item, Callback extends (item: Item, index: number, array: Item[]) => unknown, >(array: Item[], defaultValue: Item, callback: Callback, value: ReturnType): Item; /** * Get the last item matching the given value by key * * _Available as `lastOrDefault` and `last.default`_ * * @param array Array to search in * @param defaultValue Default value to return if no match is found * @param key Key to get an item's value for matching * @param value Value to match against * @returns Last item that matches the value, or the default value if no match is found * * @example * ```typescript * lastOrDefault( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * {id: -1, value: 30}, * 'value', * 30, * ); // => {id: -1, value: 30} * ``` */ export function lastOrDefault( array: Item[], defaultValue: Item, key: ItemKey, value: Item[ItemKey], ): Item; /** * Get the last item matching the filter * * _Available as `lastOrDefault` and `last.default`_ * * @param array Array to search in * @param defaultValue Default value to return if no match is found * @param filter Filter callback to match items * @returns Last item that matches the filter, or the default value if no match is found * * @example * ```typescript * lastOrDefault( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * {id: -1, value: 30}, * item => item.value === 30, * ); // => {id: -1, value: 30} * ``` */ export function lastOrDefault( array: Item[], defaultValue: Item, filter: (item: Item, index: number, array: Item[]) => boolean, ): Item; /** * Get the last item from an array * * _Available as `lastOrDefault` and `last.default`_ * * @param array Array to get from * @param defaultValue Default value to return if the array is empty * @returns Last item from the array, or the default value if the array is empty * * @example * ```typescript * lastOrDefault([], {id: -1, value: 30}); // => {id: -1, value: 30} * ``` */ export function lastOrDefault(array: Item[], defaultValue: Item): Item; export function lastOrDefault( array: unknown[], defaultValue: unknown, ...parameters: unknown[] ): unknown { return findAbsoluteValueOrDefault(array, parameters, defaultValue, true, true); } // #endregion