DisplayFormat: use toLocaleString for infinity

This commit is contained in:
Ryan McKinley
2019-09-25 09:56:25 -07:00
committed by GitHub
parent 0b85e34b20
commit f06fd5bce2
2 changed files with 20 additions and 10 deletions
@@ -1,11 +1,21 @@
import { toFixed, getValueFormat } from './valueFormats';
import { toFixed, getValueFormat, scaledUnits } from './valueFormats';
describe('valueFormats', () => {
describe('toFixed with edge cases', () => {
it('should handle non number input gracefully', () => {
describe('format edge cases', () => {
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(Number.NEGATIVE_INFINITY)).toBe('-Inf');
expect(toFixed(Number.POSITIVE_INFINITY)).toBe('Inf');
expect(toFixed(Number.NEGATIVE_INFINITY)).toBe(negInf);
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 {
if (value === null) {
return '';
}
if (value === Number.NEGATIVE_INFINITY) {
return '-Inf';
}
if (value === Number.POSITIVE_INFINITY) {
return 'Inf';
if (value === Number.NEGATIVE_INFINITY || value === Number.POSITIVE_INFINITY) {
return value.toLocaleString();
}
const factor = decimals ? Math.pow(10, Math.max(0, decimals)) : 1;
@@ -94,6 +91,9 @@ export function scaledUnits(factor: number, extArray: string[]) {
if (size === null) {
return '';
}
if (size === Number.NEGATIVE_INFINITY || size === Number.POSITIVE_INFINITY || isNaN(size)) {
return size.toLocaleString();
}
let steps = 0;
const limit = extArray.length;