From d6b581cebf9e4932499f75bd1ee640dbe595bdb3 Mon Sep 17 00:00:00 2001 From: LeeWannacott Date: Sat, 13 May 2023 02:03:04 +1200 Subject: [PATCH 1/9] WIP 1: progress on refactoring and optimizing... --- public/table-sort.js | 125 +++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 57 deletions(-) diff --git a/public/table-sort.js b/public/table-sort.js index e619beb..9cec6ac 100644 --- a/public/table-sort.js +++ b/public/table-sort.js @@ -16,6 +16,11 @@ Instructions: */ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { + let startTime, endTime; + if (testingTableSortJS) { + startTime = performance.now(); + } + function getHTMLTables() { if (testingTableSortJS === true) { const getTagTable = domDocumentWindow.getElementsByTagName("table"); @@ -59,65 +64,63 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { } } - function addInferredClass(th, columnLength, currentCount, classToAdd) { - const threshold = columnLength / 2; - if (currentCount >= threshold) { - th.classList.add(classToAdd); + function addInferredClasses(th, classes, thresholdToAddClass) { + for (let value of Object.values(classes)) { + if (value.count >= thresholdToAddClass) { + th.classList.add(value.class); + break; + } } } - 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++; - } - if (isoDatesMatch) { - isoDatesCounter++; - } + 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?)/; + let tableColumnLength = th.parentElement.childElementCount; + const thresholdToAddClass = tableColumnLength / 2; + let [runtimeCount, fileSizeCount, dmyDatesCount, isoDatesCount] = [ + 0, 0, 0, 0, + ]; + for (let tr of tableRows) { + let runtimeSortMatch, fileSizeSortMatch, datesDmyMatch, isoDatesMatch; + const tableColumn = tr.querySelectorAll("td").item(columnIndex); + if (tableColumn.innerText) { + runtimeSortMatch = tableColumn.innerText.match(regexMinutesAndSeconds); + fileSizeSortMatch = tableColumn.innerText.match(regexFileSizeSort); + datesDmyMatch = tableColumn.innerText.match(datesRegex); + isoDatesMatch = tableColumn.innerText.match(regexISODates); + } + if (runtimeSortMatch) { + runtimeCount++; + } + if (fileSizeSortMatch) { + fileSizeCount++; + } + if (datesDmyMatch) { + dmyDatesCount++; + } + if (isoDatesMatch) { + isoDatesCount++; + } + if ( + runtimeCount >= thresholdToAddClass || + fileSizeCount >= thresholdToAddClass || + dmyDatesCount >= thresholdToAddClass || + isoDatesMatch >= thresholdToAddClass + ) { + break; } - // 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" - ); } + const classes = { + runtime: { class: "runtime-sort", count: runtimeCount }, + filesize: { class: "file-size-sort", count: fileSizeCount }, + dmyDates: { class: "dates-dmy-sort", count: dmyDatesCount }, + ymdDates: { class: "dates-ymd-sort", count: isoDatesCount }, + }; + addInferredClasses(th, classes, thresholdToAddClass); } function makeTableSortable(sortableTable) { @@ -128,13 +131,17 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { const isNoSortClassInference = sortableTable.classList.contains("no-class-infer"); - if (!isNoSortClassInference) { - inferSortClasses(tableRows, tableHeadHeaders); - } + console.log(tableHeadHeaders); for (let [columnIndex, th] of tableHeadHeaders.entries()) { if (!th.classList.contains("disable-sort")) { th.style.cursor = "pointer"; + if (!isNoSortClassInference) { + const inferStart = performance.now(); + inferSortClasses(tableRows, columnIndex, th); + const inferEnd = performance.now(); + console.log(inferEnd - inferStart); + } makeEachColumnSortable(th, columnIndex, tableBody, sortableTable); } } @@ -509,6 +516,10 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { th.click(); } } + if (testingTableSortJS === true) { + endTime = performance.now(); + console.log("Time: ", (endTime - startTime).toFixed(2), " ms"); + } } if ( From 13067d9bdbbf0f8905c1fc2aeee924b2c25f0ffc Mon Sep 17 00:00:00 2001 From: LeeWannacott Date: Sat, 13 May 2023 21:53:55 +1200 Subject: [PATCH 2/9] WIP 2: progress on refactoring and optimizing... --- public/index.html | 4 +-- public/table-sort.js | 71 +++++++++++++++++++------------------------- 2 files changed, 32 insertions(+), 43 deletions(-) diff --git a/public/index.html b/public/index.html index fc7c5ec..763213b 100644 --- a/public/index.html +++ b/public/index.html @@ -33,7 +33,7 @@

Manual testing of table sort js

da Vinci Zarlo - 1452-4-15 + 1452/4/15 13000 1m 45s @@ -43,7 +43,7 @@

Manual testing of table sort js

Statham Jason - 1967-7-26 + 1967/7/26 HR 11m 40s diff --git a/public/table-sort.js b/public/table-sort.js index 9cec6ac..4ba3bb2 100644 --- a/public/table-sort.js +++ b/public/table-sort.js @@ -64,12 +64,18 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { } } - function addInferredClasses(th, classes, thresholdToAddClass) { - for (let value of Object.values(classes)) { - if (value.count >= thresholdToAddClass) { - th.classList.add(value.class); - break; + function addInferredClasses(th, match, classToAdd) { + let tableColumnLength = th.parentElement.childElementCount; + const thresholdToAddClass = Math.ceil(tableColumnLength / 2); + if (match) { + classToAdd.count++; + if (classToAdd.count >= thresholdToAddClass) { + th.classList.add(classToAdd.class); + return [classToAdd.count, true]; } + return [classToAdd.count, false]; + } else { + return [classToAdd.count, false]; } } @@ -79,48 +85,31 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { // 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 tableColumnLength = th.parentElement.childElementCount; - const thresholdToAddClass = tableColumnLength / 2; - let [runtimeCount, fileSizeCount, dmyDatesCount, isoDatesCount] = [ - 0, 0, 0, 0, - ]; + const classes = { + 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; for (let tr of tableRows) { - let runtimeSortMatch, fileSizeSortMatch, datesDmyMatch, isoDatesMatch; + let matches = { runtime: "", filesize: "", dmyDates: "", ymdDates: "" }; const tableColumn = tr.querySelectorAll("td").item(columnIndex); if (tableColumn.innerText) { - runtimeSortMatch = tableColumn.innerText.match(regexMinutesAndSeconds); - fileSizeSortMatch = tableColumn.innerText.match(regexFileSizeSort); - datesDmyMatch = tableColumn.innerText.match(datesRegex); - isoDatesMatch = tableColumn.innerText.match(regexISODates); - } - if (runtimeSortMatch) { - runtimeCount++; - } - if (fileSizeSortMatch) { - fileSizeCount++; + 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 (datesDmyMatch) { - dmyDatesCount++; - } - if (isoDatesMatch) { - isoDatesCount++; - } - if ( - runtimeCount >= thresholdToAddClass || - fileSizeCount >= thresholdToAddClass || - dmyDatesCount >= thresholdToAddClass || - isoDatesMatch >= thresholdToAddClass - ) { - break; + for (let key of Object.keys(classes)) { + [classes[key].count, classNameAdded] = addInferredClasses( th, matches[key], classes[key]); + if (classNameAdded) { + console.log(matches[key],classes[key]) + break; + } } + } - const classes = { - runtime: { class: "runtime-sort", count: runtimeCount }, - filesize: { class: "file-size-sort", count: fileSizeCount }, - dmyDates: { class: "dates-dmy-sort", count: dmyDatesCount }, - ymdDates: { class: "dates-ymd-sort", count: isoDatesCount }, - }; - addInferredClasses(th, classes, thresholdToAddClass); } function makeTableSortable(sortableTable) { From 6e7bd58efd1c9120201b4f61614ac21f81e85295 Mon Sep 17 00:00:00 2001 From: LeeWannacott Date: Sun, 14 May 2023 00:38:44 +1200 Subject: [PATCH 3/9] Done: refactored inferring sort classes from O n^2 to something less... --- public/table-sort.js | 66 +++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 37 deletions(-) diff --git a/public/table-sort.js b/public/table-sort.js index 4ba3bb2..459d2a9 100644 --- a/public/table-sort.js +++ b/public/table-sort.js @@ -16,11 +16,6 @@ Instructions: */ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { - let startTime, endTime; - if (testingTableSortJS) { - startTime = performance.now(); - } - function getHTMLTables() { if (testingTableSortJS === true) { const getTagTable = domDocumentWindow.getElementsByTagName("table"); @@ -64,36 +59,29 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { } } - function addInferredClasses(th, match, classToAdd) { - let tableColumnLength = th.parentElement.childElementCount; - const thresholdToAddClass = Math.ceil(tableColumnLength / 2); - if (match) { - classToAdd.count++; - if (classToAdd.count >= thresholdToAddClass) { - th.classList.add(classToAdd.class); - return [classToAdd.count, true]; - } - return [classToAdd.count, false]; - } else { - return [classToAdd.count, false]; - } - } - 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 classes = { + 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) { - let matches = { runtime: "", filesize: "", dmyDates: "", ymdDates: "" }; + 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); @@ -101,14 +89,26 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { matches.dmyDates = tableColumn.innerText.match(datesRegex); matches.ymdDates = tableColumn.innerText.match(regexISODates); } - for (let key of Object.keys(classes)) { - [classes[key].count, classNameAdded] = addInferredClasses( th, matches[key], classes[key]); - if (classNameAdded) { - console.log(matches[key],classes[key]) + if (regexNotFoundCount >= threshold) { + break; + } + 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 (inferableClasses[key].count >= threshold) { + th.classList.add(inferableClasses[key].class); + classNameAdded = true; break; } } - + if (classNameAdded) { + break; + } } } @@ -121,15 +121,11 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { const isNoSortClassInference = sortableTable.classList.contains("no-class-infer"); - console.log(tableHeadHeaders); for (let [columnIndex, th] of tableHeadHeaders.entries()) { if (!th.classList.contains("disable-sort")) { th.style.cursor = "pointer"; if (!isNoSortClassInference) { - const inferStart = performance.now(); inferSortClasses(tableRows, columnIndex, th); - const inferEnd = performance.now(); - console.log(inferEnd - inferStart); } makeEachColumnSortable(th, columnIndex, tableBody, sortableTable); } @@ -500,15 +496,11 @@ 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(); } } - if (testingTableSortJS === true) { - endTime = performance.now(); - console.log("Time: ", (endTime - startTime).toFixed(2), " ms"); - } } if ( From 6738576b4ce1022f68372085146db1307ad7f5c2 Mon Sep 17 00:00:00 2001 From: LeeWannacott Date: Sun, 14 May 2023 00:43:23 +1200 Subject: [PATCH 4/9] Move breaking out of loop if notfoundregex meets threshold. --- public/table-sort.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/public/table-sort.js b/public/table-sort.js index 459d2a9..4970cbc 100644 --- a/public/table-sort.js +++ b/public/table-sort.js @@ -76,6 +76,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { 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, @@ -89,9 +92,6 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { matches.dmyDates = tableColumn.innerText.match(datesRegex); matches.ymdDates = tableColumn.innerText.match(regexISODates); } - if (regexNotFoundCount >= threshold) { - break; - } if (Object.values(matches).every((match) => match === null)) { regexNotFoundCount++; continue; From c182e6020c68f6cf01bf681a3bc0f0d6fa24d39b Mon Sep 17 00:00:00 2001 From: Lee Wannacott Date: Sun, 14 May 2023 11:13:18 +1200 Subject: [PATCH 5/9] Update description in readme. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6e36cf8..1842f5b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ## TABLE-SORT-JS. -- Description: A JavaScript client-side HTML table sorting library with no dependencies required. +- Description: [Vanilla JS](http://vanilla-js.com/) HTML table sorting with sort type inference and browser extension available. - [Demo](https://leewannacott.github.io/Portfolio/#/GitHub) - [Documentation.](https://leewannacott.github.io/table-sort-js/docs/about.html) From 79b4553b3155ba6deec48d1a9390e69ea8bc9fe9 Mon Sep 17 00:00:00 2001 From: Lee Wannacott Date: Sun, 14 May 2023 11:14:58 +1200 Subject: [PATCH 6/9] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1842f5b..39b5994 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ - [Documentation.](https://leewannacott.github.io/table-sort-js/docs/about.html) (work in progress) - [npm package.](https://www.npmjs.com/package/table-sort-js) -- [firefox browser extension](https://addons.mozilla.org/en-US/firefox/addon/table-sort-js/) +- [firefox browser extension](https://addons.mozilla.org/en-US/firefox/addon/table-sort-js/) Make HTML tables of any website you visit sortable! ## Install instructions. From e8c53dd1bf3358ad520bf1b1a39dceb5053e7874 Mon Sep 17 00:00:00 2001 From: Lee Wannacott Date: Sun, 14 May 2023 11:20:43 +1200 Subject: [PATCH 7/9] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 39b5994..9809cb9 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,13 @@ ## TABLE-SORT-JS. -- Description: [Vanilla JS](http://vanilla-js.com/) HTML table sorting with sort type inference and browser extension available. +- Description: HTML table sorting library with sort type inference builtin and browser extension available. [#VanillaJS](http://vanilla-js.com/) - [Demo](https://leewannacott.github.io/Portfolio/#/GitHub) - [Documentation.](https://leewannacott.github.io/table-sort-js/docs/about.html) (work in progress) - [npm package.](https://www.npmjs.com/package/table-sort-js) -- [firefox browser extension](https://addons.mozilla.org/en-US/firefox/addon/table-sort-js/) Make HTML tables of any website you visit sortable! +- [firefox browser extension](https://addons.mozilla.org/en-US/firefox/addon/table-sort-js/): Tables of any website you visit become sortable! ## Install instructions. From ca8fa2617f7203c3daa4709ecc8610ee7119918a Mon Sep 17 00:00:00 2001 From: LeeWannacott Date: Sun, 14 May 2023 21:47:24 +1200 Subject: [PATCH 8/9] wip 3: Refactor out loop. --- public/table-sort.js | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/public/table-sort.js b/public/table-sort.js index 4970cbc..c7bc365 100644 --- a/public/table-sort.js +++ b/public/table-sort.js @@ -60,44 +60,30 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { } 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; + const runtimeRegex = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i; + const fileSizeRegex = /^([.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 dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/; + const ymdRegex = /^(\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 }, + runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 }, + filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 }, + dmyDates: { regexp: dmyRegex, class: "dates-dmy-sort", count: 0 }, + ymdDates: { regexp: ymdRegex, class: "dates-ymd-sort", count: 0 }, }; let classNameAdded = false; let regexNotFoundCount = 0; - let tableColumnLength = th.parentElement.childElementCount; - const threshold = Math.floor(tableColumnLength / 2); + const threshold = Math.ceil(tableRows.length/ 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; - } + let foundMatch = false; for (let key of Object.keys(inferableClasses)) { - if (matches[key] !== null) { + let classRegexp = inferableClasses[key].regexp; + if (tableColumn.innerText.match(classRegexp) !== null) { + foundMatch = true; inferableClasses[key].count++; } if (inferableClasses[key].count >= threshold) { @@ -109,6 +95,10 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { if (classNameAdded) { break; } + if (!foundMatch) { + regexNotFoundCount++; + continue; + } } } From 95fe4462aae4eec7fd6e0fdb629e9743f5585b31 Mon Sep 17 00:00:00 2001 From: LeeWannacott Date: Sun, 14 May 2023 21:57:31 +1200 Subject: [PATCH 9/9] Fix failing tests by checking tableRows.innertext() not false before making match. --- public/table-sort.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/public/table-sort.js b/public/table-sort.js index c7bc365..79bf2c6 100644 --- a/public/table-sort.js +++ b/public/table-sort.js @@ -73,7 +73,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { }; let classNameAdded = false; let regexNotFoundCount = 0; - const threshold = Math.ceil(tableRows.length/ 2); + const threshold = Math.ceil(tableRows.length / 2); for (let tr of tableRows) { if (regexNotFoundCount >= threshold) { break; @@ -82,9 +82,11 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { 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 (tableColumn.innerText) { + if (tableColumn.innerText.match(classRegexp) !== null) { + foundMatch = true; + inferableClasses[key].count++; + } } if (inferableClasses[key].count >= threshold) { th.classList.add(inferableClasses[key].class);