import {FIND_VALUES_ALL, findValues} from '../internal/array/find'; import type {PlainObject} from '../models'; // #region Functions /** * Get a filtered array of items that do not match the value * * _Available as `exclude` and `filter.remove`_ * * @param array Array to search in * @param callback Callback to get an item's value for matching * @param value Value to match against * @returns Filtered array of items that do not match the filter * * @example * ```typescript * exclude( * [{id: 1}, {id: 2}, {id: 3}], * item => item.id, * 2, * ); // => [{id: 1}, {id: 3}] * ``` */ export function exclude< Item, Callback extends (item: Item, index: number, array: Item[]) => unknown, >(array: Item[], callback: Callback, value: ReturnType): unknown[]; /** * Get a filtered array of items that do not match the value * * _Available as `exclude` and `filter.remove`_ * * @param array Array to search in * @param key Key to get an item's value for matching * @param value Value to match against * @returns Filtered array of items that do not match the filter * * @example * ```typescript * exclude( * [{id: 1}, {id: 2}, {id: 3}], * 'id', * 2, * ); // => [{id: 1}, {id: 3}] * ``` */ export function exclude( array: Item[], key: ItemKey, value: Item[ItemKey], ): unknown[]; /** * Get a filtered array of items that do not match the filter * * _Available as `exclude` and `filter.remove`_ * * @param array Array to search in * @param filter Filter callback to match items * @returns Filtered array of items that do not match the filter * * @example * ```typescript * exclude( * [{id: 1}, {id: 2}, {id: 3}], * item => item.id === 2, * ); // => [{id: 1}, {id: 3}] * ``` */ export function exclude( array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, ): unknown[]; /** * Get a filtered array of items that do not match the given item * * _Available as `exclude` and `filter.remove`_ * * @param array Array to search in * @param item Item to match against * @returns Filtered array of items that do not match the given item * * @example * ```typescript * exclude([1, 2, 3], 2); // => [1, 3] * ``` */ export function exclude(array: Item[], item: Item): unknown[]; export function exclude(array: unknown[], ...parameters: unknown[]): unknown[] { return findValues(FIND_VALUES_ALL, array, parameters).notMatched; } /** * Get a filtered array of items * * @param array Array to search in * @param callback Callback to get an item's value for matching * @param value Value to match against * @returns Filtered array of items * * @example * ```typescript * filter( * [{id: 1}, {id: 2}, {id: 3}], * item => item.id, * 2, * ); // => [{id: 2}] * ``` */ export function filter< Item, Callback extends (item: Item, index: number, array: Item[]) => unknown, >(array: Item[], callback: Callback, value: ReturnType): Item[]; /** * Get a filtered array of items * * @param array Array to search in * @param key Key to get an item's value for matching * @param value Value to match against * @returns Filtered array of items * * @example * ```typescript * filter( * [{id: 1}, {id: 2}, {id: 3}], * 'id', * 2, * ); // => [{id: 2}] * ``` */ export function filter( array: Item[], key: ItemKey, value: Item[ItemKey], ): Item[]; /** * Get a filtered array of items matching the filter * * @param array Array to search in * @param filter Filter callback to match items * @returns Filtered array of items * * @example * ```typescript * filter( * [{id: 1}, {id: 2}, {id: 3}], * item => item.id === 2, * ); // => [{id: 2}] * ``` */ export function filter( array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, ): Item[]; /** * Get a filtered array of items matching the given item * * @param array Array to search in * @param item Item to match against * @returns Filtered array of items * * @example * ```typescript * filter([1, 2, 3], 2); // => [2] * ``` */ export function filter(array: Item[], item: Item): Item[]; export function filter(array: unknown[], ...parameters: unknown[]): unknown[] { return findValues(FIND_VALUES_ALL, array, parameters).matched; } filter.remove = exclude; // #endregion