table-sort-js
Advanced tools
Comparing version 1.14.0 to 1.15.0
{ | ||
"name": "table-sort-js", | ||
"version": "1.14.0", | ||
"version": "1.15.0", | ||
"description": "A JavaScript client-side HTML table sorting library with no dependencies required.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -9,3 +9,3 @@ ![table-sort-js](https://img.shields.io/npm/v/table-sort-js) | ||
- Description: A JavaScript client-side HTML table sorting library with no dependencies required. | ||
- Description: HTML table sorting library with sort type inference builtin and browser extension available. [#VanillaJS](http://vanilla-js.com/) | ||
@@ -16,3 +16,3 @@ - [Demo](https://leewannacott.github.io/Portfolio/#/GitHub) | ||
- [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/): Tables of any website you visit become sortable! | ||
@@ -65,3 +65,3 @@ ## Install instructions. | ||
| "dates-ymd-sort" | Sorts dates in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) yyyy/mm/dd format. e.g (2021/10/28). Use "/" or "-" as separator. | | ||
| "file-size-sort" | Sorts file sizes(B->TiB) uses the binary prefix. (e.g KiB). Input data ideally in Bytes e.g (10b or 10B) | | ||
| "file-size-sort" | Sorts file sizes(B->TiB) uses the binary prefix. (e.g 10 B, 100 KiB, 1 MiB); optional space between number and prefix. | | ||
| "runtime-sort" | Sorts runtime in hours minutes and seconds e.g (10h 1m 20s). Useful for sorting the GitHub actions Run time column... | | ||
@@ -68,0 +68,0 @@ |
@@ -62,60 +62,44 @@ /* | ||
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); | ||
function inferSortClasses(tableRows, columnIndex, th) { | ||
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 dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/; | ||
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/; | ||
const inferableClasses = { | ||
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; | ||
const threshold = Math.ceil(tableRows.length / 2); | ||
for (let tr of tableRows) { | ||
if (regexNotFoundCount >= threshold) { | ||
break; | ||
} | ||
const tableColumn = tr.querySelectorAll("td").item(columnIndex); | ||
let foundMatch = false; | ||
for (let key of Object.keys(inferableClasses)) { | ||
let classRegexp = inferableClasses[key].regexp; | ||
if (tableColumn.innerText) { | ||
runtimeSortMatch = tableColumn.innerText.match( | ||
regexMinutesAndSeconds | ||
); | ||
fileSizeSortMatch = tableColumn.innerText.match(regexFileSizeSort); | ||
datesMatch = tableColumn.innerText.match(datesRegex); | ||
isoDatesMatch = tableColumn.innerText.match(regexISODates); | ||
if (tableColumn.innerText.match(classRegexp) !== null) { | ||
foundMatch = true; | ||
inferableClasses[key].count++; | ||
} | ||
} | ||
if (runtimeSortMatch) { | ||
runtimeCounter++; | ||
if (inferableClasses[key].count >= threshold) { | ||
th.classList.add(inferableClasses[key].class); | ||
classNameAdded = true; | ||
break; | ||
} | ||
if (fileSizeSortMatch) { | ||
fileSizeCounter++; | ||
} | ||
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, | ||
fileSizeCounter, | ||
"file-size-sort" | ||
); | ||
addInferredClass(th, tableColumnLength, datesCounter, "dates-dmy-sort"); | ||
addInferredClass( | ||
th, | ||
tableColumnLength, | ||
isoDatesCounter, | ||
"dates-ymd-sort" | ||
); | ||
if (classNameAdded) { | ||
break; | ||
} | ||
if (!foundMatch) { | ||
regexNotFoundCount++; | ||
continue; | ||
} | ||
} | ||
@@ -132,5 +116,2 @@ } | ||
sortableTable.classList.contains("no-class-infer"); | ||
if (!isNoSortClassInference) { | ||
inferSortClasses(tableRows, tableHeadHeaders); | ||
} | ||
@@ -140,2 +121,5 @@ for (let [columnIndex, th] of tableHeadHeaders.entries()) { | ||
th.style.cursor = "pointer"; | ||
if (!isNoSortClassInference) { | ||
inferSortClasses(tableRows, columnIndex, th); | ||
} | ||
makeEachColumnSortable(th, columnIndex, tableBody, sortableTable); | ||
@@ -510,4 +494,4 @@ } | ||
}); | ||
let isOnloadSort = th.classList.contains("onload-sort"); | ||
if (isOnloadSort) { | ||
if (th.classList.contains("onload-sort")) { | ||
th.click(); | ||
@@ -514,0 +498,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26257
470