Skip to content

Commit b30ccd8

Browse files
committed
feat: Add 'sort-by' option
1 parent f3b74b7 commit b30ccd8

File tree

6 files changed

+168
-30
lines changed

6 files changed

+168
-30
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ jobs:
115115
}
116116
```
117117

118+
### Sorting the list of files
119+
120+
By default, the results table is sorted by filename in ascending order. You can customize this behavior using the `order-by` option:
121+
122+
```diff
123+
name: Compressed Size
124+
on: [pull_request]
125+
jobs:
126+
build:
127+
runs-on: ubuntu-latest
128+
steps:
129+
- uses: actions/checkout@v2
130+
- uses: preactjs/compressed-size-action@v2
131+
with:
132+
+ order-by: "Size:desc"
133+
```
134+
135+
The format is "column:direction", where column is one of "Filename", "Size", or "Change" and direction is "asc" or "desc". For example, "Size:desc" sorts the table by file size in descending order.
136+
118137
### Customizing the list of files
119138

120139
`compressed-size-action` defaults to tracking the size of all JavaScript files within `dist/` directories - anywhere in your repository, not just at the root. You can change the list of files to be tracked and reported using the `pattern` and `exclude` options, both of which are [minimatch patterns](https://github.com/motemen/minimatch-cheat-sheet/blob/master/README.md):

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ inputs:
4242
description: 'A custom working directory to execute the action in relative to repo root (defaults to .)'
4343
comment-key:
4444
description: 'Optional key to include in the bot comment to allow for multiple bundle calculations to be posted in separate comments.'
45+
sort-by:
46+
description: 'The column and direction to sort the results by. The format is "column:direction", where column is one of "Filename", "Size", or "Change" and direction is "asc" or "desc". For example, "Size:desc" sorts the table by file size in descending order.'
47+
default: 'Filename:asc'
4548

4649
runs:
4750
using: 'node20'

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getInput, setFailed, startGroup, endGroup, debug } from '@actions/core'
22
import { context, getOctokit } from '@actions/github';
33
import { exec } from '@actions/exec';
44
import SizePlugin from 'size-plugin-core';
5-
import { getPackageManagerAndInstallScript, diffTable, toBool, stripHash } from './utils.js';
5+
import { getPackageManagerAndInstallScript, diffTable, toBool, stripHash, getSortOrder } from './utils.js';
66

77
/**
88
* @typedef {ReturnType<typeof import("@actions/github").getOctokit>} Octokit
@@ -136,7 +136,8 @@ async function run(octokit, context, token) {
136136
collapseUnchanged: toBool(getInput('collapse-unchanged')),
137137
omitUnchanged: toBool(getInput('omit-unchanged')),
138138
showTotal: toBool(getInput('show-total')),
139-
minimumChangeThreshold: parseInt(getInput('minimum-change-threshold'), 10)
139+
minimumChangeThreshold: parseInt(getInput('minimum-change-threshold'), 10),
140+
sortBy: getSortOrder(getInput('sort-by')),
140141
});
141142

142143
let outputRawMarkdown = false;

src/utils.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ function markdownTable(rows) {
157157
* @property {number} delta
158158
*/
159159

160+
/**
161+
* @typedef {'Filename' | 'Size' | 'Change'} DiffTableColumn
162+
* @typedef {'asc' | 'desc'} SortOrder
163+
* @typedef {`${DiffTableColumn}:${SortOrder}`} SortBy
164+
*/
165+
160166
/**
161167
* Create a Markdown table showing diff data
162168
* @param {Diff[]} files
@@ -165,10 +171,27 @@ function markdownTable(rows) {
165171
* @param {boolean} [options.collapseUnchanged]
166172
* @param {boolean} [options.omitUnchanged]
167173
* @param {number} [options.minimumChangeThreshold]
174+
* @param {SortBy} [options.sortBy]
175+
* @returns {string}
168176
*/
169-
export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged, minimumChangeThreshold }) {
170-
let changedRows = [];
171-
let unChangedRows = [];
177+
export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged, minimumChangeThreshold, sortBy }) {
178+
const changedRows = [],
179+
unChangedRows = [];
180+
181+
const [sortByColumn, sortByDirection] = /** @type {[DiffTableColumn, SortOrder]} */ (sortBy.split(':'));
182+
183+
const columnIndex = {
184+
Filename: 'filename',
185+
Size: 'size',
186+
Change: 'delta'
187+
};
188+
189+
files.sort((a, b) => {
190+
const idx = columnIndex[sortByColumn];
191+
return sortByDirection === 'asc'
192+
? a[idx].toString().localeCompare(b[idx].toString(), undefined, { numeric: true })
193+
: b[idx].toString().localeCompare(a[idx].toString(), undefined, { numeric: true });
194+
});
172195

173196
let totalSize = 0;
174197
let totalDelta = 0;
@@ -182,16 +205,16 @@ export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged,
182205

183206
if (isUnchanged && omitUnchanged) continue;
184207

185-
const columns = [
208+
const row = [
186209
`\`${filename}\``,
187210
prettyBytes(size),
188211
getDeltaText(delta, originalSize),
189212
iconForDifference(delta, originalSize)
190213
];
191214
if (isUnchanged && collapseUnchanged) {
192-
unChangedRows.push(columns);
215+
unChangedRows.push(row);
193216
} else {
194-
changedRows.push(columns);
217+
changedRows.push(row);
195218
}
196219
}
197220

