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