ValueMappings: Fixes value 0 not being mapped (#31924)

* ValueMapping: Updated tests (#31877)

Checks if value 0 or '0' is considered as a numeric value

* ValueMapping: Changed implementation of isNumeric (#31877)

isNumeric now works with the 0 value

* fixed prettier issue

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
This commit is contained in:
Villena Guillaume 2021-03-12 08:34:06 +01:00 committed by GitHub
parent 82e9d59121
commit c764bca58f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 6 deletions

View File

@ -123,9 +123,12 @@ describe('isNumeric', () => {
it.each`
value | expected
${123} | ${true}
${0} | ${true}
${'123'} | ${true}
${'0'} | ${true}
${' 123'} | ${true}
${' 123 '} | ${true}
${' 0 '} | ${true}
${-123.4} | ${true}
${'-123.4'} | ${true}
${0.41} | ${true}

View File

@ -97,11 +97,8 @@ const isNullValueMap = (mapping: ValueMap): boolean => {
return mapping.value.toLowerCase() === 'null';
};
// Ref https://stackoverflow.com/a/42356340
export function isNumeric(num: any) {
if (num === true) {
return false;
}
// Ref https://stackoverflow.com/a/58550111
return Boolean(Number(num));
export function isNumeric(num: any) {
return (typeof num === 'number' || (typeof num === 'string' && num.trim() !== '')) && !isNaN(num as number);
}