@smakss/search
Version:
Enhance your searching capabilities with @smakss/search. Effortlessly find keywords in arrays, nested arrays, and objects, perfect for deep search needs in various data structures.
35 lines (34 loc) • 1.23 kB
TypeScript
import type { SearchItem, SearchOptions } from './types';
/**
* Searches for items within a collection that match the given search text.
*
* @param {SearchOptions} options - The search parameters including searchText, searchItems, keys to search in,
* whether to include keys and if the search is exact.
* @template T - The type of the items to search through, extending SearchItem.
* @returns {T[]} The matched items as an array.
*
* @example
* type Person = { name: string; lastName: string; }
* // Define a list of objects to search
* const people: Person[] = [
* { name: "John", lastName: "Doe" },
* { name: "Jane", lastName: "Smith" },
* ];
*
* // Options for searching
* const options = {
* searchText: "doe",
* searchItems: people,
* keys: ['lastName'],
* include: true,
* exact: false
* };
*
* // Perform the search
* const found = search<Person>(options);
*
* // found will contain the object with lastName 'Doe'
* console.log(found); // [{ name: "John", lastName: "Doe" }]
*/
declare function search<T extends SearchItem = SearchItem>({ searchText, searchItems, keys, include, exact }: SearchOptions<T>): T[];
export default search;