Transformations: Relax regexp format in refIdMatcher (#97216)

This commit is contained in:
Leon Sorokin 2024-12-02 13:05:47 -06:00 committed by GitHub
parent acf67d5911
commit 88621d6fa0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import { stringStartsAsRegEx, stringToJsRegex } from '../../text/string';
import { escapeStringForRegex, stringStartsAsRegEx, stringToJsRegex } from '../../text/string';
import { DataFrame } from '../../types/dataFrame';
import { FrameMatcherInfo } from '../../types/transformations';
@ -23,6 +23,12 @@ const refIdMatcher: FrameMatcherInfo<string> = {
}
}
}
// old format that was simply unescaped pipe-joined strings -> regexp
else if (pattern.includes('|')) {
// convert A|B -> /^(?:A|B)$/, regexp-escaping all chars between pipes
const escapedUnion = pattern.split('|').map(escapeStringForRegex).join('|');
regex = new RegExp(`^(?:${escapedUnion})$`);
}
return (frame: DataFrame) => {
return regex?.test(frame.refId || '') ?? frame.refId === pattern;

View File

@ -37,8 +37,22 @@ describe('filterByRefId transformer', () => {
});
});
describe('respects', () => {
it('inclusion', async () => {
describe('respects inclusion', () => {
it('pipe delimited literals', async () => {
const cfg = {
id: DataTransformerID.filterByRefId,
options: {
include: 'A|B',
},
};
await expect(transformDataFrame([cfg], allSeries)).toEmitValuesWith((received) => {
const filtered = received[0];
expect(filtered.map((f) => f.refId)).toEqual(['A', 'B']);
});
});
it('explicit regexp', async () => {
const cfg = {
id: DataTransformerID.filterByRefId,
options: {