From 00952e2de29ba5a4192307ee5f65c6e0bbccf1ef Mon Sep 17 00:00:00 2001 From: Lee Wannacott Date: Mon, 8 May 2023 20:25:42 +1200 Subject: [PATCH] Feature: runtime-sort (#80) * Add test for time-sort. * WIP: Time sort regexing... * runtime-sort completed. --- README.md | 1 + public/table-sort.js | 35 ++++++++++++++++++++++++++++++++++- test/table.test.js | 29 ++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5857fd2..3275780 100644 --- a/README.md +++ b/README.md @@ -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. | diff --git a/public/table-sort.js b/public/table-sort.js index afff574..274eebd 100644 --- a/public/table-sort.js +++ b/public/table-sort.js @@ -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) { @@ -152,6 +178,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { tableRows, columnData, isFileSize, + isTimeSort, isDataAttribute, colSpanData, colSpanSum, @@ -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; } @@ -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); @@ -320,6 +352,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { columnData, isFileSize, isDataAttribute, + isTimeSort, colSpanData, colSpanSum, }; diff --git a/test/table.test.js b/test/table.test.js index e65875d..53e1f2b 100644 --- a/test/table.test.js +++ b/test/table.test.js @@ -242,7 +242,7 @@ test("Clicking multiple times (>2) doesn't break sorting", () => { classTags: "", }, { - colsToClick: [0,0,0,0,0], + colsToClick: [0, 0, 0, 0, 0], } ) ).toStrictEqual({ @@ -250,3 +250,30 @@ test("Clicking multiple times (>2) doesn't break sorting", () => { 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"], + }); +});