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.13.0 to 1.14.0

2

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

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

@@ -48,18 +48,19 @@ ![table-sort-js](https://img.shields.io/npm/v/table-sort-js)

| "table-sort" | Make the table sortable! (Words, numbers, dates, file sizes)... |
| "no-class-infer" | Turns off inference for adding sort classes automatically i.e (file-size-sort, runtime-sort, dates-dmy-sort). |
| "table-arrows" | Display ascending or descending triangles. |
| "no-class-infer" | Turns off inference for adding sort classes automatically i.e (file-size-sort, runtime-sort, dates-dmy-sort). |
| "remember-sort" | If clicking on different columns remembers sort of the original 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"> |
| "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. |
| "dates-mdy-sort" | Sorts dates in mm/dd/yyyy format. e.g (12/28/2023). Can use "/" or "-" or "." as separator. Overides inferred "dates-dmy-sort" class. |
| <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> Inferred Classes. | Description |
| ---------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| "dates-dmy-sort" | Sorts dates in dd/mm/yyyy format. e.g (18/10/1995). Can use "/" or "-" or "." as separator. |
| "runtime-sort" | Sorts runtime in hours minutes and seconds e.g (10h 1m 20s). Useful for sorting the GitHub actions Run time column... |
| "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) |
| <th> Inferred Classes. | Description |
| ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| "dates-dmy-sort" | Sorts dates in dd/mm/yyyy format. e.g (18/10/1995). Can use "/" or "-" as separator. |
| "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) |
| "runtime-sort" | Sorts runtime in hours minutes and seconds e.g (10h 1m 20s). Useful for sorting the GitHub actions Run time column... |

@@ -66,0 +67,0 @@ | <th> Classes that change defaults. | Description |

@@ -73,9 +73,12 @@ /*

const regexFileSizeSort = /^([.0-9]+)\s?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)/i;
const regexDates = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
let runtimeSortCounter = 0,
fileSizeSortCounter = 0,
datesSortCounter = 0;
// 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, datesSortMatch;
let runtimeSortMatch, fileSizeSortMatch, datesMatch, isoDatesMatch;
const tableColumn = tr.querySelectorAll("td").item(columnIndex);

@@ -87,32 +90,32 @@ if (tableColumn.innerText) {

fileSizeSortMatch = tableColumn.innerText.match(regexFileSizeSort);
datesSortMatch = tableColumn.innerText.match(regexDates);
datesMatch = tableColumn.innerText.match(datesRegex);
isoDatesMatch = tableColumn.innerText.match(regexISODates);
}
if (runtimeSortMatch) {
runtimeSortCounter++;
runtimeCounter++;
}
if (fileSizeSortMatch) {
fileSizeSortCounter++;
fileSizeCounter++;
}
if (datesSortMatch) {
datesSortCounter++;
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,
runtimeSortCounter,
"runtime-sort"
);
addInferredClass(
th,
tableColumnLength,
fileSizeSortCounter,
fileSizeCounter,
"file-size-sort"
);
addInferredClass(th, tableColumnLength, datesCounter, "dates-dmy-sort");
addInferredClass(
th,
tableColumnLength,
datesSortCounter,
"dates-dmy-sort"
isoDatesCounter,
"dates-ymd-sort"
);

@@ -231,27 +234,26 @@ }

function sortDates(dateFormat, tableRows, columnData) {
function sortDates(datesFormat, tableRows, columnData) {
try {
for (let [i, tr] of tableRows.entries()) {
let columnOfTd;
const regexDate = /^(\d\d?)[./-](\d\d?)[./-]((\d\d)?\d\d)$/;
let columnOfTd, datesRegex;
if (datesFormat === "mdy" || datesFormat === "dmy") {
datesRegex = /^(\d\d?)[./-](\d\d?)[./-]((\d\d)?\d\d)/;
} else if (datesFormat === "ymd") {
datesRegex = /^(\d\d\d\d)[./-](\d\d?)[./-](\d\d?)/;
}
columnOfTd = tr.querySelectorAll("td").item(columnIndex).textContent;
let match = columnOfTd.match(regexDate);
let match = columnOfTd.match(datesRegex);
let [years, days, months] = [0, 0, 0];
let numberToSort = columnOfTd;
if (match) {
const regexFirstNumber = match[1];
const regexSecondNumber = match[2];
const regexYears = match[3];
if (regexFirstNumber && regexSecondNumber) {
if (dateFormat === "mdy") {
days = regexSecondNumber;
months = regexFirstNumber;
const [regPos1, regPos2, regPos3] = [match[1], match[2], match[3]];
if (regPos1 && regPos2 && regPos3) {
if (datesFormat === "mdy") {
[months, days, years] = [regPos1, regPos2, regPos3];
} else if (datesFormat === "ymd") {
[years, months, days] = [regPos1, regPos2, regPos3];
} else {
days = regexFirstNumber;
months = regexSecondNumber;
[days, months, years] = [regPos1, regPos2, regPos3];
}
}
if (regexYears) {
years = regexYears;
}
numberToSort = Number(

@@ -304,2 +306,3 @@ years +

isSortDateMonthDayYear,
isSortDateYearMonthDay,
isDataAttribute,

@@ -330,2 +333,3 @@ colSpanData,

!isSortDateDayMonthYear &&
!isSortDateYearMonthDay &&
!isSortDateMonthDayYear

@@ -475,5 +479,8 @@ ) {

const isSortDateMonthDayYear = th.classList.contains("dates-mdy-sort");
const isSortDateYearMonthDay = th.classList.contains("dates-ymd-sort");
// pick mdy first to override the inferred default class which is dmy.
if (isSortDateMonthDayYear) {
sortDates("mdy", visibleTableRows, columnData);
} else if (isSortDateYearMonthDay) {
sortDates("ymd", visibleTableRows, columnData);
} else if (isSortDateDayMonthYear) {

@@ -497,2 +504,3 @@ sortDates("dmy", visibleTableRows, columnData);

isSortDateMonthDayYear,
isSortDateYearMonthDay,
isDataAttribute,

@@ -499,0 +507,0 @@ isTimeSort,

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