mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Transformations: Allow more complex regex expressions in Rename by regex
(#48179)
* Support proper regex in renameByRegex transformation * remove unused import
This commit is contained in:
parent
e420252d45
commit
53fcb7e171
@ -80,6 +80,59 @@ describe('Rename By Regex Transformer', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to replace globally', async () => {
|
||||
const cfg: DataTransformerConfig<RenameByRegexTransformerOptions> = {
|
||||
id: DataTransformerID.renameByRegex,
|
||||
options: {
|
||||
regex: '/e/g',
|
||||
renamePattern: 'E',
|
||||
},
|
||||
};
|
||||
await expect(transformDataFrame([cfg], [data])).toEmitValuesWith((received) => {
|
||||
const data = received[0];
|
||||
const frame = data[0];
|
||||
expect(frame.fields).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
Object {
|
||||
"config": Object {
|
||||
"displayName": "TimE",
|
||||
"name": "Time",
|
||||
},
|
||||
"name": "Time",
|
||||
"state": Object {
|
||||
"displayName": "TimE",
|
||||
"multipleFrames": false,
|
||||
},
|
||||
"type": "time",
|
||||
"values": Array [
|
||||
3000,
|
||||
4000,
|
||||
5000,
|
||||
6000,
|
||||
],
|
||||
},
|
||||
Object {
|
||||
"config": Object {
|
||||
"displayName": "wEb-01.ExamplE.com",
|
||||
},
|
||||
"name": "Value",
|
||||
"state": Object {
|
||||
"displayName": "wEb-01.ExamplE.com",
|
||||
"multipleFrames": false,
|
||||
},
|
||||
"type": "number",
|
||||
"values": Array [
|
||||
10000.3,
|
||||
10000.4,
|
||||
10000.5,
|
||||
10000.6,
|
||||
],
|
||||
},
|
||||
]
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not rename misses', async () => {
|
||||
const cfg: DataTransformerConfig<RenameByRegexTransformerOptions> = {
|
||||
id: DataTransformerID.renameByRegex,
|
||||
@ -147,7 +200,6 @@ describe('Rename By Regex Transformer', () => {
|
||||
Array [
|
||||
Object {
|
||||
"config": Object {
|
||||
"displayName": "Time",
|
||||
"name": "Time",
|
||||
},
|
||||
"name": "Time",
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { getFieldDisplayName } from '../../field/fieldState';
|
||||
import { stringToJsRegex } from '../../text/string';
|
||||
import { DataFrame } from '../../types/dataFrame';
|
||||
import { DataTransformerInfo } from '../../types/transformations';
|
||||
|
||||
@ -47,7 +48,7 @@ export const renameByRegexTransformer: DataTransformerInfo<RenameByRegexTransfor
|
||||
};
|
||||
|
||||
const renameFieldsByRegex = (options: RenameByRegexTransformerOptions) => (frame: DataFrame) => {
|
||||
const regex = new RegExp(options.regex);
|
||||
const regex = stringToJsRegex(options.regex);
|
||||
const fields = frame.fields.map((field) => {
|
||||
const displayName = getFieldDisplayName(field, frame);
|
||||
if (!regex.test(displayName)) {
|
||||
|
Loading…
Reference in New Issue
Block a user