Add regex support

Reviewed By: LukeDefeo

Differential Revision: D51198464

fbshipit-source-id: 445cc47f90c2730f3b0728e5bf667330274d103d
This commit is contained in:
Andrey Goncharov
2023-11-10 04:34:09 -08:00
committed by Facebook GitHub Bot
parent d023bcc42e
commit a1070b8cea
2 changed files with 36 additions and 0 deletions

View File

@@ -21,6 +21,11 @@ export type PowerSearchOperatorProcessor = (
) => boolean;
export const dataTablePowerSearchOperators = {
string_matches_regex: () => ({
label: 'matches regex',
key: 'string_matches_regex',
valueType: 'STRING',
}),
string_contains: () => ({
label: 'contains',
key: 'string_contains',
@@ -46,6 +51,11 @@ export const dataTablePowerSearchOperators = {
key: 'searializable_object_contains',
valueType: 'STRING',
}),
searializable_object_matches_regex: () => ({
label: 'matches regex',
key: 'searializable_object_matches_regex',
valueType: 'STRING',
}),
searializable_object_not_contains: () => ({
label: 'does not contain',
key: 'searializable_object_not_contains',
@@ -217,7 +227,23 @@ const tryConvertingUnknownToString = (value: unknown): string | null => {
}
};
const regexCache: Record<string, RegExp> = {};
function safeCreateRegExp(source: string): RegExp | undefined {
try {
if (!regexCache[source]) {
regexCache[source] = new RegExp(source);
}
return regexCache[source];
} catch (_e) {
return undefined;
}
}
export const dataTablePowerSearchOperatorProcessorConfig = {
string_matches_regex: (_operator, searchValue: string, value: string) =>
!!safeCreateRegExp(searchValue)?.test(
tryConvertingUnknownToString(value) ?? '',
),
string_contains: (_operator, searchValue: string, value: string) =>
!!tryConvertingUnknownToString(value)
?.toLowerCase()
@@ -226,6 +252,11 @@ export const dataTablePowerSearchOperatorProcessorConfig = {
!tryConvertingUnknownToString(value)
?.toLowerCase()
.includes(searchValue.toLowerCase()),
searializable_object_matches_regex: (
_operator,
searchValue: string,
value: object,
) => !!safeCreateRegExp(searchValue)?.test(JSON.stringify(value)),
searializable_object_contains: (
_operator,
searchValue: string,

View File

@@ -119,6 +119,8 @@ const powerSearchConfigEntireRow: FieldConfig = {
dataTablePowerSearchOperators.searializable_object_contains(),
searializable_object_not_contains:
dataTablePowerSearchOperators.searializable_object_not_contains(),
searializable_object_matches_regex:
dataTablePowerSearchOperators.searializable_object_matches_regex(),
},
useWholeRow: true,
};
@@ -388,6 +390,7 @@ export function DataTable<T extends object>(
dataTablePowerSearchOperators.string_not_matches_exactly(),
dataTablePowerSearchOperators.string_set_contains_any_of(),
dataTablePowerSearchOperators.string_set_contains_none_of(),
dataTablePowerSearchOperators.string_matches_regex(),
];
} else if (Array.isArray(column.powerSearchConfig)) {
columnPowerSearchOperators = column.powerSearchConfig;
@@ -433,6 +436,7 @@ export function DataTable<T extends object>(
dataTablePowerSearchOperators.string_not_matches_exactly(),
dataTablePowerSearchOperators.string_set_contains_any_of(),
dataTablePowerSearchOperators.string_set_contains_none_of(),
dataTablePowerSearchOperators.string_matches_regex(),
];
break;
}
@@ -483,6 +487,7 @@ export function DataTable<T extends object>(
columnPowerSearchOperators = [
dataTablePowerSearchOperators.searializable_object_contains(),
dataTablePowerSearchOperators.searializable_object_not_contains(),
dataTablePowerSearchOperators.searializable_object_matches_regex(),
];
break;
}