This particular situation was a bit perplexing for me. When I use the search function, I am seeking substring matches of the search keywords. Therefore, before comparing any object, I convert all properties to strings and check for substrings.
Based on my understanding, everything in JavaScript has a .toString
method. Even when there is ambiguity in types, calling the method should not pose an issue.
For further clarity, here is the code snippet:
const results = rows.filter((row) =>
fields.some((field) =>
row[field].toString().toLowerCase().includes(search)
)
);
The variable rows
is of type ListFields[L]
, where L
is a generic that extends keyof ListFields
. The variable fields
is an array of type keyof ListFields[L]
, so accessing non-existent properties should not be a concern.
Below is the definition for ListFields
:
type ListFields = {
Users: MetaData &
LocationData & {
wuNumber: string;
dispName: string;
firstName: string;
lastName: string;
};
// ... Other similar list definition
}
// Definitions for MetaData and LocationData if needed:
type MetaData = {
Title: string | null;
FileSystemObjectType: number;
Id: number;
ServerRedirectedEmbedUri: null | string;
ServerRedirectedEmbedUrl: string;
ID: number;
ContentTypeId: string;
Modified: string;
Created: string;
AuthorId: number;
EditorId: number;
OData__UIVersionString: string;
Attachments: boolean;
GUID: string;
ComplianceAssetId: null | number;
};
type LocationData = {
Address: string;
CountryOrRegion: string;
State: string;
City: string;
PostalCode: string;
Street: string;
GeoLoc: string;
};