2
0

🐛 (conditions) Parse regex flags as well

Closes #1393
This commit is contained in:
Baptiste Arnaud
2024-03-29 08:06:16 +01:00
parent 76e7fbd1c7
commit a0ba8c5c2a

View File

@ -126,15 +126,18 @@ const executeComparison =
case ComparisonOperators.MATCHES_REGEX: { case ComparisonOperators.MATCHES_REGEX: {
const matchesRegex = (a: string | null, b: string | null) => { const matchesRegex = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return false if (b === '' || !b || !a) return false
if (b.startsWith('/') && b.endsWith('/')) b = b.slice(1, -1) const regex = preprocessRegex(b)
return new RegExp(b).test(a) if (!regex) return false
return new RegExp(regex.pattern, regex.flags).test(a)
} }
return compare(matchesRegex, inputValue, value, 'some') return compare(matchesRegex, inputValue, value, 'some')
} }
case ComparisonOperators.NOT_MATCH_REGEX: { case ComparisonOperators.NOT_MATCH_REGEX: {
const matchesRegex = (a: string | null, b: string | null) => { const matchesRegex = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return false if (b === '' || !b || !a) return false
return !new RegExp(b).test(a) const regex = preprocessRegex(b)
if (!regex) return true
return !new RegExp(regex.pattern, regex.flags).test(a)
} }
return compare(matchesRegex, inputValue, value) return compare(matchesRegex, inputValue, value)
} }
@ -171,3 +174,11 @@ const parseDateOrNumber = (value: string): number => {
} }
return parsed return parsed
} }
const preprocessRegex = (regex: string) => {
const match = regex.match(/^\/([^\/]+)\/([gimuy]*)$/)
if (!match) return null
return { pattern: match[1], flags: match[2] }
}