From 32b73bb496d5912b015bd13c4b3c1be957a43381 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Wed, 25 Sep 2019 00:04:51 -0700 Subject: [PATCH] ValueFormats: check for inf (#19376) --- .../src/utils/valueFormats/valueFormats.test.ts | 8 ++++++++ .../grafana-ui/src/utils/valueFormats/valueFormats.ts | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/packages/grafana-ui/src/utils/valueFormats/valueFormats.test.ts b/packages/grafana-ui/src/utils/valueFormats/valueFormats.test.ts index 4b28c076967..2bb3c6392b8 100644 --- a/packages/grafana-ui/src/utils/valueFormats/valueFormats.test.ts +++ b/packages/grafana-ui/src/utils/valueFormats/valueFormats.test.ts @@ -1,6 +1,14 @@ import { toFixed, getValueFormat } from './valueFormats'; describe('valueFormats', () => { + describe('toFixed with edge cases', () => { + it('should handle non number input gracefully', () => { + expect(toFixed(NaN)).toBe('NaN'); + expect(toFixed(Number.NEGATIVE_INFINITY)).toBe('-Inf'); + expect(toFixed(Number.POSITIVE_INFINITY)).toBe('Inf'); + }); + }); + describe('toFixed and negative decimals', () => { it('should treat as zero decimals', () => { const str = toFixed(186.123, -2); diff --git a/packages/grafana-ui/src/utils/valueFormats/valueFormats.ts b/packages/grafana-ui/src/utils/valueFormats/valueFormats.ts index 353014c6f5c..3aca7ed60a1 100644 --- a/packages/grafana-ui/src/utils/valueFormats/valueFormats.ts +++ b/packages/grafana-ui/src/utils/valueFormats/valueFormats.ts @@ -33,6 +33,12 @@ export function toFixed(value: number, decimals?: DecimalCount): string { if (value === null) { return ''; } + if (value === Number.NEGATIVE_INFINITY) { + return '-Inf'; + } + if (value === Number.POSITIVE_INFINITY) { + return 'Inf'; + } const factor = decimals ? Math.pow(10, Math.max(0, decimals)) : 1; const formatted = String(Math.round(value * factor) / factor);