From 6eac8e6d0b4c3dbc31ccf76ad3490aab5cae8d7e Mon Sep 17 00:00:00 2001 From: Yunus A M Date: Fri, 6 Oct 2023 13:21:54 +0530 Subject: [PATCH] feat: update regex to math operators and text operators --- .../filters/QueryBuilderSearch/utils.ts | 20 +++++++++++++++++-- .../src/hooks/queryBuilder/useAutoComplete.ts | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/frontend/src/container/QueryBuilder/filters/QueryBuilderSearch/utils.ts b/frontend/src/container/QueryBuilder/filters/QueryBuilderSearch/utils.ts index 0b29c5a8e8..11c06cc0be 100644 --- a/frontend/src/container/QueryBuilder/filters/QueryBuilderSearch/utils.ts +++ b/frontend/src/container/QueryBuilder/filters/QueryBuilderSearch/utils.ts @@ -3,21 +3,37 @@ import { parse } from 'papaparse'; import { orderByValueDelimiter } from '../OrderByFilter/utils'; +const operators = /=|!=|>=|>|<=|<$/; + // eslint-disable-next-line no-useless-escape -export const tagRegexp = /^\s*(.+?)\s+(IN|in|NOT_IN|not_in|LIKE|like|NOT_LIKE|not_like|REGEX|regex|NOT_REGEX|not_regex|=|!=|EXISTS|exists|NOT_EXISTS|not_exists|CONTAINS|contains|NOT_CONTAINS|not_contains|>=|>|<=|<|HAS|has|NHAS|nhas)\s*(.*)$/g; +export const tagRegexpV1 = /^\s*(.*?)\s*(IN|NOT_IN|LIKE|NOT_LIKE|REGEX|NOT_REGEX|=|!=|EXISTS|NOT_EXISTS|CONTAINS|NOT_CONTAINS|>=|>|<=|<|HAS|NHAS)\s*(.*)$/g; + +export const tagRegexpV2 = /^\s*(.+?)\s+(IN|in|NOT_IN|not_in|LIKE|like|NOT_LIKE|not_like|REGEX|regex|NOT_REGEX|not_regex|EXISTS|exists|NOT_EXISTS|not_exists|CONTAINS|contains|NOT_CONTAINS|not_contains|HAS|has|NHAS|nhas|=|!=|>=|>|<=|<)\s*(.*)$/g; export function isInNInOperator(value: string): boolean { return value === OPERATORS.IN || value === OPERATORS.NIN; } +function endsWithOperator(inputString: string): boolean { + return operators.test(inputString); +} + interface ITagToken { tagKey: string; tagOperator: string; tagValue: string[]; } +export function getMatchRegex(str: string): RegExp { + if (endsWithOperator(str)) { + return tagRegexpV1; + } + + return tagRegexpV2; +} + export function getTagToken(tag: string): ITagToken { - const matches = tag?.matchAll(tagRegexp); + const matches = tag?.matchAll(getMatchRegex(tag)); const [match] = matches ? Array.from(matches) : []; if (match) { diff --git a/frontend/src/hooks/queryBuilder/useAutoComplete.ts b/frontend/src/hooks/queryBuilder/useAutoComplete.ts index def235dfb5..9562f5e4d7 100644 --- a/frontend/src/hooks/queryBuilder/useAutoComplete.ts +++ b/frontend/src/hooks/queryBuilder/useAutoComplete.ts @@ -1,8 +1,8 @@ import { + getMatchRegex, getRemovePrefixFromKey, getTagToken, replaceStringWithMaxLength, - tagRegexp, } from 'container/QueryBuilder/filters/QueryBuilderSearch/utils'; import { Option } from 'container/QueryBuilder/type'; import { parse } from 'papaparse'; @@ -58,7 +58,7 @@ export const useAutoComplete = ( (value: string): void => { if (isMulti) { setSearchValue((prev: string) => { - const matches = prev?.matchAll(tagRegexp); + const matches = prev?.matchAll(getMatchRegex(prev)); const [match] = matches ? Array.from(matches) : []; const [, , , matchTagValue] = match; const data = parse(matchTagValue).data.flat();