Skip to content

Commit

Permalink
Feature: runtime-sort (#80)
Browse files Browse the repository at this point in the history
* Add test for time-sort.

* WIP: Time sort regexing...

* runtime-sort completed.
  • Loading branch information
LeeWannacott committed May 8, 2023
1 parent 75685ee commit 00952e2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Refer to the documenation for examples on how to use table-sort-js with [HTML](h
| "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"> |
| "onload-sort" | Sort column on loading of the page. Simulates a click from the user. (can only sort onload for one column) |
| "file-size-sort" | Sort file sizes(B->TiB) uses the binary prefix. (e.g KiB) |
| "runtime-sort" | Sorts runtime in minutes and seconds e.g (1m 20s). Useful for sorting the GitHub actions Run time column... |
| "disable-sort" | Disallow sorting the table by this specific column. |
| "alpha-sort" | Sort alphabetically (z11,z2); default is [natural sort](https://en.wikipedia.org/wiki/Natural_sort_order) (z2,z11). |
| "punct-sort" | Sort punctuation; default ignores punctuation. |
Expand Down
35 changes: 34 additions & 1 deletion public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}
}

function sortByRuntime(tableRows, columnData) {
for (let [i, tr] of tableRows.entries()) {
const regexMinutesAndSeconds = /^(\d+m)\s?(\d+s)$/i;
let columnOfTd = tr
.querySelectorAll("td")
.item(columnIndex).textContent;
let match = columnOfTd.match(regexMinutesAndSeconds);
let minutesInSeconds,
seconds,
timeinSeconds = [0, 0, 0];
if (match) {
const regexMinutes = match[1];
if (regexMinutes) {
minutesInSeconds = Number(regexMinutes.replace("m", "")) * 60;
}
const regexSeconds = match[2];
if (regexSeconds) {
seconds = Number(regexSeconds.replace("s", ""));
}
timeinSeconds = minutesInSeconds + seconds;
}
columnData.push(`${timeinSeconds}#${i}`);
columnIndexAndTableRow[columnData[i]] = tr.innerHTML;
}
}

let [timesClickedColumn, columnIndexesClicked] = [0, []];

function rememberSort(timesClickedColumn, columnIndexesClicked) {
Expand Down Expand Up @@ -152,6 +178,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
tableRows,
columnData,
isFileSize,
isTimeSort,
isDataAttribute,
colSpanData,
colSpanSum,
Expand All @@ -171,7 +198,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
if (isFileSize) {
fileSizeColumnTextAndRow[columnData[i]] = tr.innerHTML;
}
if (!isFileSize && !isDataAttribute) {
if (!isFileSize && !isDataAttribute && !isTimeSort) {
columnData.push(`${tdTextContent}#${i}`);
columnIndexAndTableRow[`${tdTextContent}#${i}`] = tr.innerHTML;
}
Expand Down Expand Up @@ -307,6 +334,11 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
sortFileSize(visibleTableRows, columnData);
}

const isTimeSort = th.classList.contains("runtime-sort");
if (isTimeSort) {
sortByRuntime(visibleTableRows, columnData);
}

const isRememberSort = sortableTable.classList.contains("remember-sort");
if (!isRememberSort) {
rememberSort(timesClickedColumn, columnIndexesClicked);
Expand All @@ -320,6 +352,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
columnData,
isFileSize,
isDataAttribute,
isTimeSort,
colSpanData,
colSpanSum,
};
Expand Down
29 changes: 28 additions & 1 deletion test/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,38 @@ test("Clicking multiple times (>2) doesn't break sorting", () => {
classTags: "",
},
{
colsToClick: [0,0,0,0,0],
colsToClick: [0, 0, 0, 0, 0],
}
)
).toStrictEqual({
col0: ["alpha", "beta", "charlie"],
col1: ["carrie", "fisher", "doris"],
});
});

test("time-sort class", () => {
expect(
createTestTable(
{
col0: [
"2m 52s",
"3s",
"7s",
"11m 40s",
"36s",
"9m 44s",
"1m 36s",
"41s",
],
},
{
classTags: "runtime-sort",
},
{
colsToClick: [0],
}
)
).toStrictEqual({
col0: ["3s", "7s", "36s", "41s", "1m 36s", "2m 52s", "9m 44s", "11m 40s"],
});
});

0 comments on commit 00952e2

Please sign in to comment.