From c764bca58fb6f8b02092288c26effae9731777e2 Mon Sep 17 00:00:00 2001 From: Villena Guillaume Date: Fri, 12 Mar 2021 08:34:06 +0100 Subject: [PATCH] ValueMappings: Fixes value 0 not being mapped (#31924) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- packages/grafana-data/src/utils/valueMappings.test.ts | 3 +++ packages/grafana-data/src/utils/valueMappings.ts | 9 +++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/grafana-data/src/utils/valueMappings.test.ts b/packages/grafana-data/src/utils/valueMappings.test.ts index e6bf474f76c..e9d04959725 100644 --- a/packages/grafana-data/src/utils/valueMappings.test.ts +++ b/packages/grafana-data/src/utils/valueMappings.test.ts @@ -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} diff --git a/packages/grafana-data/src/utils/valueMappings.ts b/packages/grafana-data/src/utils/valueMappings.ts index e7b2721784f..3839683c615 100644 --- a/packages/grafana-data/src/utils/valueMappings.ts +++ b/packages/grafana-data/src/utils/valueMappings.ts @@ -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); }