import {FIND_VALUE_INDEX, findValue} from './find'; import type {PlainObject} from '../../models'; // #region Functions /** * Get the index of the first matching item by callback * * @param array Array to search in * @param callback Callback to get an item's value * @param value Value to match against * @returns Index of the first matching item, or `-1` if no match is found * * @example * ```typescript * indexOf( * [{id: 1}, {id: 2}, {id: 3}], * item => item.id, * 2, * ); // => 1 * ``` */ export function indexOf< Item, Callback extends (item: Item, index: number, array: Item[]) => unknown, >(array: Item[], callback: Callback, value: ReturnType): number; /** * Get the index of the first matching item by key * * @param array Array to search in * @param key Key to match items by * @param value Value to match against * @returns Index of the first matching item, or `-1` if no match is found * * @example * ```typescript * indexOf( * [{id: 1}, {id: 2}, {id: 3}], * 'id', * 2, * ); // => 1 * ``` */ export function indexOf( array: Item[], key: ItemKey, value: Item[ItemKey], ): number; /** * Get the index of the first item matching the filter * * @param array Array to search in * @param filter Filter callback to match items * @returns Index of the first matching item, or `-1` if no match is found * * @example * ```typescript * indexOf( * [{id: 1}, {id: 2}, {id: 3}], * item => item.id === 2, * ); // => 1 * ``` */ export function indexOf( array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, ): number; /** * Get the index of the first item matching the given item * * @param array Array to search in * @param item Item to match against * @returns Index of the first matching item, or `-1` if no match is found * * @example * ```typescript * indexOf([1, 2, 3, 4, 5], 3); // => 2 * ``` */ export function indexOf(array: Item[], item: Item): number; export function indexOf(array: unknown[], ...parameters: unknown[]): number { return findValue(FIND_VALUE_INDEX, array, parameters, false); } indexOf.last = lastIndexOf; /** * Get the index of the last matching item by callback * * _Available as `lastIndexOf` and `indexOf.last`_ * * @param array Array to search in * @param callback Callback to get an item's value * @param value Value to match against * @returns Index of the last matching item, or `-1` if no match is found * * @example * ```typescript * lastIndexOf( * [{id: 1}, {id: 2}, {id: 3}, {id: 2}], * item => item.id, * 2, * ); // => 3 * ``` */ export function lastIndexOf< Item, Callback extends (item: Item, index: number, array: Item[]) => unknown, >(array: Item[], callback: Callback, value: ReturnType): number; /** * Get the index of the last matching item by key * * _Available as `lastIndexOf` and `indexOf.last`_ * * @param array Array to search in * @param key Key to match items by * @param value Value to match against * @returns Index of the last matching item, or `-1` if no match is found * * @example * ```typescript * lastIndexOf( * [{id: 1}, {id: 2}, {id: 3}, {id: 2}], * 'id', * 2, * ); // => 3 * ``` */ export function lastIndexOf( array: Item[], key: ItemKey, value: Item[ItemKey], ): number; /** * Get the index of the last item matching the filter * * _Available as `lastIndexOf` and `indexOf.last`_ * * @param array Array to search in * @param filter Filter callback to match items * @returns Index of the last matching item, or `-1` if no match is found * * @example * ```typescript * lastIndexOf( * [{id: 1}, {id: 2}, {id: 3}, {id: 2}], * item => item.id === 2, * ); // => 3 * ``` */ export function lastIndexOf( array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, ): number; /** * Get the index of the last item matching the given item * * _Available as `lastIndexOf` and `indexOf.last`_ * * @param array Array to search in * @param item Item to match against * @returns Index of the last matching item, or `-1` if no match is found * * @example * ```typescript * lastIndexOf([1, 2, 3, 2, 1], 2); // => 3 * ``` */ export function lastIndexOf(array: Item[], item: Item): number; export function lastIndexOf(array: unknown[], ...parameters: unknown[]): number { return findValue(FIND_VALUE_INDEX, array, parameters, true); } // #endregion