Skip to content

Commit

Permalink
feat: update regex to math operators and text operators
Browse files Browse the repository at this point in the history
  • Loading branch information
YounixM authored and palashgdev committed Oct 6, 2023
1 parent cf77fae commit 6eac8e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/hooks/queryBuilder/useAutoComplete.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 6eac8e6

Please sign in to comment.