29 lines
765 B
TypeScript
29 lines
765 B
TypeScript
/*fields example = [
|
|
['id'],
|
|
['companyInformation', 'companyInformation', 'name']
|
|
]*/
|
|
|
|
const getFieldValue = (fields: string[], data: any): string => {
|
|
if (fields.length === 0) return data;
|
|
const [key, ...otherFields] = fields;
|
|
|
|
if (data[key]) return getFieldValue(otherFields, data[key]);
|
|
return data;
|
|
};
|
|
|
|
export const search = (text: string, fields: string[][], rows: any[]) => {
|
|
const searchText = text.toLowerCase();
|
|
return rows.filter((row) => {
|
|
return fields.some((fieldsKeys) => {
|
|
const value = getFieldValue(fieldsKeys, row);
|
|
if (typeof value === "string") {
|
|
return value.toLowerCase().includes(searchText);
|
|
}
|
|
|
|
if (typeof value === "number") {
|
|
return (value as Number).toString().includes(searchText);
|
|
}
|
|
});
|
|
});
|
|
};
|