@@ -220,3 +243,19 @@ export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged,
220243
export function toBool(v) {
221244
return /^(1|true|yes)$/.test(v);
222245
}
246+
247+
/**
248+
* @param {string} sortBy
249+
* @returns {SortBy}
250+
*/
251+
export function getSortOrder(sortBy) {
252+
const validColumns = ['Filename', 'Size', 'Change'];
253+
const validDirections = ['asc', 'desc'];
254+
255+
const [column, direction] = sortBy.split(':');
256+
if (validColumns.includes(column) && validDirections.includes(direction)) {
257+
return /** @type {SortBy} */ (sortBy);
258+
}
259+
console.warn(`Invalid 'order-by' value '${sortBy}', defaulting to 'Filename:asc'`);
260+
return 'Filename:asc';
261+
}

tests/__snapshots__/utils.spec.js.snap

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

33
exports[`diffTable 1`] = `
4-
"**Size Change:** +9 B (+0.06%)
4+
"**Size Change:** +9 B (+0.04%)
55
6-
**Total Size:** 14.8 kB
6+
**Total Size:** 21.3 kB
77
88
| Filename | Size | Change | |
99
| :--- | :---: | :---: | :---: |
10+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
1011
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
1112
| \`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
1616
| Filename | Size |
1717
| :--- | :---: |
18+
| \`five.js\` | 6.5 kB |
1819
| \`three.js\` | 300 B |
1920
2021
</details>
@@ -25,14 +26,15 @@ exports[`diffTable 1`] = `
2526
exports[`diffTable 2`] = `
2627
"| Filename | Size | Change | |
2728
| :--- | :---: | :---: | :---: |
29+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
2830
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
2931
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
30-
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
3132
3233
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
3334
3435
| Filename | Size |
3536
| :--- | :---: |
37+
| \`five.js\` | 6.5 kB |
3638
| \`three.js\` | 300 B |
3739
3840
</details>
@@ -41,34 +43,35 @@ exports[`diffTable 2`] = `
4143
`;
4244

4345
exports[`diffTable 3`] = `
44-
"**Size Change:** +9 B (+0.06%)
46+
"**Size Change:** +9 B (+0.04%)
4547
46-
**Total Size:** 14.8 kB
48+
**Total Size:** 21.3 kB
4749
4850
| Filename | Size | Change | |
4951
| :--- | :---: | :---: | :---: |
52+
| \`five.js\` | 6.5 kB | 0 B | |
53+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
5054
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
51-
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
5255
| \`three.js\` | 300 B | 0 B | |
53-
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |"
56+
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |"
5457
`;
5558

5659
exports[`diffTable 4`] = `
57-
"**Size Change:** +9 B (+0.06%)
60+
"**Size Change:** +9 B (+0.04%)
5861
59-
**Total Size:** 14.8 kB
62+
**Total Size:** 21.3 kB
6063
6164
| Filename | Size | Change | |
6265
| :--- | :---: | :---: | :---: |
66+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
6367
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
64-
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
65-
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |"
68+
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |"
6669
`;
6770

