Units: remove unreachable code (#20684)

* Refactor: Remove the code which can't reach

* Test: Add more unit tests for toFixed and scaledUnits methods

- To make them easier to understand
This commit is contained in:
Sparkle 2019-11-28 00:03:22 +08:00 committed by Ryan McKinley
parent a538e67d4d
commit 49dfb9a5d3
2 changed files with 42 additions and 4 deletions

View File

@ -1,6 +1,47 @@
import { toFixed, getValueFormat, scaledUnits } from './valueFormats';
describe('valueFormats', () => {
describe('normal cases', () => {
it('toFixed should handle number correctly if decimal is null', () => {
expect(toFixed(100)).toBe('100');
expect(toFixed(100.4)).toBe('100');
expect(toFixed(100.5)).toBe('101');
});
it('toFixed should handle number correctly if decimal is not null', () => {
expect(toFixed(100, 1)).toBe('100.0');
expect(toFixed(100.37, 1)).toBe('100.4');
expect(toFixed(100.63, 1)).toBe('100.6');
expect(toFixed(100.4, 2)).toBe('100.40');
expect(toFixed(100.5, 2)).toBe('100.50');
});
it('scaledUnit should handle number correctly if scaledDecimals is not null', () => {
const units = ['', 'K', 'M', 'B', 'T'];
const scaler = scaledUnits(1000, units);
expect(scaler(98765, 0, 0)).toBe('98.765K');
expect(scaler(98765, 0, -1)).toBe('98.77K');
expect(scaler(9876543, 0, 0)).toBe('9.876543M');
expect(scaler(9876543, 0, -1)).toBe('9.87654M');
});
it('scaledUnit should handle number correctly if scaledDecimals is null', () => {
const units = ['', 'K', 'M', 'B', 'T'];
const scaler = scaledUnits(1000, units);
expect(scaler(98765, 1, null)).toBe('98.8K');
expect(scaler(98765, 2, null)).toBe('98.77K');
expect(scaler(9876543, 2, null)).toBe('9.88M');
expect(scaler(9876543, 3, null)).toBe('9.877M');
});
});
describe('format edge cases', () => {
const negInf = Number.NEGATIVE_INFINITY.toLocaleString();
const posInf = Number.POSITIVE_INFINITY.toLocaleString();

View File

@ -66,11 +66,8 @@ export function toFixedScaled(
) {
if (scaledDecimals === null || scaledDecimals === undefined) {
return toFixed(value, decimals) + ext;
} else {
return toFixed(value, scaledDecimals + additionalDecimals) + ext;
}
return toFixed(value, decimals) + ext;
return toFixed(value, scaledDecimals + additionalDecimals) + ext;
}
export function toFixedUnit(unit: string): ValueFormatter {