diff --git a/packages/grafana-data/src/field/displayProcessor.test.ts b/packages/grafana-data/src/field/displayProcessor.test.ts index b9e698e7e0f..02aadcab7d4 100644 --- a/packages/grafana-data/src/field/displayProcessor.test.ts +++ b/packages/grafana-data/src/field/displayProcessor.test.ts @@ -46,6 +46,10 @@ describe('Process simple display values', () => { assertSame('3', processors, { text: '3', numeric: 3 }); }); + it('Empty string is NaN', () => { + assertSame('', processors, { text: '', numeric: NaN }); + }); + it('Simple String', () => { assertSame('hello', processors, { text: 'hello', numeric: NaN }); }); @@ -167,8 +171,19 @@ describe('Format value', () => { expect(instance(value).text).toEqual('1-20'); }); + it('should return mapped value and leave numeric value in tact if value mapping maps to empty string', () => { + const valueMappings: ValueMapping[] = [ + { id: 1, operator: '', text: '', type: MappingType.ValueToText, value: '1' }, + ]; + const value = '1'; + const instance = getDisplayProcessor({ config: { decimals: 1, mappings: valueMappings } }); + + expect(instance(value).text).toEqual(''); + expect(instance(value).numeric).toEqual(1); + }); + // - // Below is current behavior but I it's clearly not working great + // Below is current behavior but it's clearly not working great // it('with value 1000 and unit short', () => { diff --git a/packages/grafana-data/src/field/displayProcessor.ts b/packages/grafana-data/src/field/displayProcessor.ts index 42eb9368d1c..87f71d8e757 100644 --- a/packages/grafana-data/src/field/displayProcessor.ts +++ b/packages/grafana-data/src/field/displayProcessor.ts @@ -112,7 +112,7 @@ function toNumber(value: any): number { if (typeof value === 'number') { return value; } - if (value === null || value === undefined || Array.isArray(value)) { + if (value === '' || value === null || value === undefined || Array.isArray(value)) { return NaN; // lodash calls them 0 } if (typeof value === 'boolean') {