table-sort-js
Advanced tools
Comparing version 1.11.2 to 1.12.0
{ | ||
"name": "table-sort-js", | ||
"version": "1.11.2", | ||
"version": "1.12.0", | ||
"description": "A JavaScript client-side HTML table sorting library with no dependencies required.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -44,7 +44,8 @@ ![table-sort-js](https://img.shields.io/npm/v/table-sort-js) | ||
| <table> classes | Description | | ||
| --------------------- | ----------------------------------------------------------------------- | | ||
| "table-sort" | Make the table sortable! (Words, numbers, dates) | | ||
| "table-arrows" | Display ascending or descending triangles. | | ||
| "remember-sort" | If clicking on different columns remembers sort of the original column. | | ||
| <table> classes | Description | | ||
| --------------------- | -------------------------------------------------------------------------------------------- | | ||
| "table-sort" | Make the table sortable! (Words, numbers)... | | ||
| "table-arrows" | Display ascending or descending triangles. | | ||
| "no-class-infer" | Turns off inference for adding sort classes automatically (file-size-sort and runtime-sort). | | ||
| "remember-sort" | If clicking on different columns remembers sort of the original column. | | ||
@@ -51,0 +52,0 @@ | <th> classes | Description | |
@@ -62,2 +62,49 @@ /* | ||
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; | ||
let runtimeSortCounter = 0, | ||
fileSizeSortCounter = 0; | ||
let tableColumnLength = th.parentElement.childElementCount; | ||
for (let tr of tableRows) { | ||
let runtimeSortMatch, fileSizeSortMatch; | ||
const tableColumn = tr.querySelectorAll("td").item(columnIndex); | ||
if (tableColumn.innerText) { | ||
runtimeSortMatch = tableColumn.innerText.match( | ||
regexMinutesAndSeconds | ||
); | ||
fileSizeSortMatch = tableColumn.innerText.match(regexFileSizeSort); | ||
} | ||
if (runtimeSortMatch) { | ||
runtimeSortCounter++; | ||
} | ||
if (fileSizeSortMatch) { | ||
fileSizeSortCounter++; | ||
} | ||
} | ||
// TODO: refactor this into one function called addInferredClasses that loops over sort classes and counters | ||
addInferredClass( | ||
th, | ||
tableColumnLength, | ||
runtimeSortCounter, | ||
"runtime-sort" | ||
); | ||
addInferredClass( | ||
th, | ||
tableColumnLength, | ||
fileSizeSortCounter, | ||
"file-size-sort" | ||
); | ||
} | ||
} | ||
function makeTableSortable(sortableTable) { | ||
@@ -67,3 +114,10 @@ const tableBody = getTableBody(sortableTable); | ||
const tableHeadHeaders = tableHead.querySelectorAll("th"); | ||
const tableRows = tableBody.querySelectorAll("tr"); | ||
const isNoSortClassInference = | ||
sortableTable.classList.contains("no-class-infer"); | ||
if (!isNoSortClassInference) { | ||
inferSortClasses(tableRows, tableHeadHeaders); | ||
} | ||
for (let [columnIndex, th] of tableHeadHeaders.entries()) { | ||
@@ -129,3 +183,3 @@ if (!th.classList.contains("disable-sort")) { | ||
for (let [i, tr] of tableRows.entries()) { | ||
const regexMinutesAndSeconds = /^(\d+m)\s?(\d+s)$/i; | ||
const regexMinutesAndSeconds = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i; | ||
let columnOfTd = tr | ||
@@ -135,15 +189,17 @@ .querySelectorAll("td") | ||
let match = columnOfTd.match(regexMinutesAndSeconds); | ||
let minutesInSeconds, | ||
seconds, | ||
timeinSeconds = [0, 0, 0]; | ||
let [minutesInSeconds, hours, seconds, timeinSeconds] = [0, 0, 0, 0]; | ||
if (match) { | ||
const regexMinutes = match[1]; | ||
const regexHours = match[1]; | ||
if (regexHours) { | ||
hours = Number(regexHours.replace("h", "")) * 60 * 60; | ||
} | ||
const regexMinutes = match[2]; | ||
if (regexMinutes) { | ||
minutesInSeconds = Number(regexMinutes.replace("m", "")) * 60; | ||
} | ||
const regexSeconds = match[2]; | ||
const regexSeconds = match[3]; | ||
if (regexSeconds) { | ||
seconds = Number(regexSeconds.replace("s", "")); | ||
} | ||
timeinSeconds = minutesInSeconds + seconds; | ||
timeinSeconds = hours + minutesInSeconds + seconds; | ||
} | ||
@@ -150,0 +206,0 @@ columnData.push(`${timeinSeconds}#${i}`); |
21672
398
65