Commit caa3c6c9 by Hendrik van Huyssteen Committed by Torkel Ödegaard

DisplayProcessor: Interpret empty strings as NaN instead of 0 to support empty…

DisplayProcessor: Interpret empty strings as NaN instead of 0 to support empty value map texts in Singlestat (#20952)
parent a7f4e4c5
...@@ -46,6 +46,10 @@ describe('Process simple display values', () => { ...@@ -46,6 +46,10 @@ describe('Process simple display values', () => {
assertSame('3', processors, { text: '3', numeric: 3 }); assertSame('3', processors, { text: '3', numeric: 3 });
}); });
it('Empty string is NaN', () => {
assertSame('', processors, { text: '', numeric: NaN });
});
it('Simple String', () => { it('Simple String', () => {
assertSame('hello', processors, { text: 'hello', numeric: NaN }); assertSame('hello', processors, { text: 'hello', numeric: NaN });
}); });
...@@ -167,8 +171,19 @@ describe('Format value', () => { ...@@ -167,8 +171,19 @@ describe('Format value', () => {
expect(instance(value).text).toEqual('1-20'); 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', () => { it('with value 1000 and unit short', () => {
......
...@@ -112,7 +112,7 @@ function toNumber(value: any): number { ...@@ -112,7 +112,7 @@ function toNumber(value: any): number {
if (typeof value === 'number') { if (typeof value === 'number') {
return value; 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 return NaN; // lodash calls them 0
} }
if (typeof value === 'boolean') { if (typeof value === 'boolean') {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment