Skip to content

Commit 00952e2

Browse files
Feature: runtime-sort (#80)
* Add test for time-sort. * WIP: Time sort regexing... * runtime-sort completed.
1 parent 75685ee commit 00952e2

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
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. |

public/table-sort.js

Lines changed: 34 additions & 1 deletion
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
};

test/table.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,38 @@ test("Clicking multiple times (>2) doesn't break sorting", () => {
242242
classTags: "",
243243
},
244244
{
245-
colsToClick: [0,0,0,0,0],
245+
colsToClick: [0, 0, 0, 0, 0],
246246
}
247247
)
248248
).toStrictEqual({
249249
col0: ["alpha", "beta", "charlie"],
250250
col1: ["carrie", "fisher", "doris"],
251251
});
252252
});
253+
254+
test("time-sort class", () => {
255+
expect(
256+
createTestTable(
257+
{
258+
col0: [
259+
"2m 52s",
260+
"3s",
261+
"7s",
262+
"11m 40s",
263+
"36s",
264+
"9m 44s",
265+
"1m 36s",
266+
"41s",
267+
],
268+
},
269+
{
270+
classTags: "runtime-sort",
271+
},
272+
{
273+
colsToClick: [0],
274+
}
275+
)
276+
).toStrictEqual({
277+
col0: ["3s", "7s", "36s", "41s", "1m 36s", "2m 52s", "9m 44s", "11m 40s"],
278+
});
279+
});

0 commit comments

Comments
 (0)