mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
ValueFormats: check for inf (#19376)
This commit is contained in:
parent
94d7af888c
commit
32b73bb496
@ -1,6 +1,14 @@
|
|||||||
import { toFixed, getValueFormat } from './valueFormats';
|
import { toFixed, getValueFormat } from './valueFormats';
|
||||||
|
|
||||||
describe('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', () => {
|
describe('toFixed and negative decimals', () => {
|
||||||
it('should treat as zero decimals', () => {
|
it('should treat as zero decimals', () => {
|
||||||
const str = toFixed(186.123, -2);
|
const str = toFixed(186.123, -2);
|
||||||
|
@ -33,6 +33,12 @@ export function toFixed(value: number, decimals?: DecimalCount): string {
|
|||||||
if (value === null) {
|
if (value === null) {
|
||||||
return '';
|
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 factor = decimals ? Math.pow(10, Math.max(0, decimals)) : 1;
|
||||||
const formatted = String(Math.round(value * factor) / factor);
|
const formatted = String(Math.round(value * factor) / factor);
|
||||||
|
Loading…
Reference in New Issue
Block a user