fix(Switch Node): Fix issue preventing some regex patterns from working (#8422)

This commit is contained in:
Jon
2024-01-24 09:30:09 +00:00
committed by GitHub
parent 7660d7e735
commit e9fea16301
3 changed files with 388 additions and 2 deletions

View File

@@ -125,6 +125,19 @@ function parseFilterConditionValues(
return { ok: true, result: { left: parsedLeftValue.newValue, right: parsedRightValue.newValue } };
}
function parseRegexPattern(pattern: string): RegExp {
const regexMatch = (pattern || '').match(new RegExp('^/(.*?)/([gimusy]*)$'));
let regex: RegExp;
if (!regexMatch) {
regex = new RegExp((pattern || '').toString());
} else {
regex = new RegExp(regexMatch[1], regexMatch[2]);
}
return regex;
}
export function executeFilterCondition(
condition: FilterConditionValue,
filterOptions: FilterOptionsValue,
@@ -183,9 +196,9 @@ export function executeFilterCondition(
case 'notEndsWith':
return !left.endsWith(right);
case 'regex':
return new RegExp(right).test(left);
return parseRegexPattern(right).test(left);
case 'notRegex':
return !new RegExp(right).test(left);
return !parseRegexPattern(right).test(left);
}
break;

View File

@@ -431,6 +431,7 @@ describe('FilterParameter', () => {
{ left: 'any string', right: '[0-9]', expected: false },
{ left: 'any string', right: '[a-z]', expected: true },
{ left: 'lowercase', right: '[A-Z]', expected: false },
{ left: 'foo', right: '/^fo{2}$/g', expected: true },
])('string:regex("$left","$right") === $expected', ({ left, right, expected }) => {
const result = executeFilter(
filterFactory({
@@ -454,6 +455,7 @@ describe('FilterParameter', () => {
{ left: 'any string', right: '[0-9]', expected: true },
{ left: 'any string', right: '[a-z]', expected: false },
{ left: 'lowercase', right: '[A-Z]', expected: true },
{ left: 'foo', right: '/^fo{2}$/g', expected: false },
])('string:notRegex("$left","$right") === $expected', ({ left, right, expected }) => {
const result = executeFilter(
filterFactory({