Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for more precise percentage changes #97

Merged
merged 2 commits into from
Apr 22, 2024
Merged
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
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
Loading