6871
exports[`diffTable 5`] = `
69-
"**Size Change:** +9 B (+0.06%)
72+
"**Size Change:** +9 B (+0.04%)
7073
71-
**Total Size:** 14.8 kB
74+
**Total Size:** 21.3 kB
7275
7376
| Filename | Size | Change | |
7477
| :--- | :---: | :---: | :---: |
@@ -79,8 +82,9 @@ exports[`diffTable 5`] = `
7982
8083
| Filename | Size | Change |
8184
| :--- | :---: | :---: |
82-
| \`three.js\` | 300 B | 0 B |
85+
| \`five.js\` | 6.5 kB | 0 B |
8386
| \`four.js\` | 4.5 kB | +9 B (+0.2%) |
87+
| \`three.js\` | 300 B | 0 B |
8488
8589
</details>
8690
@@ -90,36 +94,98 @@ exports[`diffTable 5`] = `
9094
exports[`diffTable 6`] = `
9195
"**Size Change:** 0 B
9296
93-
**Total Size:** 14.8 kB
97+
**Total Size:** 21.3 kB
9498
9599
96100
97101
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
98102
99103
| Filename | Size |
100104
| :--- | :---: |
105+
| \`five.js\` | 6.5 kB |
106+
| \`four.js\` | 4.5 kB |
101107
| \`one.js\` | 5 kB |
102-
| \`two.js\` | 5 kB |
103108
| \`three.js\` | 300 B |
104-
| \`four.js\` | 4.5 kB |
109+
| \`two.js\` | 5 kB |
105110
106111
</details>
107112
108113
"
109114
`;
110115

111116
exports[`diffTable 7`] = `
112-
"**Size Change:** 0 B
117+
"**Size Change:** +2.5 kB (+100%) 🆘
118+
119+
**Total Size:** 5 kB
113120
114-
**Total Size:** 300 B
121+
| Filename | Size | Change | |
122+
| :--- | :---: | :---: | :---: |
123+
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |"
124+
`;
125+
126+
exports[`diffTable 8`] = `
127+
"**Size Change:** +9 B (+0.04%)
128+
129+
**Total Size:** 21.3 kB
130+
131+
| Filename | Size | Change | |
132+
| :--- | :---: | :---: | :---: |
133+
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
134+
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
135+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
136+
137+
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
115138
139+
| Filename | Size |
140+
| :--- | :---: |
141+
| \`three.js\` | 300 B |
142+
| \`five.js\` | 6.5 kB |
116143
144+
</details>
145+
146+
"
147+
`;
148+
149+
exports[`diffTable 9`] = `
150+
"**Size Change:** +9 B (+0.04%)
151+
152+
**Total Size:** 21.3 kB
153+
154+
| Filename | Size | Change | |
155+
| :--- | :---: | :---: | :---: |
156+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
157+
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
158+
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
159+
160+
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
161+
162+
| Filename | Size |
163+
| :--- | :---: |
164+
| \`three.js\` | 300 B |
165+
| \`five.js\` | 6.5 kB |
166+
167+
</details>
168+
169+
"
170+
`;
171+
172+
exports[`diffTable 10`] = `
173+
"**Size Change:** +9 B (+0.04%)
174+
175+
**Total Size:** 21.3 kB
176+
177+
| Filename | Size | Change | |
178+
| :--- | :---: | :---: | :---: |
179+
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
180+
| \`four.js\` | 4.5 kB | +9 B (+0.2%) | |
181+
| \`two.js\` | 5 kB | -2.5 kB (-33.33%) | 🎉 |
117182
118183
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
119184
120185
| Filename | Size |
121186
| :--- | :---: |
122187
| \`three.js\` | 300 B |
188+
| \`five.js\` | 6.5 kB |
123189
124190
</details>
125191

tests/utils.spec.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@ test('diffTable', () => {
4747
filename: 'four.js',
4848
size: 4500,
4949
delta: 9
50-
}
50+
},
51+
{
52+
filename: 'five.js',
53+
size: 6500,
54+
delta: 0
55+
},
5156
];
5257
const defaultOptions = {
5358
showTotal: true,
5459
collapseUnchanged: true,
5560
omitUnchanged: false,
56-
minimumChangeThreshold: 1
61+
minimumChangeThreshold: 1,
62+
orderBy: /** @type {const} */ ('Filename:asc')
5763
};
5864

5965
expect(diffTable(files, { ...defaultOptions })).toMatchSnapshot();
@@ -64,6 +70,10 @@ test('diffTable', () => {
6470
expect(diffTable(files.map(file => ({...file, delta: 0})), { ...defaultOptions })).toMatchSnapshot();
6571

6672
expect(diffTable([files[2]], { ...defaultOptions })).toMatchSnapshot();
73+
74+
expect(diffTable(files, { ...defaultOptions, orderBy: 'Filename:desc' })).toMatchSnapshot();
75+
expect(diffTable(files, { ...defaultOptions, orderBy: 'Size:asc' })).toMatchSnapshot();
76+
expect(diffTable(files, { ...defaultOptions, orderBy: 'Change:desc' })).toMatchSnapshot();
6777
});
6878

6979
test('getPackageManagerAndInstallScript', async () => {

0 commit comments

Comments
 (0)