Skip to content

Commit

Permalink
Feature: Sort class inference for runtime and file size sorts automat…
Browse files Browse the repository at this point in the history
…ically! (#81)
  • Loading branch information
LeeWannacott committed May 9, 2023
1 parent ce636c5 commit 4c2ed4e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 20 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ Refer to the documenation for examples on how to use table-sort-js with [HTML](h

#### Classes:

| <table> classes | Description |
| --------------------- | ----------------------------------------------------------------------- |
| "table-sort" | Make the table sortable! (Words, numbers, dates) |
| "table-arrows" | Display ascending or descending triangles. |
| "remember-sort" | If clicking on different columns remembers sort of the original column. |
| <table> classes | Description |
| --------------------- | --------------------------------------------------------------------------------------------|
| "table-sort" | Make the table sortable! (Words, numbers)... |
| "table-arrows" | Display ascending or descending triangles. |
| "no-class-infer" | Turns off inference for adding sort classes automatically (file-size-sort and runtime-sort).|
| "remember-sort" | If clicking on different columns remembers sort of the original column. |

| <th> classes | Description |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------- |
Expand Down
1 change: 1 addition & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cp public/table-sort.js npm/table-sort.js
cp public/table-sort.js browser-extension/table-sort.js
cp README.md npm/README.md
cp LICENSE npm/LICENSE
cp Contributors.md npm/Contributors.md
Expand Down
18 changes: 15 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<!DOCTYPE html5>
<!DOCTYPE html>
<html>
<head></head>

<body>
<div id="display"></div>
<script src="table-sort.js"></script>
<script src="../public//table-sort.js"></script>

<h1>Testing table sort js</h1>
<table class="table-sort table-arrows">
<thead>
<tr>
<th class="onload-sort">Last Name</th>
<th >Last Name</th>
<th>First Name</th>
<th class="order-by-desc">Birth Date</th>
<th>Employee ID</th>
<th>Department</th>
<th>Runtime</th>
<th class="onload-sort">File Size</th>
</tr>
</thead>
<tr>
Expand All @@ -23,27 +25,35 @@ <h1>Testing table sort js</h1>
<td>1706,1,17</td>
<td></td>
<td>k-level</td>
<td>1h 1m 17s</td>
<td>10b</td>
</tr>
<tr>
<td>da Vinci</td>
<td>Zarlo</td>
<td>1452.4.15</td>
<td></td>
<td></td>
<td>1m 45s</td>
<td>192038998987021b</td>
</tr>
<tr>
<td>Statham</td>
<td></td>
<td>1967-7-26</td>
<td></td>
<td>HR</td>
<td>11m 40s</td>
<td>134809b</td>
</tr>
<tr>
<td>Micheal</td>
<td></td>
<td>1958/8/21</td>
<td>54</td>
<td>Marketing</td>
<td>29s</td>
<td>30980980b</td>
</tr>

<tr>
Expand All @@ -52,6 +62,8 @@ <h1>Testing table sort js</h1>
<td>1994/9/23</td>
<td>134</td>
<td>Marketing</td>
<td>41s</td>
<td>902938402398b</td>
</tr>
</table>
</body>
Expand Down
70 changes: 63 additions & 7 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,64 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}
}

function addInferredClass(th, columnLength, currentCount, classToAdd) {
const threshold = columnLength / 2;
if (currentCount >= threshold) {
th.classList.add(classToAdd);
}
}

function inferSortClasses(tableRows, tableHeadHeaders) {
for (let [columnIndex, th] of tableHeadHeaders.entries()) {
const regexMinutesAndSeconds = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i;
const regexFileSizeSort = /^([.0-9]+)\s?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)/i;
let runtimeSortCounter = 0,
fileSizeSortCounter = 0;

let tableColumnLength = th.parentElement.childElementCount;
for (let tr of tableRows) {
let runtimeSortMatch, fileSizeSortMatch;
const tableColumn = tr.querySelectorAll("td").item(columnIndex);
if (tableColumn.innerText) {
runtimeSortMatch = tableColumn.innerText.match(
regexMinutesAndSeconds
);
fileSizeSortMatch = tableColumn.innerText.match(regexFileSizeSort);
}
if (runtimeSortMatch) {
runtimeSortCounter++;
}
if (fileSizeSortMatch) {
fileSizeSortCounter++;
}
}
// TODO: refactor this into one function called addInferredClasses that loops over sort classes and counters
addInferredClass(
th,
tableColumnLength,
runtimeSortCounter,
"runtime-sort"
);
addInferredClass(
th,
tableColumnLength,
fileSizeSortCounter,
"file-size-sort"
);
}
}

