Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion apps/web/src/common/utils/format.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { formatRepoName, shortenAxisLabel, roundedNum } from './format';
import {
formatNegativeNumber,
formatRepoName,
percentageValueFormat,
shortenAxisLabel,
roundedNum,
} from './format';

describe('format', () => {
it('formatRepoName', function () {
Expand Down Expand Up @@ -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);
});
});
5 changes: 5 additions & 0 deletions apps/web/src/common/utils/format.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
})
Expand Down