diff --git a/npm/README.md b/npm/README.md index 5857fd2..3275780 100644 --- a/npm/README.md +++ b/npm/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/npm/package.json b/npm/package.json index afa220a..10a3564 100644 --- a/npm/package.json +++ b/npm/package.json @@ -1,6 +1,6 @@ { "name": "table-sort-js", - "version": "1.10.2", + "version": "1.11.2", "description": "A JavaScript client-side HTML table sorting library with no dependencies required.", "license": "MIT", "repository": "LeeWannacott/table-sort-js", diff --git a/npm/table-sort.js b/npm/table-sort.js index afff574..274eebd 100644 --- a/npm/table-sort.js +++ b/npm/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, };