Skip to content

Commit

Permalink
feat: Show more precise percentage changes (#97)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
mister-ben and rschristian authored Apr 22, 2024
1 parent 75744b3 commit f2cd878
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ inputs:
description: 'A custom working directory to execute the action in relative to repo root (defaults to .)'
runs:
using: 'node20'
main: 'index.js'
main: 'index.js'
2 changes: 1 addition & 1 deletion index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function getDeltaText(delta, originalSize) {
} else if (originalSize === -delta) {
deltaText += ` (removed)`;
} else {
const percentage = Math.round((delta / originalSize) * 100);
const percentage = Number(((delta / originalSize) * 100).toFixed(2));
deltaText += ` (${percentage > 0 ? '+' : ''}${percentage}%)`;
}
return deltaText;
Expand Down Expand Up @@ -151,8 +151,8 @@ export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged,
if (isUnchanged && omitUnchanged) continue;

const columns = [
`\`${filename}\``,
prettyBytes(size),
`\`${filename}\``,
prettyBytes(size),
getDeltaText(delta, originalSize),
iconForDifference(delta, originalSize)
];
Expand Down
28 changes: 14 additions & 14 deletions tests/__snapshots__/utils.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`diffTable 1`] = `
"**Size Change:** +9 B (0%)
"**Size Change:** +9 B (+0.06%)
**Total Size:** 14.8 kB
| Filename | Size | Change | |
| :--- | :---: | :---: | :---: |
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
| \`four.js\` | 4.5 kB | +9 B (0%) | |
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
Expand All @@ -26,8 +26,8 @@ exports[`diffTable 2`] = `
"| Filename | Size | Change | |
| :--- | :---: | :---: | :---: |
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
| \`four.js\` | 4.5 kB | +9 B (0%) | |
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
Expand All @@ -41,46 +41,46 @@ exports[`diffTable 2`] = `
`;

exports[`diffTable 3`] = `
"**Size Change:** +9 B (0%)
"**Size Change:** +9 B (+0.06%)
**Total Size:** 14.8 kB
| Filename | Size | Change | |
| :--- | :---: | :---: | :---: |
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
| \`three.js\` | 300 B | 0 B | |
| \`four.js\` | 4.5 kB | +9 B (0%) | |"
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |"
`;

exports[`diffTable 4`] = `
"**Size Change:** +9 B (0%)
"**Size Change:** +9 B (+0.06%)
**Total Size:** 14.8 kB
| Filename | Size | Change | |
| :--- | :---: | :---: | :---: |
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
| \`four.js\` | 4.5 kB | +9 B (0%) | |"
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |"
`;

exports[`diffTable 5`] = `
"**Size Change:** +9 B (0%)
"**Size Change:** +9 B (+0.06%)
**Total Size:** 14.8 kB
| Filename | Size | Change | |
| :--- | :---: | :---: | :---: |
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
| Filename | Size | Change |
| :--- | :---: | :---: |
| \`three.js\` | 300 B | 0 B |
| \`four.js\` | 4.5 kB | +9 B (0%) |
| \`four.js\` | 4.5 kB | +9 B (+0.2%) |
</details>
Expand Down
2 changes: 2 additions & 0 deletions tests/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ test('getDeltaText', () => {
expect(getDeltaText(-5000, 20000)).toBe('-5 kB (-25%)');
expect(getDeltaText(210, 0)).toBe('+210 B (new file)');
expect(getDeltaText(0, 0)).toBe('0 B');
expect(getDeltaText(4875, 20000)).toBe('+4.88 kB (+24.38%)');
expect(getDeltaText(-4875, 20000)).toBe('-4.88 kB (-24.38%)');
});

test('iconForDifference', () => {
Expand Down

0 comments on commit f2cd878

Please sign in to comment.