Value mappings: add regex value mapping. (#38931)
This commit is contained in:
1 parent
15e278e9e1
commit
1aaa00a1e4
9 files changed
+174
-1
No files matched your search
@@ -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' } } }];
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
export enum MappingType {
|
||||
ValueToText = 'value', // was 1
|
||||
RangeToText = 'range', // was 2
|
||||
RegexToText = 'regex',
|
||||
SpecialValue = 'special',
|
||||
}
|
||||
|
||||
@@ -47,6 +48,21 @@ export interface RangeMap extends BaseValueMap<RangeMapOptions> {
|
||||
type: MappingType.RangeToText;
|
||||
}
|
||||
|
||||
/**
|
||||
* @alpha
|
||||
*/
|
||||
export interface RegexMapOptions {
|
||||
pattern: string;
|
||||
result: ValueMappingResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* @alpha
|
||||
*/
|
||||
export interface RegexMap extends BaseValueMap<RegexMapOptions> {
|
||||
type: MappingType.RegexToText;
|
||||
}
|
||||
|
||||
/**
|
||||
* @alpha
|
||||
*/
|
||||
@@ -77,4 +93,4 @@ export interface SpecialValueMap extends BaseValueMap<SpecialValueOptions> {
|
||||
/**
|
||||
* @alpha
|
||||
*/
|
||||
export type ValueMapping = ValueMap | RangeMap | SpecialValueMap;
|
||||
export type ValueMapping = ValueMap | RangeMap | RegexMap | SpecialValueMap;
|
||||
@@ -1,5 +1,6 @@
|
||||
import { MappingType, SpecialValueMatch, ThresholdsConfig, ValueMap, ValueMapping, ValueMappingResult } from '../types';
|
||||
import { getActiveThreshold } from '../field';
|
||||
import { stringToJsRegex } from '../text/string';
|
||||
|
||||
export function getValueMappingResult(valueMappings: ValueMapping[], value: any): ValueMappingResult | null {
|
||||
for (const vm of valueMappings) {
|
||||
@@ -38,6 +39,22 @@ export function getValueMappingResult(valueMappings: ValueMapping[], value: any)
|
||||
|
||||
return vm.options.result;
|
||||
|
||||
case MappingType.RegexToText:
|
||||
if (value == null) {
|
||||
console.log('null value');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (typeof value !== 'string') {
|
||||
console.log('non-string value', typeof value);
|
||||
continue;
|
||||
}
|
||||
|
||||
const regex = stringToJsRegex(vm.options.pattern);
|
||||
const thisResult = Object.create(vm.options.result);
|
||||
thisResult.text = value.replace(regex, vm.options.result.text || '');
|
||||
return thisResult;
|
||||
|
||||
case MappingType.SpecialValue:
|
||||
switch (vm.options.match) {
|
||||
case SpecialValueMatch.Null: {
|
||||
|
||||
Reference in new issue
Block a user