Skip to content

Commit

Permalink
fix: format count in markdown report (#545)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski authored Jan 30, 2025
1 parent 8812725 commit 89274b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/compare/src/utils/__tests__/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { formatCountChange, formatCountDiff } from '../format';

test(`formatCountChange`, () => {
expect(formatCountChange(1, 2)).toMatchInlineSnapshot(`"2 → 1 (-1, -50.0%) 🟢"`);
expect(formatCountChange(1, 1)).toMatchInlineSnapshot(`"1 → 1 "`);
expect(formatCountChange(2, 1)).toMatchInlineSnapshot(`"1 → 2 (+1, +100.0%) 🔴"`);
expect(formatCountChange(1.01, 2.0)).toMatchInlineSnapshot(`"2 → 1.01 (-0.99, -49.5%) 🟢"`);
expect(formatCountChange(1.45, 2.05)).toMatchInlineSnapshot(`"2.05 → 1.45 (-0.60, -29.3%) 🟢"`);
});

test('formatCountDiff', () => {
expect(formatCountDiff(2, 1)).toMatchInlineSnapshot('"+1"');
expect(formatCountDiff(0, 1)).toMatchInlineSnapshot('"-1"');
expect(formatCountDiff(2, 2)).toMatchInlineSnapshot('"±0"');

expect(formatCountDiff(1.01, 2.23)).toMatchInlineSnapshot(`"-1.22"`);
expect(formatCountDiff(0.01, 5.54)).toMatchInlineSnapshot(`"-5.53"`);
expect(formatCountDiff(1.01, 1.01)).toMatchInlineSnapshot('"±0"');
});
9 changes: 7 additions & 2 deletions packages/compare/src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ export function formatCount(value?: number) {

export function formatCountDiff(current: number, baseline: number): string {
const diff = current - baseline;
if (diff > 0) return `+${diff}`;
if (diff < 0) return `${diff}`;
if (diff > 0) {
return `+${formatCount(diff)}`;
}
if (diff < 0) {
return `${formatCount(diff)}`;
}

return '±0';
}

Expand Down

0 comments on commit 89274b8

Please sign in to comment.