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

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

* Fixed test name
This commit is contained in:
Torkel Ödegaard
2021-11-29 16:03:13 +01:00
committed by GitHub
parent becfd776c3
commit c3d0d37bd7
2 changed files with 18 additions and 3 deletions
@@ -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', () => {
@@ -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: