Commit f06fd5bc by Ryan McKinley Committed by GitHub

DisplayFormat: use toLocaleString for infinity

parent 0b85e34b
import { toFixed, getValueFormat } from './valueFormats'; import { toFixed, getValueFormat, scaledUnits } from './valueFormats';
describe('valueFormats', () => { describe('valueFormats', () => {
describe('toFixed with edge cases', () => { describe('format edge cases', () => {
it('should handle non number input gracefully', () => { const negInf = Number.NEGATIVE_INFINITY.toLocaleString();
const posInf = Number.POSITIVE_INFINITY.toLocaleString();
it('toFixed should handle non number input gracefully', () => {
expect(toFixed(NaN)).toBe('NaN'); expect(toFixed(NaN)).toBe('NaN');
expect(toFixed(Number.NEGATIVE_INFINITY)).toBe('-Inf'); expect(toFixed(Number.NEGATIVE_INFINITY)).toBe(negInf);
expect(toFixed(Number.POSITIVE_INFINITY)).toBe('Inf'); expect(toFixed(Number.POSITIVE_INFINITY)).toBe(posInf);
});
it('scaledUnits should handle non number input gracefully', () => {
const disp = scaledUnits(5, ['a', 'b', 'c']);
expect(disp(NaN)).toBe('NaN');
expect(disp(Number.NEGATIVE_INFINITY)).toBe(negInf);
expect(disp(Number.POSITIVE_INFINITY)).toBe(posInf);
}); });
}); });
......
...@@ -33,11 +33,8 @@ export function toFixed(value: number, decimals?: DecimalCount): string { ...@@ -33,11 +33,8 @@ export function toFixed(value: number, decimals?: DecimalCount): string {
if (value === null) { if (value === null) {
return ''; return '';
} }
if (value === Number.NEGATIVE_INFINITY) { if (value === Number.NEGATIVE_INFINITY || value === Number.POSITIVE_INFINITY) {
return '-Inf'; return value.toLocaleString();
}
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;
...@@ -94,6 +91,9 @@ export function scaledUnits(factor: number, extArray: string[]) { ...@@ -94,6 +91,9 @@ export function scaledUnits(factor: number, extArray: string[]) {
if (size === null) { if (size === null) {
return ''; return '';
} }
if (size === Number.NEGATIVE_INFINITY || size === Number.POSITIVE_INFINITY || isNaN(size)) {
return size.toLocaleString();
}
let steps = 0; let steps = 0;
const limit = extArray.length; const limit = extArray.length;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment