table-sort-js
Advanced tools
Comparing version 1.18.0 to 1.18.1
{ | ||
"name": "table-sort-js", | ||
"version": "1.18.0", | ||
"version": "1.18.1", | ||
"description": "A JavaScript client-side HTML table sorting library with no dependencies required.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -64,8 +64,8 @@ ![npm version](https://img.shields.io/npm/v/table-sort-js) | ||
| <th> classes | Description | | ||
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------- | | ||
| "data-sort" | Sort by [data attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes), e.g <td data-sort="42"> | | ||
| "dates-mdy-sort" | Sorts dates in US style mm/dd/yyyy format;. e.g (12/28/2023). Can use "/" or "-" as separator. Overides inferred "dates-dmy-sort" class. | | ||
| "onload-sort" | Sort column on loading of the page. Simulates a click from the user. (can only sort onload for one column) | | ||
| "disable-sort" | Disallow sorting the table by this specific column. | | ||
| <th> classes | Description | | ||
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| "data-sort" | Sort by [data attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes), e.g <td data-sort="42">. Useful for doing custom sorts. | | ||
| "dates-mdy-sort" | Sorts dates in US style mm/dd/yyyy format;. e.g (12/28/2023). Can use "/" or "-" as separator. Overides inferred "dates-dmy-sort" class. | | ||
| "onload-sort" | Sort column on loading of the page. Simulates a click from the user. (can only sort onload for one column) | | ||
| "disable-sort" | Disallow sorting the table by this specific column. | | ||
@@ -72,0 +72,0 @@ <br> |
@@ -58,53 +58,57 @@ /* | ||
function inferSortClasses(tableRows, columnIndex, column, 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; | ||
// Don'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 numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas | ||
const numericRegex = | ||
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/; | ||
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 }, | ||
numericRegex: { regexp: numericRegex, class: "numeric-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( | ||
column.span[columnIndex] === 1 | ||
? column.spanSum[columnIndex] - 1 | ||
: column.spanSum[columnIndex] - column.span[columnIndex] | ||
); | ||
let foundMatch = false; | ||
for (let key of Object.keys(inferableClasses)) { | ||
let classRegexp = inferableClasses[key].regexp; | ||
if (tableColumn?.innerText !== undefined) { | ||
if (tableColumn.innerText.match(classRegexp)) { | ||
foundMatch = true; | ||
inferableClasses[key].count++; | ||
try { | ||
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; | ||
// Don'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 numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas | ||
const numericRegex = | ||
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/; | ||
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 }, | ||
numericRegex: { regexp: numericRegex, class: "numeric-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( | ||
column.span[columnIndex] === 1 | ||
? column.spanSum[columnIndex] - 1 | ||
: column.spanSum[columnIndex] - column.span[columnIndex] | ||
); | ||
let foundMatch = false; | ||
for (let key of Object.keys(inferableClasses)) { | ||
let classRegexp = inferableClasses[key].regexp; | ||
if (tableColumn.innerText !== undefined) { | ||
if (tableColumn.innerText.match(classRegexp)) { | ||
foundMatch = true; | ||
inferableClasses[key].count++; | ||
} | ||
} | ||
if (inferableClasses[key].count >= threshold) { | ||
th.classList.add(inferableClasses[key].class); | ||
classNameAdded = true; | ||
break; | ||
} | ||
} | ||
if (inferableClasses[key].count >= threshold) { | ||
th.classList.add(inferableClasses[key].class); | ||
classNameAdded = true; | ||
if (classNameAdded) { | ||
break; | ||
} | ||
if (!foundMatch) { | ||
regexNotFoundCount++; | ||
continue; | ||
} | ||
} | ||
if (classNameAdded) { | ||
break; | ||
} | ||
if (!foundMatch) { | ||
regexNotFoundCount++; | ||
continue; | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
@@ -111,0 +115,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
29850
580