Skip to content

Commit

Permalink
refactor: Hold off on making it a new option
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Apr 22, 2024
1 parent 42d3f5d commit 1cacfe9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 42 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,3 @@ By default, files are compared after gzip compression, but it's possible to use
```yaml
compression: "none"
```

### Showing percentage change with decimal places

The number of decimal places in the percentage change displayed in the table can be set to a level of precision. By default the percentage is rounded to an integer.

```yaml
percent-decimal-places: 2
```
5 changes: 1 addition & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ inputs:
default: '{**/*.map,**/node_modules/**}'
cwd:
description: 'A custom working directory to execute the action in relative to repo root (defaults to .)'
percent-decimal-places:
description: 'The number of decimal places to use with percentages'
default: 0
runs:
using: 'node20'
main: 'index.js'
main: 'index.js'
2 changes: 1 addition & 1 deletion index.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ async function run(octokit, context, token) {
collapseUnchanged: toBool(getInput('collapse-unchanged')),
omitUnchanged: toBool(getInput('omit-unchanged')),
showTotal: toBool(getInput('show-total')),
minimumChangeThreshold: parseInt(getInput('minimum-change-threshold'), 10),
decimalPlaces: parseInt(getInput('percent-decimal-places'), 10)
minimumChangeThreshold: parseInt(getInput('minimum-change-threshold'), 10)
});

let outputRawMarkdown = false;
Expand Down
16 changes: 7 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ export function stripHash(regex) {
/**
* @param {number} delta
* @param {number} originalSize
* @param {number} decimalPlaces
*/
export function getDeltaText(delta, originalSize, decimalPlaces = 0) {
export function getDeltaText(delta, originalSize) {
let deltaText = (delta > 0 ? '+' : '') + prettyBytes(delta);
if (Math.abs(delta) === 0) {
// only print size
Expand All @@ -53,7 +52,7 @@ export function getDeltaText(delta, originalSize, decimalPlaces = 0) {
} else if (originalSize === -delta) {
deltaText += ` (removed)`;
} else {
const percentage = ((delta / originalSize) * 100).toFixed(decimalPlaces);
const percentage = Number(((delta / originalSize) * 100).toFixed(2));
deltaText += ` (${percentage > 0 ? '+' : ''}${percentage}%)`;
}
return deltaText;
Expand Down Expand Up @@ -134,9 +133,8 @@ function markdownTable(rows) {
* @param {boolean} [options.collapseUnchanged]
* @param {boolean} [options.omitUnchanged]
* @param {number} [options.minimumChangeThreshold]
* @param {number} [options.decimalPlaces]
*/
export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged, minimumChangeThreshold, decimalPlaces }) {
export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged, minimumChangeThreshold }) {
let changedRows = [];
let unChangedRows = [];

Expand All @@ -153,9 +151,9 @@ export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged,
if (isUnchanged && omitUnchanged) continue;

const columns = [
`\`${filename}\``,
prettyBytes(size),
getDeltaText(delta, originalSize, decimalPlaces),
`\`${filename}\``,
prettyBytes(size),
getDeltaText(delta, originalSize),
iconForDifference(delta, originalSize)
];
if (isUnchanged && collapseUnchanged) {
Expand All @@ -174,7 +172,7 @@ export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged,

if (showTotal) {
const totalOriginalSize = totalSize - totalDelta;
let totalDeltaText = getDeltaText(totalDelta, totalOriginalSize, decimalPlaces);
let totalDeltaText = getDeltaText(totalDelta, totalOriginalSize);
let totalIcon = iconForDifference(totalDelta, totalOriginalSize);
out = `**Total Size:** ${prettyBytes(totalSize)}\n\n${out}`;
out = `**Size Change:** ${totalDeltaText} ${totalIcon}\n\n${out}`;
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
6 changes: 2 additions & 4 deletions tests/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +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%)');
expect(getDeltaText(-4875, 20000)).toBe('-4.88 kB (-24%)');
expect(getDeltaText(4875, 20000, 2)).toBe('+4.88 kB (+24.38%)');
expect(getDeltaText(-4875, 20000,2)).toBe('-4.88 kB (-24.38%)');
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 1cacfe9

Please sign in to comment.