function makeTableSortable(sortableTable) {
const tableBody = getTableBody(sortableTable);
const tableHead = sortableTable.querySelector("thead");
const tableHeadHeaders = tableHead.querySelectorAll("th");
const tableRows = tableBody.querySelectorAll("tr");

const isNoSortClassInference =
sortableTable.classList.contains("no-class-infer");
if (!isNoSortClassInference) {
inferSortClasses(tableRows, tableHeadHeaders);
}

for (let [columnIndex, th] of tableHeadHeaders.entries()) {
if (!th.classList.contains("disable-sort")) {
Expand Down Expand Up @@ -124,24 +178,26 @@ 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;
const regexMinutesAndSeconds = /^(\d+h)?\s?(\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];
let [minutesInSeconds, hours, seconds, timeinSeconds] = [0, 0, 0, 0];
if (match) {
const regexMinutes = match[1];
const regexHours = match[1];
if (regexHours) {
hours = Number(regexHours.replace("h", "")) * 60 * 60;
}
const regexMinutes = match[2];
if (regexMinutes) {
minutesInSeconds = Number(regexMinutes.replace("m", "")) * 60;
}
const regexSeconds = match[2];
const regexSeconds = match[3];
if (regexSeconds) {
seconds = Number(regexSeconds.replace("s", ""));
}
timeinSeconds = minutesInSeconds + seconds;
timeinSeconds = hours + minutesInSeconds + seconds;
}
columnData.push(`${timeinSeconds}#${i}`);
columnIndexAndTableRow[columnData[i]] = tr.innerHTML;
Expand Down
6 changes: 3 additions & 3 deletions src/test-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export class App extends Component {
<th className="order-by-desc">Forks</th>
<th className="order-by-desc">Open issues</th>
<th className="order-by-desc">Watchers</th>
<th className="order-by-desc">Stars</th>
<th className="order-by-desc">Size (MB)</th>
<th className="order-by-desc">Sktars</th>
<th className="onload-sort">Size (MB)</th>
</tr>
</thead>
<tbody className="table-hover">
Expand All @@ -114,7 +114,7 @@ export class App extends Component {
<td> {repo.open_issues}</td>
<td> {repo.watchers}</td>
<td> {repo.stargazers_count}</td>
<td> {Math.round(repo.size / 1000)}</td>
<td> {repo.size * 1000 + "B"}</td>
</tr>
))}
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const tableSortJs = require("../public/table-sort");
function createTestTable(
testTableData,
thAttributes = { classTags: "", colspan: "" },
props = { colsToClick: [], invisibleIndex: [] }
props = { colsToClick: [], invisibleIndex: [] ,tableTags:""}
) {
const numberOfTableColumns = Object.keys(testTableData).length;
let testTableHeaders = "";
Expand Down
17 changes: 16 additions & 1 deletion test/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,13 @@ test("time-sort class", () => {
{
col0: [
"2m 52s",
"1h 20m 10s",
"3s",
"11h 10m 10s",
"7s",
"11m 40s",
"36s",
"1h 10m 10s",
"9m 44s",
"1m 36s",
"41s",
Expand All @@ -274,6 +277,18 @@ test("time-sort class", () => {
}
)
).toStrictEqual({
col0: ["3s", "7s", "36s", "41s", "1m 36s", "2m 52s", "9m 44s", "11m 40s"],
col0: [
"3s",
"7s",
"36s",
"41s",
"1m 36s",
"2m 52s",
"9m 44s",
"11m 40s",
"1h 10m 10s",
"1h 20m 10s",
"11h 10m 10s",
],
});
});

0 comments on commit 4c2ed4e

Please sign in to comment.