Skip to content

Commit ce636c5

Browse files
npm release 1.11.2; runtime-sort class for sorting github actions runtime column.
1 parent 00952e2 commit ce636c5

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

npm/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Refer to the documenation for examples on how to use table-sort-js with [HTML](h
5353
| "data-sort" | Sort by [data attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes), e.g <td data-sort="42"> |
5454
| "onload-sort" | Sort column on loading of the page. Simulates a click from the user. (can only sort onload for one column) |
5555
| "file-size-sort" | Sort file sizes(B->TiB) uses the binary prefix. (e.g KiB) |
56+
| "runtime-sort" | Sorts runtime in minutes and seconds e.g (1m 20s). Useful for sorting the GitHub actions Run time column... |
5657
| "disable-sort" | Disallow sorting the table by this specific column. |
5758
| "alpha-sort" | Sort alphabetically (z11,z2); default is [natural sort](https://en.wikipedia.org/wiki/Natural_sort_order) (z2,z11). |
5859
| "punct-sort" | Sort punctuation; default ignores punctuation. |

npm/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "table-sort-js",
3-
"version": "1.10.2",
3+
"version": "1.11.2",
44
"description": "A JavaScript client-side HTML table sorting library with no dependencies required.",
55
"license": "MIT",
66
"repository": "LeeWannacott/table-sort-js",

npm/table-sort.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,32 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
122122
}
123123
}
124124

125+
function sortByRuntime(tableRows, columnData) {
126+
for (let [i, tr] of tableRows.entries()) {
127+
const regexMinutesAndSeconds = /^(\d+m)\s?(\d+s)$/i;
128+
let columnOfTd = tr
129+
.querySelectorAll("td")
130+
.item(columnIndex).textContent;
131+
let match = columnOfTd.match(regexMinutesAndSeconds);
132+
let minutesInSeconds,
133+
seconds,
134+
timeinSeconds = [0, 0, 0];
135+
if (match) {
136+
const regexMinutes = match[1];
137+
if (regexMinutes) {
138+
minutesInSeconds = Number(regexMinutes.replace("m", "")) * 60;
139+
}
140+
const regexSeconds = match[2];
141+
if (regexSeconds) {
142+
seconds = Number(regexSeconds.replace("s", ""));
143+
}
144+
timeinSeconds = minutesInSeconds + seconds;
145+
}
146+
columnData.push(`${timeinSeconds}#${i}`);
147+
columnIndexAndTableRow[columnData[i]] = tr.innerHTML;
148+
}
149+
}
150+
125151
let [timesClickedColumn, columnIndexesClicked] = [0, []];
126152

127153
function rememberSort(timesClickedColumn, columnIndexesClicked) {
@@ -152,6 +178,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
152178
tableRows,
153179
columnData,
154180
isFileSize,
181+
isTimeSort,
155182
isDataAttribute,
156183
colSpanData,
157184
colSpanSum,
@@ -171,7 +198,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
171198
if (isFileSize) {
172199
fileSizeColumnTextAndRow[columnData[i]] = tr.innerHTML;
173200
}
174-
if (!isFileSize && !isDataAttribute) {
201+
if (!isFileSize && !isDataAttribute && !isTimeSort) {
175202
columnData.push(`${tdTextContent}#${i}`);
176203
columnIndexAndTableRow[`${tdTextContent}#${i}`] = tr.innerHTML;
177204
}
@@ -307,6 +334,11 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
307334
sortFileSize(visibleTableRows, columnData);
308335
}
309336

337+
const isTimeSort = th.classList.contains("runtime-sort");
338+
if (isTimeSort) {
339+
sortByRuntime(visibleTableRows, columnData);
340+
}
341+
310342
const isRememberSort = sortableTable.classList.contains("remember-sort");
311343
if (!isRememberSort) {
312344
rememberSort(timesClickedColumn, columnIndexesClicked);
@@ -320,6 +352,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
320352
columnData,
321353
isFileSize,
322354
isDataAttribute,
355+
isTimeSort,
323356
colSpanData,
324357
colSpanSum,
325358
};

0 commit comments

Comments
 (0)