Skip to content

Commit

Permalink
ValueFormats: check for inf (grafana#19376)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantxu authored and torkelo committed Sep 25, 2019
1 parent 94d7af8 commit 32b73bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
@@ -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);
Expand Down
6 changes: 6 additions & 0 deletions packages/grafana-ui/src/utils/valueFormats/valueFormats.ts
Expand Up @@ -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);
Expand Down

0 comments on commit 32b73bb

Please sign in to comment.