Value mappings: add regex value mapping. (#38931)

This commit is contained in:
Jim McDonald
2021-09-14 17:15:14 -07:00
committed by GitHub
parent 15e278e9e1
commit 1aaa00a1e4
9 changed files with 174 additions and 1 deletions
@@ -157,6 +157,50 @@ describe('Format value', () => {
expect(result.text).toEqual('elva');
});
it('should replace a matching regex', () => {
const valueMappings: ValueMapping[] = [
{ type: MappingType.RegexToText, options: { pattern: '([^.]*).example.com', result: { text: '$1' } } },
];
const instance = getDisplayProcessorFromConfig({ decimals: 1, mappings: valueMappings });
const result = instance('hostname.example.com');
expect(result.text).toEqual('hostname');
});
it('should not replace a non-matching regex', () => {
const valueMappings: ValueMapping[] = [
{ type: MappingType.RegexToText, options: { pattern: '([^.]*).example.com', result: { text: '$1' } } },
];
const instance = getDisplayProcessorFromConfig({ decimals: 1, mappings: valueMappings });
const result = instance('hostname.acme.com');
expect(result.text).toEqual('hostname.acme.com');
});
it('should empty a matching regex without replacement', () => {
const valueMappings: ValueMapping[] = [
{ type: MappingType.RegexToText, options: { pattern: '([^.]*).example.com', result: { text: '' } } },
];
const instance = getDisplayProcessorFromConfig({ decimals: 1, mappings: valueMappings });
const result = instance('hostname.example.com');
expect(result.text).toEqual('');
});
it('should not empty a non-matching regex', () => {
const valueMappings: ValueMapping[] = [
{ type: MappingType.RegexToText, options: { pattern: '([^.]*).example.com', result: { text: '' } } },
];
const instance = getDisplayProcessorFromConfig({ decimals: 1, mappings: valueMappings });
const result = instance('hostname.acme.com');
expect(result.text).toEqual('hostname.acme.com');
});
it('should return value with color if mapping has color', () => {
const valueMappings: ValueMapping[] = [{ type: MappingType.ValueToText, options: { Low: { color: 'red' } } }];