Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeWannacott committed May 12, 2023
1 parent 13c4dbb commit d5a32cd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html><head><link href="/table-sort-js/static/css/main.0f3f41ac.chunk.css" rel="stylesheet"></head><body><div id="display"></div><script src="../public//table-sort.js"></script><h1>Manual testing of table sort js</h1><table class="table-sort table-arrows"><thead><tr><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><th>dates in dd/mm/yyyy</th></tr></thead><tr><td>Franklin</td><td>Benjamin</td><td>1706,1,17</td><td>1</td><td>k-level</td><td>1h 1m 17s</td><td>10b</td><td>17/6/1978</td></tr><tr><td>da Vinci</td><td>Zarlo</td><td>1452.4.15</td><td>13000</td><td></td><td>1m 45s</td><td>192038998987021b</td><td>18/10/2027</td></tr><tr><td>Statham</td><td>Jason</td><td>1967-7-26</td><td></td><td>HR</td><td>11m 40s</td><td>134809b</td><td>4/9/2008</td></tr><tr><td>Micheal</td><td>Angelo</td><td>1958/8/21</td><td>54</td><td>Marketing</td><td>29s</td><td>30980980b</td><td>2/3/1879</td></tr><tr><td>Ben</td><td></td><td>1994/9/23</td><td>134</td><td>Marketing</td><td>41s</td><td>902938402398b</td><td>8/6/1978</td></tr></table><script>!function(e){function t(t){for(var n,l,a=t[0],f=t[1],i=t[2],c=0,s=[];c<a.length;c++)l=a[c],Object.prototype.hasOwnProperty.call(o,l)&&o[l]&&s.push(o[l][0]),o[l]=0;for(n in f)Object.prototype.hasOwnProperty.call(f,n)&&(e[n]=f[n]);for(p&&p(t);s.length;)s.shift()();return u.push.apply(u,i||[]),r()}function r(){for(var e,t=0;t<u.length;t++){for(var r=u[t],n=!0,a=1;a<r.length;a++){var f=r[a];0!==o[f]&&(n=!1)}n&&(u.splice(t--,1),e=l(l.s=r[0]))}return e}var n={},o={1:0},u=[];function l(t){if(n[t])return n[t].exports;var r=n[t]={i:t,l:!1,exports:{}};return e[t].call(r.exports,r,r.exports,l),r.l=!0,r.exports}l.m=e,l.c=n,l.d=function(e,t,r){l.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},l.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},l.t=function(e,t){if(1&t&&(e=l(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(l.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)l.d(r,n,function(t){return e[t]}.bind(null,n));return r},l.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return l.d(t,"a",t),t},l.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},l.p="/table-sort-js/";var a=this["webpackJsonptable-sort-js"]=this["webpackJsonptable-sort-js"]||[],f=a.push.bind(a);a.push=t,a=a.slice();for(var i=0;i<a.length;i++)t(a[i]);var p=f;r()}([])</script><script src="/table-sort-js/static/js/2.6f1221a6.chunk.js"></script><script src="/table-sort-js/static/js/main.b5316d8e.chunk.js"></script></body></html>
<!doctype html><html><head><link href="/table-sort-js/static/css/main.0f3f41ac.chunk.css" rel="stylesheet"></head><body><div id="display"></div><script src="../public//table-sort.js"></script><h1>Manual testing of table sort js</h1><table class="table-sort table-arrows"><thead><tr><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><th>dates in dd/mm/yyyy</th></tr></thead><tr><td>Franklin</td><td>Benjamin</td><td>1706/1/17</td><td>1</td><td>k-level</td><td>1h 1m 17s</td><td>10b</td><td>17/6/1978</td></tr><tr><td>da Vinci</td><td>Zarlo</td><td>1452-4-15</td><td>13000</td><td></td><td>1m 45s</td><td>192038998987021b</td><td>18/10/2027</td></tr><tr><td>Statham</td><td>Jason</td><td>1967-7-26</td><td></td><td>HR</td><td>11m 40s</td><td>134809b</td><td>4/9/2008</td></tr><tr><td>Micheal</td><td>Angelo</td><td>1958/8/21</td><td>54</td><td>Marketing</td><td>29s</td><td>30980980b</td><td>2/3/1879</td></tr><tr><td>Ben</td><td></td><td>1994/9/23</td><td>134</td><td>Marketing</td><td>41s</td><td>902938402398b</td><td>8/6/1978</td></tr></table><script>!function(e){function t(t){for(var n,l,a=t[0],f=t[1],i=t[2],c=0,s=[];c<a.length;c++)l=a[c],Object.prototype.hasOwnProperty.call(o,l)&&o[l]&&s.push(o[l][0]),o[l]=0;for(n in f)Object.prototype.hasOwnProperty.call(f,n)&&(e[n]=f[n]);for(p&&p(t);s.length;)s.shift()();return u.push.apply(u,i||[]),r()}function r(){for(var e,t=0;t<u.length;t++){for(var r=u[t],n=!0,a=1;a<r.length;a++){var f=r[a];0!==o[f]&&(n=!1)}n&&(u.splice(t--,1),e=l(l.s=r[0]))}return e}var n={},o={1:0},u=[];function l(t){if(n[t])return n[t].exports;var r=n[t]={i:t,l:!1,exports:{}};return e[t].call(r.exports,r,r.exports,l),r.l=!0,r.exports}l.m=e,l.c=n,l.d=function(e,t,r){l.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},l.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},l.t=function(e,t){if(1&t&&(e=l(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(l.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)l.d(r,n,function(t){return e[t]}.bind(null,n));return r},l.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return l.d(t,"a",t),t},l.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},l.p="/table-sort-js/";var a=this["webpackJsonptable-sort-js"]=this["webpackJsonptable-sort-js"]||[],f=a.push.bind(a);a.push=t,a=a.slice();for(var i=0;i<a.length;i++)t(a[i]);var p=f;r()}([])</script><script src="/table-sort-js/static/js/2.6f1221a6.chunk.js"></script><script src="/table-sort-js/static/js/main.b5316d8e.chunk.js"></script></body></html>
78 changes: 43 additions & 35 deletions table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,49 +70,52 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
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;
const regexDates = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
let runtimeSortCounter = 0,
fileSizeSortCounter = 0,
datesSortCounter = 0;
// 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, datesSortMatch;
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);
datesSortMatch = tableColumn.innerText.match(regexDates);
datesMatch = tableColumn.innerText.match(datesRegex);
isoDatesMatch = tableColumn.innerText.match(regexISODates);
}
if (runtimeSortMatch) {
runtimeSortCounter++;
runtimeCounter++;
}
if (fileSizeSortMatch) {
fileSizeSortCounter++;
fileSizeCounter++;
}
if (datesSortMatch) {
datesSortCounter++;
if (datesMatch) {
datesCounter++;
}
if (isoDatesMatch) {
isoDatesCounter++;
}
}
// TODO: refactor this into one function called addInferredClasses that loops over sort classes and counters
addInferredClass(th, tableColumnLength, runtimeCounter, "runtime-sort");
addInferredClass(
th,
tableColumnLength,
runtimeSortCounter,
"runtime-sort"
);
addInferredClass(
th,
tableColumnLength,
fileSizeSortCounter,
fileSizeCounter,
"file-size-sort"
);
addInferredClass(th, tableColumnLength, datesCounter, "dates-dmy-sort");
addInferredClass(
th,
tableColumnLength,
datesSortCounter,
"dates-dmy-sort"
isoDatesCounter,
"dates-ymd-sort"
);
}
}
Expand Down Expand Up @@ -226,31 +229,30 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}
}

