DisplayProcessor: Interpret empty strings as NaN instead of 0 to support empty value map texts in Singlestat (#20952)

This commit is contained in:
Hendrik van Huyssteen 2019-12-09 11:07:31 +02:00 committed by Torkel Ödegaard
parent a7f4e4c56a
commit caa3c6c9a5
2 changed files with 17 additions and 2 deletions

View File

@ -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', () => {

View File

@ -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') {