Skip to content

Commit f2cd878

Browse files
feat: Show more precise percentage changes (#97)
* feat: Add support for more precise percentage changes * refactor: Hold off on making it a new option --------- Co-authored-by: Ryan Christian <rchristian@ryanchristian.dev>
1 parent 75744b3 commit f2cd878

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ inputs:
4141
description: 'A custom working directory to execute the action in relative to repo root (defaults to .)'
4242
runs:
4343
using: 'node20'
44-
main: 'index.js'
44+
main: 'index.js'

index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function getDeltaText(delta, originalSize) {
5252
} else if (originalSize === -delta) {
5353
deltaText += ` (removed)`;
5454
} else {
55-
const percentage = Math.round((delta / originalSize) * 100);
55+
const percentage = Number(((delta / originalSize) * 100).toFixed(2));
5656
deltaText += ` (${percentage > 0 ? '+' : ''}${percentage}%)`;
5757
}
5858
return deltaText;
@@ -151,8 +151,8 @@ export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged,
151151
if (isUnchanged && omitUnchanged) continue;
152152

153153
const columns = [
154-
`\`${filename}\``,
155-
prettyBytes(size),
154+
`\`${filename}\``,
155+
prettyBytes(size),
156156
getDeltaText(delta, originalSize),
157157
iconForDifference(delta, originalSize)
158158
];

tests/__snapshots__/utils.spec.js.snap

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`diffTable 1`] = `
4-
"**Size Change:** +9 B (0%)
4+
"**Size Change:** +9 B (+0.06%)
55
66
**Total Size:** 14.8 kB
77
88
| Filename | Size | Change | |
99
| :--- | :---: | :---: | :---: |
1010
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
11-
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
12-
| \`four.js\` | 4.5 kB | +9 B (0%) | |
11+
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
12+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
1313
1414
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
1515
@@ -26,8 +26,8 @@ exports[`diffTable 2`] = `
2626
"| Filename | Size | Change | |
2727
| :--- | :---: | :---: | :---: |
2828
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
29-
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
30-
| \`four.js\` | 4.5 kB | +9 B (0%) | |
29+
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
30+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
3131
3232
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
3333
@@ -41,46 +41,46 @@ exports[`diffTable 2`] = `
4141
`;
4242

4343
exports[`diffTable 3`] = `
44-
"**Size Change:** +9 B (0%)
44+
"**Size Change:** +9 B (+0.06%)
4545
4646
**Total Size:** 14.8 kB
4747
4848
| Filename | Size | Change | |
4949
| :--- | :---: | :---: | :---: |
5050
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
51-
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
51+
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
5252
| \`three.js\` | 300 B | 0 B | |
53-
| \`four.js\` | 4.5 kB | +9 B (0%) | |"
53+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |"
5454
`;
5555

5656
exports[`diffTable 4`] = `
57-
"**Size Change:** +9 B (0%)
57+
"**Size Change:** +9 B (+0.06%)
5858
5959
**Total Size:** 14.8 kB
6060
6161
| Filename | Size | Change | |
6262
| :--- | :---: | :---: | :---: |
6363
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
64-
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
65-
| \`four.js\` | 4.5 kB | +9 B (0%) | |"
64+
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
65+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |"
6666
`;
6767

6868
exports[`diffTable 5`] = `
69-
"**Size Change:** +9 B (0%)
69+
"**Size Change:** +9 B (+0.06%)
7070
7171
**Total Size:** 14.8 kB
7272
7373
| Filename | Size | Change | |
7474
| :--- | :---: | :---: | :---: |
7575
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
76-
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
76+
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
7777
7878
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
7979
8080
| Filename | Size | Change |
8181
| :--- | :---: | :---: |
8282
| \`three.js\` | 300 B | 0 B |
83-
| \`four.js\` | 4.5 kB | +9 B (0%) |
83+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) |
8484
8585
</details>
8686

tests/utils.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ test('getDeltaText', () => {
1515
expect(getDeltaText(-5000, 20000)).toBe('-5 kB (-25%)');
1616
expect(getDeltaText(210, 0)).toBe('+210 B (new file)');
1717
expect(getDeltaText(0, 0)).toBe('0 B');
18+
expect(getDeltaText(4875, 20000)).toBe('+4.88 kB (+24.38%)');
19+
expect(getDeltaText(-4875, 20000)).toBe('-4.88 kB (-24.38%)');
1820
});
1921

2022
test('iconForDifference', () => {

0 commit comments

Comments
 (0)