function sortDates(dateFormat, tableRows, columnData) {
function sortDates(datesFormat, tableRows, columnData) {
try {
for (let [i, tr] of tableRows.entries()) {
let columnOfTd;
const regexDate = /^(\d\d?)[./-](\d\d?)[./-]((\d\d)?\d\d)$/;
let columnOfTd, datesRegex;
if (datesFormat === "mdy" || datesFormat === "dmy") {
datesRegex = /^(\d\d?)[./-](\d\d?)[./-]((\d\d)?\d\d)/;
} else if (datesFormat === "ymd") {
datesRegex = /^(\d\d\d\d)[./-](\d\d?)[./-](\d\d?)/;
}
columnOfTd = tr.querySelectorAll("td").item(columnIndex).textContent;
let match = columnOfTd.match(regexDate);
let match = columnOfTd.match(datesRegex);
let [years, days, months] = [0, 0, 0];
let numberToSort = columnOfTd;
if (match) {
const regexFirstNumber = match[1];
const regexSecondNumber = match[2];
const regexYears = match[3];
if (regexFirstNumber && regexSecondNumber) {
if (dateFormat === "mdy") {
days = regexSecondNumber;
months = regexFirstNumber;
const [regPos1, regPos2, regPos3] = [match[1], match[2], match[3]];
if (regPos1 && regPos2 && regPos3) {
if (datesFormat === "mdy") {
[months, days, years] = [regPos1, regPos2, regPos3];
} else if (datesFormat === "ymd") {
[years, months, days] = [regPos1, regPos2, regPos3];
} else {
days = regexFirstNumber;
months = regexSecondNumber;
[days, months, years] = [regPos1, regPos2, regPos3];
}
}
if (regexYears) {
years = regexYears;
}
numberToSort = Number(
years +
String(months).padStart(2, "0") +
Expand Down Expand Up @@ -298,6 +300,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
isTimeSort,
isSortDateDayMonthYear,
isSortDateMonthDayYear,
isSortDateYearMonthDay,
isDataAttribute,
colSpanData,
colSpanSum,
Expand All @@ -323,6 +326,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
!isDataAttribute &&
!isTimeSort &&
!isSortDateDayMonthYear &&
!isSortDateYearMonthDay &&
!isSortDateMonthDayYear
) {
columnData.push(`${tdTextContent}#${i}`);
Expand Down Expand Up @@ -467,9 +471,12 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {

const isSortDateDayMonthYear = th.classList.contains("dates-dmy-sort");
const isSortDateMonthDayYear = th.classList.contains("dates-mdy-sort");
const isSortDateYearMonthDay = th.classList.contains("dates-ymd-sort");
// pick mdy first to override the inferred default class which is dmy.
if (isSortDateMonthDayYear) {
sortDates("mdy", visibleTableRows, columnData);
} else if (isSortDateYearMonthDay) {
sortDates("ymd", visibleTableRows, columnData);
} else if (isSortDateDayMonthYear) {
sortDates("dmy", visibleTableRows, columnData);
}
Expand All @@ -488,6 +495,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
isFileSize,
isSortDateDayMonthYear,
isSortDateMonthDayYear,
isSortDateYearMonthDay,
isDataAttribute,
isTimeSort,
colSpanData,
Expand Down

0 comments on commit d5a32cd

Please sign in to comment.