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

Refactor and optimize #86

Merged
merged 9 commits into from
May 14, 2023
4 changes: 2 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1>Manual testing of table sort js</h1>
<tr>
<td>da Vinci</td>
<td>Zarlo</td>
<td>1452-4-15</td>
<td>1452/4/15</td>
<td>13000</td>
<td></td>
<td>1m 45s</td>
Expand All @@ -43,7 +43,7 @@ <h1>Manual testing of table sort js</h1>
<tr>
<td>Statham</td>
<td>Jason</td>
<td>1967-7-26</td>
<td>1967/7/26</td>
<td></td>
<td>HR</td>
<td>11m 40s</td>
Expand Down
112 changes: 52 additions & 60 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,64 +59,56 @@ 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;
// Doesn't infer dates with delimiter "."; as could capture semantic version numbers.
const datesRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
const regexISODates = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
let runtimeCounter = 0,
fileSizeCounter = 0,
datesCounter = 0,
isoDatesCounter = 0;
let tableColumnLength = th.parentElement.childElementCount;
for (let tr of tableRows) {
let runtimeSortMatch, fileSizeSortMatch, datesMatch, isoDatesMatch;
const tableColumn = tr.querySelectorAll("td").item(columnIndex);
if (tableColumn.innerText) {
runtimeSortMatch = tableColumn.innerText.match(
regexMinutesAndSeconds
);
fileSizeSortMatch = tableColumn.innerText.match(regexFileSizeSort);
datesMatch = tableColumn.innerText.match(datesRegex);
isoDatesMatch = tableColumn.innerText.match(regexISODates);
}
if (runtimeSortMatch) {
runtimeCounter++;
}
if (fileSizeSortMatch) {
fileSizeCounter++;
}
if (datesMatch) {
datesCounter++;
function inferSortClasses(tableRows, columnIndex, th) {
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;
// Doesn't infer dates with delimiter "."; as could capture semantic version numbers.
const datesRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
const regexISODates = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
const inferableClasses = {
runtime: { class: "runtime-sort", count: 0 },
filesize: { class: "file-size-sort", count: 0 },
dmyDates: { class: "dates-dmy-sort", count: 0 },
ymdDates: { class: "dates-ymd-sort", count: 0 },
};
let classNameAdded = false;
let regexNotFoundCount = 0;
let tableColumnLength = th.parentElement.childElementCount;
const threshold = Math.floor(tableColumnLength / 2);
for (let tr of tableRows) {
if (regexNotFoundCount >= threshold) {
break;
}
let matches = {
runtime: null,
filesize: null,
dmyDates: null,
ymdDates: null,
};
const tableColumn = tr.querySelectorAll("td").item(columnIndex);
if (tableColumn.innerText) {
matches.runtime = tableColumn.innerText.match(regexMinutesAndSeconds);
matches.filesize = tableColumn.innerText.match(regexFileSizeSort);
matches.dmyDates = tableColumn.innerText.match(datesRegex);
matches.ymdDates = tableColumn.innerText.match(regexISODates);
}
if (Object.values(matches).every((match) => match === null)) {
regexNotFoundCount++;
continue;
}
for (let key of Object.keys(inferableClasses)) {
if (matches[key] !== null) {
inferableClasses[key].count++;
}
if (isoDatesMatch) {
isoDatesCounter++;
if (inferableClasses[key].count >= threshold) {
th.classList.add(inferableClasses[key].class);
classNameAdded = true;
break;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (Object.values(matches).every((match) => match === null)) {
regexNotFoundCount++;
continue;
}
for (let key of Object.keys(inferableClasses)) {
if (matches[key] !== null) {
inferableClasses[key].count++;
}
if (isoDatesMatch) {
isoDatesCounter++;
if (inferableClasses[key].count >= threshold) {
th.classList.add(inferableClasses[key].class);
classNameAdded = true;
break;
}
}
let foundMatch = false;
for (let key of Object.keys(inferableClasses)) {
let classRegexp = inferableClasses[key].regexp;
if (tableColumn.innerText.match(classRegexp) !== null) {
foundMatch = true;
inferableClasses[key].count++;
}
if (inferableClasses[key].count >= threshold) {
th.classList.add(inferableClasses[key].class);
classNameAdded = true;
break;
}
}
if (!foundMatch) {
regexNotFoundCount++;
continue;
}

// TODO: refactor this into one function called addInferredClasses that loops over sort classes and counters
addInferredClass(th, tableColumnLength, runtimeCounter, "runtime-sort");
addInferredClass(
th,
tableColumnLength,
fileSizeCounter,
"file-size-sort"
);
addInferredClass(th, tableColumnLength, datesCounter, "dates-dmy-sort");
addInferredClass(
th,
tableColumnLength,
isoDatesCounter,
"dates-ymd-sort"
);
if (classNameAdded) {
break;
}
}
}

Expand All @@ -128,13 +120,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {

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")) {
th.style.cursor = "pointer";
if (!isNoSortClassInference) {
inferSortClasses(tableRows, columnIndex, th);
}
makeEachColumnSortable(th, columnIndex, tableBody, sortableTable);
}
}
Expand Down Expand Up @@ -504,8 +496,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
getTableData(tableProperties);
updateTable(tableProperties);
});
let isOnloadSort = th.classList.contains("onload-sort");
if (isOnloadSort) {

if (th.classList.contains("onload-sort")) {
th.click();
}
}
Expand Down