@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.
36 lines (35 loc) • 1.14 kB
TypeScript
/**
* Represents a searchable item as a key-value record.
* Any value type is allowed since the search functionality will cast to string for comparison.
*
* @example
* // A sample SearchItem could look like this:
* const item: SearchItem = { name: "John", age: 30, address: "123 Main St" };
*/
export type SearchItem = Record<string, any>;
/**
* Describes the options for the search function including the text to search for, the items to search within,
* the keys in the items to consider, whether to include or exclude the specified keys, and whether the search should match exactly.
*
* @example
* // An example of SearchOptions usage:
* const options: SearchOptions = {
* searchText: 'John',
* searchItems: [{ name: 'John Doe', age: 28 }, { name: 'Jane Doe', age: 32 }],
* keys: ['name'],
* include: true,
* exact: false
* };
*/
export interface SearchOptions<T extends SearchItem> {
searchText: string;
searchItems: T | T[];
keys?: KeyOf<T>[];
include?: boolean;
exact?: boolean;
}
/**
* Represents a key of a given object type.
* @alias keyof
*/
export type KeyOf<T> = keyof T;