Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

table-sort-js

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

table-sort-js - npm Package Compare versions

Comparing version 1.5.8 to 1.6.8

2

package.json
{
"name": "table-sort-js",
"version": "1.5.8",
"version": "1.6.8",
"description": "A JavaScript client-side HTML table sorting library with no dependencies required.",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -18,5 +18,5 @@ /*

function tableSortJs(test = false, domDocumentWindow = document) {
function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
function getHTMLTables() {
if (test === true) {
if (testingTableSortJS === true) {
const getTagTable = domDocumentWindow.getElementsByTagName("table");

@@ -29,2 +29,3 @@ return [getTagTable];

}
const [getTagTable] = getHTMLTables();

@@ -43,3 +44,3 @@ const columnIndexAndTableRow = {};

if (sortableTable.getElementsByTagName("thead").length === 0) {
if (test === true) {
if (testingTableSortJS === true) {
createTableHead = domDocumentWindow.createElement("thead");

@@ -64,76 +65,209 @@ } else {

let columnIndexesClicked = [];
for (let [columnIndex, th] of tableHeadHeaders.entries()) {
let timesClickedColumn = 0;
let desc = th.classList.contains("order-by-desc");
let tableArrows = sortableTable.classList.contains("table-arrows");
let arrowUp = " ▲";
let arrowDown = " ▼";
makeEachColumnSortable(th, columnIndex, tableBody, sortableTable);
}
}
if (desc && tableArrows) {
th.insertAdjacentText("beforeend", arrowDown);
} else if (tableArrows){
th.insertAdjacentText("beforeend", arrowUp);
function makeEachColumnSortable(th, columnIndex, tableBody, sortableTable) {
let desc = th.classList.contains("order-by-desc");
let tableArrows = sortableTable.classList.contains("table-arrows");
const arrowUp = " ▲";
const arrowDown = " ▼";
if (desc && tableArrows) {
th.insertAdjacentText("beforeend", arrowDown);
} else if (tableArrows) {
th.insertAdjacentText("beforeend", arrowUp);
}
function sortDataAttributes(tableRows, columnData) {
for (let [i, tr] of tableRows.entries()) {
const dataAttributeTd = tr.querySelectorAll("td").item(columnIndex)
.dataset.sort;
columnData.push(`${dataAttributeTd}#${i}`);
columnIndexAndTableRow[columnData[i]] = tr.innerHTML;
}
}
th.addEventListener("click", function () {
const tableRows = tableBody.querySelectorAll("tr");
const columnData = [];
function sortFileSize(tableRows, columnData) {
const numberWithUnitType =
/[.0-9]+(\s?B|\s?KB|\s?KiB|\s?MB|\s?MiB|\s?GB|\s?GiB|T\s?B|\s?TiB)/i;
const unitType =
/(\s?B|\s?KB|\s?KiB|\s?MB|\s?MiB|\s?GB|G\s?iB|\s?TB|\s?TiB)/i;
const fileSizes = {
Kibibyte: 1024,
Mebibyte: 1.049e6,
Gibibyte: 1.074e9,
Tebibyte: 1.1e12,
Pebibyte: 1.126e15,
Kilobyte: 1000,
Megabyte: 1e6,
Gigabyte: 1e9,
Terabyte: 1e12,
};
function removeUnitTypeConvertToBytes(fileSizeTd, _replace) {
fileSizeTd = fileSizeTd.replace(unitType, "");
fileSizeTd = fileSizeTd.replace(
fileSizeTd,
fileSizeTd * fileSizes[_replace]
);
return fileSizeTd;
}
for (let [i, tr] of tableRows.entries()) {
let fileSizeTd = tr
.querySelectorAll("td")
.item(columnIndex).textContent;
if (fileSizeTd.match(numberWithUnitType)) {
if (fileSizeTd.match(/\s?KB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Kilobyte");
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?KiB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Kibibyte");
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?MB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Megabyte");
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?MiB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Mebibyte");
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?GB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Gigabyte");
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?GiB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Gibibyte");
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?TB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Terabyte");
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?TiB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(fileSizeTd, "Tebibyte");
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?B/i)) {
fileSizeTd = fileSizeTd.replace(unitType, "");
columnData.push(`${fileSizeTd}#${i}`);
}
} else {
columnData.push(`!X!Y!Z!#${i}`);
}
}
}
let isDataAttribute = th.classList.contains("data-sort");
if (isDataAttribute) {
for (let [i, tr] of tableRows.entries()) {
const dataAttributeTd = tr.querySelectorAll("td").item(columnIndex)
.dataset.sort;
columnData.push(`${dataAttributeTd}#${i}`);
columnIndexAndTableRow[columnData[i]] = tr.innerHTML;
let timesClickedColumn = 0;
let columnIndexesClicked = [];
function rememberSort(timesClickedColumn, columnIndexesClicked) {
columnIndexesClicked.push(columnIndex);
if (timesClickedColumn === 1 && columnIndexesClicked.length > 1) {
const lastColumnClicked =
columnIndexesClicked[columnIndexesClicked.length - 1];
const secondLastColumnClicked =
columnIndexesClicked[columnIndexesClicked.length - 2];
if (lastColumnClicked !== secondLastColumnClicked) {
timesClickedColumn = 0;
columnIndexesClicked.shift();
}
}
}
function getTableData(tableRows, columnData, isFileSize, isDataAttribute) {
for (let [i, tr] of tableRows.entries()) {
// inner text for column we click on
let tdTextContent = tr
.querySelectorAll("td")
.item(columnIndex).textContent;
if (tdTextContent.length === 0) {
tdTextContent = "";
}
if (tdTextContent.trim() !== "") {
if (isFileSize) {
fileSizeColumnTextAndRow[columnData[i]] = tr.innerHTML;
}
if (!isFileSize && !isDataAttribute) {
columnData.push(`${tdTextContent}#${i}`);
columnIndexAndTableRow[`${tdTextContent}#${i}`] = tr.innerHTML;
}
} else {
// Fill in blank table cells dict key with filler value.
columnData.push(`!X!Y!Z!#${i}`);
columnIndexAndTableRow[`!X!Y!Z!#${i}`] = tr.innerHTML;
}
}
let isDayOfWeek = th.classList.contains("days-of-week");
if (isDayOfWeek) {
const day =
/(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday|Mon|Tue|Wed|Thur|Fri|Sat|Sun)/i;
const dayOfWeek = {
Monday: 1,
Tuesday: 2,
Wednesday: 3,
Thursday: 4,
Friday: 5,
Saturday: 6,
Sunday: 7,
};
for (let [i, tr] of tableRows.entries()) {
const dayOfWeekTd = tr
.querySelectorAll("td")
.item(columnIndex).textContent;
if (dayOfWeekTd.match(day)) {
if (dayOfWeekTd.match(/Monday|Mon/i)) {
columnData.push(`${dayOfWeek.Monday}#${i}`);
} else if (dayOfWeekTd.match(/Tuesday|Tue/i)) {
columnData.push(`${dayOfWeek.Tuesday}#${i}`);
} else if (dayOfWeekTd.match(/Wednesday|Wed/i)) {
columnData.push(`${dayOfWeek.Wednesday}#${i}`);
} else if (dayOfWeekTd.match(/Thursday|Thur/i)) {
columnData.push(`${dayOfWeek.Thursday}#${i}`);
} else if (dayOfWeekTd.match(/Friday|Fri/i)) {
columnData.push(`${dayOfWeek.Friday}#${i}`);
} else if (dayOfWeekTd.match(/Saturday|Sat/i)) {
columnData.push(`${dayOfWeek.Saturday}#${i}`);
} else if (dayOfWeekTd.match(/Sunday|Sun/i)) {
columnData.push(`${dayOfWeek.Sunday}#${i}`);
}
} else {
columnData.push(`!X!Y!Z!#${i}`);
}
function naturalSortAescending(a, b) {
if (a.includes("X!Y!Z!#")) {
return 1;
} else if (b.includes("X!Y!Z!#")) {
return -1;
} else {
return a.localeCompare(
b,
navigator.languages[0] || navigator.language,
{ numeric: true, ignorePunctuation: true }
);
}
}
function naturalSortDescending(a, b) {
return naturalSortAescending(b, a);
}
function clearArrows(arrowUp = "▲", arrowDown = "▼") {
th.innerText = th.innerText.replace(arrowUp, "");
th.innerText = th.innerText.replace(arrowDown, "");
}
// Sort naturally; default aescending unless th contains 'order-by-desc'
// as className.
if (columnData[0] === undefined) {
return;
}
if (timesClickedColumn === 1) {
if (desc) {
if (tableArrows) {
clearArrows(arrowUp, arrowDown);
th.insertAdjacentText("beforeend", arrowDown);
}
columnData.sort(naturalSortDescending, {
numeric: true,
ignorePunctuation: true,
});
} else {
if (tableArrows) {
clearArrows(arrowUp, arrowDown);
th.insertAdjacentText("beforeend", arrowUp);
}
columnData.sort(naturalSortAescending);
}
} else if (timesClickedColumn === 2) {
timesClickedColumn = 0;
if (desc) {
if (tableArrows) {
clearArrows(arrowUp, arrowDown);
th.insertAdjacentText("beforeend", arrowUp);
}
columnData.sort(naturalSortAescending, {
numeric: true,
ignorePunctuation: true,
});
} else {
if (tableArrows) {
clearArrows(arrowUp, arrowDown);
th.insertAdjacentText("beforeend", arrowDown);
}
columnData.sort(naturalSortDescending);
}
}
}
// Handle filesize sorting (e.g KB, MB, GB, TB) - Turns data into KiB.
let isFileSize = th.classList.contains("file-size");
function updateTable(tableRows, columnData, isFileSize) {
for (let [i, tr] of tableRows.entries()) {
if (isFileSize) {
const numberWithUnitType =
/[.0-9]+(\s?B|\s?KB|\s?KiB|\s?MB|\s?MiB|\s?GB|\s?GiB|T\s?B|\s?TiB)/i;
const unitType =
/(\s?B|\s?KB|\s?KiB|\s?MB|\s?MiB|\s?GB|G\s?iB|\s?TB|\s?TiB)/i;
tr.innerHTML = fileSizeColumnTextAndRow[columnData[i]];
let fileSizeInBytesHTML = tr
.querySelectorAll("td")
.item(columnIndex).innerHTML;
let fileSizeInBytesText = tr
.querySelectorAll("td")
.item(columnIndex).textContent;
const fileSizes = {

@@ -145,265 +279,85 @@ Kibibyte: 1024,

Pebibyte: 1.126e15,
Kilobyte: 1000,
Megabyte: 1e6,
Gigabyte: 1e9,
Terabyte: 1e12,
};
function removeUnitTypeConvertToBytes(fileSizeTd, _replace) {
fileSizeTd = fileSizeTd.replace(unitType, "");
fileSizeTd = fileSizeTd.replace(
fileSizeTd,
fileSizeTd * fileSizes[_replace]
// Remove the unique identifyer for duplicate values(#number).
columnData[i] = columnData[i].replace(/#[0-9]*/, "");
if (columnData[i] < fileSizes.Kibibyte) {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
`${parseFloat(columnData[i]).toFixed(2)} B`
);
return fileSizeTd;
} else if (
columnData[i] >= fileSizes.Kibibyte &&
columnData[i] < fileSizes.Mebibyte
) {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
`${(columnData[i] / fileSizes.Kibibyte).toFixed(2)} KiB`
);
} else if (
columnData[i] >= fileSizes.Mebibyte &&
columnData[i] < fileSizes.Gibibyte
) {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
`${(columnData[i] / fileSizes.Mebibyte).toFixed(2)} MiB`
);
} else if (
columnData[i] >= fileSizes.Gibibyte &&
columnData[i] < fileSizes.Tebibyte
) {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
`${(columnData[i] / fileSizes.Gibibyte).toFixed(2)} GiB`
);
} else if (
columnData[i] >= fileSizes.Tebibyte &&
columnData[i] < fileSizes.Pebibyte
) {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
`${(columnData[i] / fileSizes.Tebibyte).toFixed(2)} TiB`
);
} else {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
"NaN"
);
}
for (let [i, tr] of tableRows.entries()) {
let fileSizeTd = tr
.querySelectorAll("td")
.item(columnIndex).textContent;
if (fileSizeTd.match(numberWithUnitType)) {
if (fileSizeTd.match(/\s?KB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(
fileSizeTd,
"Kilobyte"
);
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?KiB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(
fileSizeTd,
"Kibibyte"
);
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?MB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(
fileSizeTd,
"Megabyte"
);
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?MiB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(
fileSizeTd,
"Mebibyte"
);
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?GB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(
fileSizeTd,
"Gigabyte"
);
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?GiB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(
fileSizeTd,
"Gibibyte"
);
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?TB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(
fileSizeTd,
"Terabyte"
);
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?TiB/i)) {
fileSizeTd = removeUnitTypeConvertToBytes(
fileSizeTd,
"Tebibyte"
);
columnData.push(`${fileSizeTd}#${i}`);
} else if (fileSizeTd.match(/\s?B/i)) {
fileSizeTd = fileSizeTd.replace(unitType, "");
columnData.push(`${fileSizeTd}#${i}`);
}
} else {
columnData.push(`!X!Y!Z!#${i}`);
}
}
tr.querySelectorAll("td").item(columnIndex).innerHTML =
fileSizeInBytesHTML;
} else if (!isFileSize) {
tr.innerHTML = columnIndexAndTableRow[columnData[i]];
}
}
}
let isRememberSort = sortableTable.classList.contains("remember-sort");
// Checking if user has clicked different column from the first column if yes reset times clicked.
if (!isRememberSort) {
columnIndexesClicked.push(columnIndex);
if (timesClickedColumn === 1 && columnIndexesClicked.length > 1) {
const lastColumnClicked =
columnIndexesClicked[columnIndexesClicked.length - 1];
const secondLastColumnClicked =
columnIndexesClicked[columnIndexesClicked.length - 2];
if (lastColumnClicked !== secondLastColumnClicked) {
timesClickedColumn = 0;
columnIndexesClicked.shift();
}
}
}
th.addEventListener("click", function () {
const columnData = [];
const tableRows = tableBody.querySelectorAll("tr");
timesClickedColumn += 1;
let isDataAttribute = th.classList.contains("data-sort");
// Check if using data-sort attribute; if so sort by value of data-sort
// attribute.
if (isDataAttribute) {
sortDataAttributes(tableRows, columnData);
}
getTableData();
updateTable();
let isFileSize = th.classList.contains("file-size");
// Handle filesize sorting (e.g KB, MB, GB, TB) - Turns data into KiB.
if (isFileSize) {
sortFileSize(tableRows, columnData);
}
function updateTable() {
for (let [i, tr] of tableRows.entries()) {
if (isFileSize) {
tr.innerHTML = fileSizeColumnTextAndRow[columnData[i]];
let fileSizeInBytesHTML = tr
.querySelectorAll("td")
.item(columnIndex).innerHTML;
let fileSizeInBytesText = tr
.querySelectorAll("td")
.item(columnIndex).textContent;
const fileSizes = {
Kibibyte: 1024,
Mebibyte: 1.049e6,
Gibibyte: 1.074e9,
Tebibyte: 1.1e12,
Pebibyte: 1.126e15,
};
// Remove the unique identifyer for duplicate values(#number).
columnData[i] = columnData[i].replace(/#[0-9]*/, "");
if (columnData[i] < fileSizes.Kibibyte) {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
`${parseFloat(columnData[i]).toFixed(2)} B`
);
} else if (
columnData[i] >= fileSizes.Kibibyte &&
columnData[i] < fileSizes.Mebibyte
) {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
`${(columnData[i] / fileSizes.Kibibyte).toFixed(2)} KiB`
);
} else if (
columnData[i] >= fileSizes.Mebibyte &&
columnData[i] < fileSizes.Gibibyte
) {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
`${(columnData[i] / fileSizes.Mebibyte).toFixed(2)} MiB`
);
} else if (
columnData[i] >= fileSizes.Gibibyte &&
columnData[i] < fileSizes.Tebibyte
) {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
`${(columnData[i] / fileSizes.Gibibyte).toFixed(2)} GiB`
);
} else if (
columnData[i] >= fileSizes.Tebibyte &&
columnData[i] < fileSizes.Pebibyte
) {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
`${(columnData[i] / fileSizes.Tebibyte).toFixed(2)} TiB`
);
} else {
fileSizeInBytesHTML = fileSizeInBytesHTML.replace(
fileSizeInBytesText,
"NaN"
);
}
tr.querySelectorAll("td").item(columnIndex).innerHTML =
fileSizeInBytesHTML;
} else if (!isFileSize) {
tr.innerHTML = columnIndexAndTableRow[columnData[i]];
}
}
}
// Checking if user has clicked different column from the first column if
// yes reset times clicked.
let isRememberSort = sortableTable.classList.contains("remember-sort");
if (!isRememberSort) {
rememberSort(timesClickedColumn, columnIndexesClicked);
}
function getTableData() {
for (let [i, tr] of tableRows.entries()) {
// inner text for column we click on
let tdTextContent = tr
.querySelectorAll("td")
.item(columnIndex).textContent;
if (tdTextContent.length === 0) {
tdTextContent = "";
}
if (tdTextContent.trim() !== "") {
if (isFileSize) {
fileSizeColumnTextAndRow[columnData[i]] = tr.innerHTML;
}
if (isDayOfWeek) {
columnIndexAndTableRow[columnData[i]] = tr.innerHTML;
}
if (!isFileSize && !isDayOfWeek && !isDataAttribute) {
columnData.push(`${tdTextContent}#${i}`);
columnIndexAndTableRow[`${tdTextContent}#${i}`] = tr.innerHTML;
}
} else {
// Fill in blank table cells dict key with filler value.
columnData.push(`!X!Y!Z!#${i}`);
columnIndexAndTableRow[`!X!Y!Z!#${i}`] = tr.innerHTML;
}
}
timesClickedColumn += 1;
function naturalSortAescending(a, b) {
if (a.includes("X!Y!Z!#")) {
return 1;
} else if (b.includes("X!Y!Z!#")) {
return -1;
} else {
return a.localeCompare(
b,
navigator.languages[0] || navigator.language,
{ numeric: true, ignorePunctuation: true }
);
}
}
function naturalSortDescending(a, b) {
return naturalSortAescending(b, a);
}
function clearArrows(arrowUp = "▲", arrowDown = "▼") {
th.innerText = th.innerText.replace(arrowUp, "");
th.innerText = th.innerText.replace(arrowDown, "");
}
// Sort naturally; default aescending unless th contains 'order-by-desc' as className.
if (columnData[0] === undefined) {
return;
}
if (timesClickedColumn === 1) {
if (desc) {
if (tableArrows) {
clearArrows(arrowUp, arrowDown);
th.insertAdjacentText("beforeend", arrowDown);
}
columnData.sort(naturalSortDescending, {
numeric: true,
ignorePunctuation: true,
});
} else {
if (tableArrows) {
clearArrows(arrowUp, arrowDown);
th.insertAdjacentText("beforeend", arrowUp);
}
columnData.sort(naturalSortAescending);
}
} else if (timesClickedColumn === 2) {
timesClickedColumn = 0;
if (desc) {
if (tableArrows) {
clearArrows(arrowUp, arrowDown);
th.insertAdjacentText("beforeend", arrowUp);
}
columnData.sort(naturalSortAescending, {
numeric: true,
ignorePunctuation: true,
});
} else {
if (tableArrows) {
clearArrows(arrowUp, arrowDown);
th.insertAdjacentText("beforeend", arrowDown);
}
columnData.sort(naturalSortDescending);
}
}
}
});
}
getTableData(tableRows, columnData, isFileSize, isDataAttribute);
updateTable(tableRows, columnData, isFileSize);
});
}

@@ -410,0 +364,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc