table-sort-js
Advanced tools
Comparing version 1.8.1 to 1.8.2
{ | ||
"name": "table-sort-js", | ||
"version": "1.8.1", | ||
"version": "1.8.2", | ||
"description": "A JavaScript client-side HTML table sorting library with no dependencies required.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -70,6 +70,6 @@ /* | ||
function makeEachColumnSortable(th, columnIndex, tableBody, sortableTable) { | ||
let desc = th.classList.contains("order-by-desc"); | ||
const desc = th.classList.contains("order-by-desc"); | ||
let tableArrows = sortableTable.classList.contains("table-arrows"); | ||
const arrowUp = " ▲"; | ||
const arrowDown = " ▼"; | ||
const [arrowUp, arrowDown] = [" ▲", " ▼"]; | ||
const fillValue = "!X!Y!Z!"; | ||
@@ -92,3 +92,2 @@ if (desc && tableArrows) { | ||
function sortFileSize(tableRows, columnData) { | ||
// Handle filesize sorting (e.g KB, MB, GB, TB) - Turns data into KiB. | ||
const numberWithUnitType = | ||
@@ -109,3 +108,3 @@ /[.0-9]+(\s?B|\s?KB|\s?KiB|\s?MB|\s?MiB|\s?GB|\s?GiB|T\s?B|\s?TiB)/i; | ||
}; | ||
function removeUnitTypeConvertToBytes(fileSizeTd, _replace) { | ||
function removeUnitTypeConvertToBytes(fileSizeTd, _replace, i) { | ||
fileSizeTd = fileSizeTd.replace(unitType, ""); | ||
@@ -116,2 +115,5 @@ fileSizeTd = fileSizeTd.replace( | ||
); | ||
if (i) { | ||
columnData.push(`${fileSizeTd}#${i}`); | ||
} | ||
return fileSizeTd; | ||
@@ -125,25 +127,19 @@ } | ||
if (fileSizeTd.match(/\s?KB/i)) { | ||
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Kilobyte"); | ||
columnData.push(`${fileSizeTd}#${i}`); | ||
removeUnitTypeConvertToBytes(fileSizeTd, "Kilobyte", i); | ||
} else if (fileSizeTd.match(/\s?KiB/i)) { | ||
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Kibibyte"); | ||
columnData.push(`${fileSizeTd}#${i}`); | ||
removeUnitTypeConvertToBytes(fileSizeTd, "Kibibyte", i); | ||
} else if (fileSizeTd.match(/\s?MB/i)) { | ||
// TODO: figure out why refactoring this line breaks test. | ||
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Megabyte"); | ||
columnData.push(`${fileSizeTd}#${i}`); | ||
} else if (fileSizeTd.match(/\s?MiB/i)) { | ||
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Mebibyte"); | ||
columnData.push(`${fileSizeTd}#${i}`); | ||
removeUnitTypeConvertToBytes(fileSizeTd, "Mebibyte", i); | ||
} else if (fileSizeTd.match(/\s?GB/i)) { | ||
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Gigabyte"); | ||
columnData.push(`${fileSizeTd}#${i}`); | ||
removeUnitTypeConvertToBytes(fileSizeTd, "Gigabyte", i); | ||
} else if (fileSizeTd.match(/\s?GiB/i)) { | ||
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Gibibyte"); | ||
columnData.push(`${fileSizeTd}#${i}`); | ||
removeUnitTypeConvertToBytes(fileSizeTd, "Gibibyte", i); | ||
} else if (fileSizeTd.match(/\s?TB/i)) { | ||
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Terabyte"); | ||
columnData.push(`${fileSizeTd}#${i}`); | ||
removeUnitTypeConvertToBytes(fileSizeTd, "Terabyte", i); | ||
} else if (fileSizeTd.match(/\s?TiB/i)) { | ||
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Tebibyte"); | ||
columnData.push(`${fileSizeTd}#${i}`); | ||
removeUnitTypeConvertToBytes(fileSizeTd, "Tebibyte", i); | ||
} else if (fileSizeTd.match(/\s?B/i)) { | ||
@@ -154,3 +150,3 @@ fileSizeTd = fileSizeTd.replace(unitType, ""); | ||
} else { | ||
columnData.push(`!X!Y!Z!#${i}`); | ||
columnData.push(`${fillValue}#${i}`); | ||
} | ||
@@ -160,8 +156,6 @@ } | ||
let timesClickedColumn = 0; | ||
let columnIndexesClicked = []; | ||
let [timesClickedColumn, columnIndexesClicked] = [0, []]; | ||
function rememberSort(timesClickedColumn, columnIndexesClicked) { | ||
// Check if user has clicked different column from the first column if | ||
// yes reset times clicked. | ||
// if user clicked different column from first column reset times clicked. | ||
columnIndexesClicked.push(columnIndex); | ||
@@ -188,12 +182,12 @@ if (timesClickedColumn === 1 && columnIndexesClicked.length > 1) { | ||
function getTableData( | ||
tableRows, | ||
columnData, | ||
isFileSize, | ||
isDataAttribute, | ||
colSpanData, | ||
colSpanSum | ||
) { | ||
function getTableData(tableProperties) { | ||
const { | ||
tableRows, | ||
columnData, | ||
isFileSize, | ||
isDataAttribute, | ||
colSpanData, | ||
colSpanSum, | ||
} = tableProperties; | ||
for (let [i, tr] of tableRows.entries()) { | ||
// inner text for column we click on | ||
let tdTextContent = tr | ||
@@ -206,3 +200,2 @@ .querySelectorAll("td") | ||
).textContent; | ||
if (tdTextContent.length === 0) { | ||
@@ -221,11 +214,11 @@ tdTextContent = ""; | ||
// Fill in blank table cells dict key with filler value. | ||
columnData.push(`!X!Y!Z!#${i}`); | ||
columnIndexAndTableRow[`!X!Y!Z!#${i}`] = tr.innerHTML; | ||
columnData.push(`${fillValue}#${i}`); | ||
columnIndexAndTableRow[`${fillValue}#${i}`] = tr.innerHTML; | ||
} | ||
} | ||
function naturalSortAescending(a, b) { | ||
if (a.includes("X!Y!Z!#")) { | ||
function naturalSortAscending(a, b) { | ||
if (a.includes(`${fillValue}#`)) { | ||
return 1; | ||
} else if (b.includes("X!Y!Z!#")) { | ||
} else if (b.includes(`${fillValue}#`)) { | ||
return -1; | ||
@@ -242,3 +235,3 @@ } else { | ||
function naturalSortDescending(a, b) { | ||
return naturalSortAescending(b, a); | ||
return naturalSortAscending(b, a); | ||
} | ||
@@ -251,4 +244,2 @@ | ||
// Sort naturally; default aescending unless th contains 'order-by-desc' | ||
// as className. | ||
if (columnData[0] === undefined) { | ||
@@ -258,18 +249,23 @@ return; | ||
function changeTableArrow(arrowDirection) { | ||
if (tableArrows) { | ||
clearArrows(arrowUp, arrowDown); | ||
th.insertAdjacentText("beforeend", arrowDirection); | ||
} | ||
} | ||
function sortColumn(sortDirection) { | ||
columnData.sort(sortDirection, { | ||
numeric: true, | ||
ignorePunctuation: true, | ||
}); | ||
} | ||
if (timesClickedColumn === 1) { | ||
if (desc) { | ||
if (tableArrows) { | ||
clearArrows(arrowUp, arrowDown); | ||
th.insertAdjacentText("beforeend", arrowDown); | ||
} | ||
columnData.sort(naturalSortDescending, { | ||
numeric: true, | ||
ignorePunctuation: true, | ||
}); | ||
changeTableArrow(arrowDown); | ||
sortColumn(naturalSortDescending); | ||
} else { | ||
if (tableArrows) { | ||
clearArrows(arrowUp, arrowDown); | ||
th.insertAdjacentText("beforeend", arrowUp); | ||
} | ||
columnData.sort(naturalSortAescending); | ||
changeTableArrow(arrowUp); | ||
sortColumn(naturalSortAscending); | ||
} | ||
@@ -279,16 +275,7 @@ } else if (timesClickedColumn === 2) { | ||
if (desc) { | ||
if (tableArrows) { | ||
clearArrows(arrowUp, arrowDown); | ||
th.insertAdjacentText("beforeend", arrowUp); | ||
} | ||
columnData.sort(naturalSortAescending, { | ||
numeric: true, | ||
ignorePunctuation: true, | ||
}); | ||
changeTableArrow(arrowUp); | ||
sortColumn(naturalSortAscending); | ||
} else { | ||
if (tableArrows) { | ||
clearArrows(arrowUp, arrowDown); | ||
th.insertAdjacentText("beforeend", arrowDown); | ||
} | ||
columnData.sort(naturalSortDescending); | ||
changeTableArrow(arrowDown); | ||
sortColumn(naturalSortDescending); | ||
} | ||
@@ -298,3 +285,4 @@ } | ||
function updateTable(tableRows, columnData, isFileSize) { | ||
function updateTable(tableProperties) { | ||
const { tableRows, columnData, isFileSize } = tableProperties; | ||
for (let [i, tr] of tableRows.entries()) { | ||
@@ -306,3 +294,3 @@ if (isFileSize) { | ||
.item(columnIndex).innerHTML; | ||
let fileSizeInBytesText = tr | ||
const fileSizeInBytesText = tr | ||
.querySelectorAll("td") | ||
@@ -319,38 +307,39 @@ .item(columnIndex).textContent; | ||
columnData[i] = columnData[i].replace(/#[0-9]*/, ""); | ||
if (columnData[i] < fileSizes.Kibibyte) { | ||
const fileSize = columnData[i]; | ||
if (fileSize < fileSizes.Kibibyte) { | ||
fileSizeInBytesHTML = fileSizeInBytesHTML.replace( | ||
fileSizeInBytesText, | ||
`${parseFloat(columnData[i]).toFixed(2)} B` | ||
`${parseFloat(fileSize).toFixed(2)} B` | ||
); | ||
} else if ( | ||
columnData[i] >= fileSizes.Kibibyte && | ||
columnData[i] < fileSizes.Mebibyte | ||
fileSize >= fileSizes.Kibibyte && | ||
fileSize < fileSizes.Mebibyte | ||
) { | ||
fileSizeInBytesHTML = fileSizeInBytesHTML.replace( | ||
fileSizeInBytesText, | ||
`${(columnData[i] / fileSizes.Kibibyte).toFixed(2)} KiB` | ||
`${(fileSize / fileSizes.Kibibyte).toFixed(2)} KiB` | ||
); | ||
} else if ( | ||
columnData[i] >= fileSizes.Mebibyte && | ||
columnData[i] < fileSizes.Gibibyte | ||
fileSize >= fileSizes.Mebibyte && | ||
fileSize < fileSizes.Gibibyte | ||
) { | ||
fileSizeInBytesHTML = fileSizeInBytesHTML.replace( | ||
fileSizeInBytesText, | ||
`${(columnData[i] / fileSizes.Mebibyte).toFixed(2)} MiB` | ||
`${(fileSize / fileSizes.Mebibyte).toFixed(2)} MiB` | ||
); | ||
} else if ( | ||
columnData[i] >= fileSizes.Gibibyte && | ||
columnData[i] < fileSizes.Tebibyte | ||
fileSize >= fileSizes.Gibibyte && | ||
fileSize < fileSizes.Tebibyte | ||
) { | ||
fileSizeInBytesHTML = fileSizeInBytesHTML.replace( | ||
fileSizeInBytesText, | ||
`${(columnData[i] / fileSizes.Gibibyte).toFixed(2)} GiB` | ||
`${(fileSize / fileSizes.Gibibyte).toFixed(2)} GiB` | ||
); | ||
} else if ( | ||
columnData[i] >= fileSizes.Tebibyte && | ||
columnData[i] < fileSizes.Pebibyte | ||
fileSize >= fileSizes.Tebibyte && | ||
fileSize < fileSizes.Pebibyte | ||
) { | ||
fileSizeInBytesHTML = fileSizeInBytesHTML.replace( | ||
fileSizeInBytesText, | ||
`${(columnData[i] / fileSizes.Tebibyte).toFixed(2)} TiB` | ||
`${(fileSize / fileSizes.Tebibyte).toFixed(2)} TiB` | ||
); | ||
@@ -372,5 +361,3 @@ } else { | ||
th.addEventListener("click", function () { | ||
const columnData = []; | ||
const colSpanData = {}; | ||
const colSpanSum = {}; | ||
const [columnData, colSpanData, colSpanSum] = [[], {}, {}]; | ||
@@ -384,3 +371,3 @@ const visibleTableRows = Array.prototype.filter.call( | ||
let isDataAttribute = th.classList.contains("data-sort"); | ||
const isDataAttribute = th.classList.contains("data-sort"); | ||
if (isDataAttribute) { | ||
@@ -390,3 +377,3 @@ sortDataAttributes(visibleTableRows, columnData); | ||
let isFileSize = th.classList.contains("file-size"); | ||
const isFileSize = th.classList.contains("file-size"); | ||
if (isFileSize) { | ||
@@ -396,3 +383,3 @@ sortFileSize(visibleTableRows, columnData); | ||
let isRememberSort = sortableTable.classList.contains("remember-sort"); | ||
const isRememberSort = sortableTable.classList.contains("remember-sort"); | ||
if (!isRememberSort) { | ||
@@ -405,5 +392,4 @@ rememberSort(timesClickedColumn, columnIndexesClicked); | ||
getColSpanData(sortableTable, colSpanData, colSpanSum); | ||
// TODO: refactor function to take object. | ||
getTableData( | ||
visibleTableRows, | ||
const tableProperties = { | ||
tableRows: visibleTableRows, | ||
columnData, | ||
@@ -413,5 +399,6 @@ isFileSize, | ||
colSpanData, | ||
colSpanSum | ||
); | ||
updateTable(visibleTableRows, columnData, isFileSize); | ||
colSpanSum, | ||
}; | ||
getTableData(tableProperties); | ||
updateTable(tableProperties); | ||
}); | ||
@@ -418,0 +405,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
19697
365