ValueMappings: Fixes issue with regex value mapping that only sets color (#42311) (#42457)

* ValueMappings: Fixes issue with regex value mapping that only sets color

* Fixed test name

(cherry picked from commit c3d0d37bd7)

Co-authored-by: Torkel Ödegaard <torkel@grafana.org>
This commit is contained in:
Grot (@grafanabot) 2021-11-29 10:19:20 -05:00 committed by GitHub
parent 66ae92abe9
commit 0ca6910f55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -67,6 +67,13 @@ const testSet2: ValueMapping[] = [
result: { text: 'Hostname $1' },
},
},
{
type: MappingType.RegexToText,
options: {
pattern: '/hello/',
result: { color: 'red' },
},
},
];
describe('Format value with value mappings', () => {
@ -164,6 +171,10 @@ describe('Format value with regex mappings', () => {
const value = 'www.baz.com';
expect(getValueMappingResult(testSet2, value)).toBeNull();
});
it('should not replace match when replace text is null', () => {
expect(getValueMappingResult(testSet2, 'hello my name is')).toEqual({ color: 'red' });
});
});
describe('isNumeric', () => {

View File

@ -58,9 +58,13 @@ export function getValueMappingResult(valueMappings: ValueMapping[], value: any)
const regex = stringToJsRegex(vm.options.pattern);
if (value.match(regex)) {
const thisResult = Object.create(vm.options.result);
thisResult.text = value.replace(regex, vm.options.result.text || '');
return thisResult;
const res = { ...vm.options.result };
if (res.text != null) {
res.text = value.replace(regex, vm.options.result.text || '');
}
return res;
}
case MappingType.SpecialValue: