diff --git a/apps/web/src/common/utils/format.test.tsx b/apps/web/src/common/utils/format.test.tsx index 7b6f1ea55..067573b60 100644 --- a/apps/web/src/common/utils/format.test.tsx +++ b/apps/web/src/common/utils/format.test.tsx @@ -1,4 +1,10 @@ -import { formatRepoName, shortenAxisLabel, roundedNum } from './format'; +import { + formatNegativeNumber, + formatRepoName, + percentageValueFormat, + shortenAxisLabel, + roundedNum, +} from './format'; describe('format', () => { it('formatRepoName', function () { @@ -32,4 +38,19 @@ describe('format', () => { expect(roundedNum(1.2345, 3)).toEqual(1.235); expect(roundedNum(100000)).toEqual(100000); }); + + it('leaves empty values alone when negating', function () { + expect(formatNegativeNumber(true, ['', null as any, '-', '5'])).toEqual([ + '', + null, + '-', + -5, + ]); + }); + + it('leaves empty values alone when formatting percentages', function () { + expect(percentageValueFormat('')).toBe(''); + expect(percentageValueFormat(null as any)).toBe(null); + expect(percentageValueFormat('0.5')).toBe(50); + }); }); diff --git a/apps/web/src/common/utils/format.tsx b/apps/web/src/common/utils/format.tsx index b684d448b..ea27066b1 100644 --- a/apps/web/src/common/utils/format.tsx +++ b/apps/web/src/common/utils/format.tsx @@ -82,6 +82,10 @@ export const formatRepoName = ({ }; export const percentageValueFormat = (value: string | number) => { + // Number('') and Number(null) are 0, so an empty cell used to be printed as 0%. + if (value === '' || value === null || value === undefined) { + return value; + } if (!isNaN(Number(value))) { return toFixed(+value * 100, 3); } @@ -101,6 +105,7 @@ export const formatNegativeNumber = ( ) => { return condition ? data.map((v) => { + if (v === '' || v === null || v === undefined) return v; if (!isNaN(Number(v))) return -v; return v; })