Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time sort #80

Merged
merged 3 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"],
});
});