Matchers: Require explicit regexp syntax in byRefId matcher (#96358)

This commit is contained in:
Leon Sorokin
2024-11-13 14:57:17 -06:00
committed by GitHub
parent bfecce320a
commit 7038bbe318
2 changed files with 15 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
import { stringToJsRegex } from '../../text/string'; import { stringStartsAsRegEx, stringToJsRegex } from '../../text/string';
import { DataFrame } from '../../types/dataFrame'; import { DataFrame } from '../../types/dataFrame';
import { FrameMatcherInfo } from '../../types/transformations'; import { FrameMatcherInfo } from '../../types/transformations';
@@ -12,9 +12,20 @@ const refIdMatcher: FrameMatcherInfo<string> = {
defaultOptions: 'A', defaultOptions: 'A',
get: (pattern: string) => { get: (pattern: string) => {
const regex = stringToJsRegex(pattern); let regex: RegExp | null = null;
if (stringStartsAsRegEx(pattern)) {
try {
regex = stringToJsRegex(pattern);
} catch (error) {
if (error instanceof Error) {
console.warn(error.message);
}
}
}
return (frame: DataFrame) => { return (frame: DataFrame) => {
return regex.test(frame.refId || ''); return regex?.test(frame.refId || '') ?? frame.refId === pattern;
}; };
}, },

View File

@@ -42,7 +42,7 @@ describe('filterByRefId transformer', () => {
const cfg = { const cfg = {
id: DataTransformerID.filterByRefId, id: DataTransformerID.filterByRefId,
options: { options: {
include: 'A|B', include: '/A|B/',
}, },
}; };