Skip to content

Commit 4c33cd8

Browse files
NPM 1.18.0; handle commas in numeric sort.
1 parent 3396d40 commit 4c33cd8

File tree

9 files changed

+65
-52
lines changed

9 files changed

+65
-52
lines changed

browser-extensions/chrome/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 3,
33
"author": "Lee Wannacott",
44
"name": "table-sort-js",
5-
"version": "1.17.1",
5+
"version": "1.18.0",
66
"description": "Makes tables sortable using table-sort-js: https://github.com/LeeWannacott/table-sort-js",
77
"icons": { "48": "icons/t.png" },
88
"browser_action": {
130 Bytes
Binary file not shown.

browser-extensions/chrome/table-sort.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
5454
}
5555
}
5656

57-
function inferSortClasses(tableRows, columnIndex, th) {
57+
function inferSortClasses(tableRows, columnIndex, column, th) {
5858
const runtimeRegex = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i;
5959
const fileSizeRegex = /^([.0-9]+)\s?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)/i;
60-
// Doesn't infer dates with delimiter "."; as could capture semantic version numbers.
60+
// Don't infer dates with delimiter "."; as could capture semantic version numbers.
6161
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
6262
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
63-
const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/;
63+
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
64+
const numericRegex =
65+
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
6466
const inferableClasses = {
6567
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
6668
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
@@ -75,7 +77,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
7577
if (regexNotFoundCount >= threshold) {
7678
break;
7779
}
78-
const tableColumn = tr.querySelectorAll("td").item(columnIndex);
80+
const tableColumn = tr
81+
.querySelectorAll("td")
82+
.item(
83+
column.span[columnIndex] === 1
84+
? column.spanSum[columnIndex] - 1
85+
: column.spanSum[columnIndex] - column.span[columnIndex]
86+
);
7987
let foundMatch = false;
8088
for (let key of Object.keys(inferableClasses)) {
8189
let classRegexp = inferableClasses[key].regexp;
@@ -129,11 +137,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
129137
headerIndex++
130138
) {
131139
let columnIndexesClicked = [];
140+
const column = { span: {}, spanSum: {} };
141+
getColSpanData(table.headers[headerIndex], column);
132142
for (let [columnIndex, th] of table.headers[headerIndex].entries()) {
133143
if (!th.classList.contains("disable-sort")) {
134144
th.style.cursor = "pointer";
135145
if (!table.hasClass.noClassInfer) {
136-
inferSortClasses(table.rows[headerIndex], columnIndex, th);
146+
inferSortClasses(table.rows[headerIndex], columnIndex, column, th);
137147
}
138148
makeEachColumnSortable(
139149
th,
@@ -337,6 +347,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
337347

338348
function handleNumbers(str1, str2) {
339349
let num1, num2;
350+
str1 = str1.replaceAll(",", "");
351+
str2 = str2.replaceAll(",", "");
340352
num1 = parseNumberFromString(str1);
341353
num2 = parseNumberFromString(str2);
342354

@@ -416,17 +428,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
416428
template.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
417429
tr = template.content.firstChild;
418430
}
419-
let fileSizeInBytesHTML = column.getColumn(
420-
tr,
421-
column.spanSum,
422-
column.span
423-
).outerHTML;
424-
const fileSizeInBytesText = column.getColumn(
425-
tr,
426-
column.spanSum,
427-
column.span
428-
).textContent;
429-
431+
let getColumnTd = column.getColumn(tr, column.spanSum, column.span);
432+
let fileSizeInBytesHTML = getColumnTd.outerHTML;
433+
const fileSizeInBytesText = getColumnTd.textContent;
430434
const fileSize = column.toBeSorted[i].replace(/#[0-9]*/, "");
431435
let prefixes = ["", "Ki", "Mi", "Gi", "Ti", "Pi"];
432436
let replaced = false;

browser-extensions/firefox/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"author": "Lee Wannacott",
44
"name": "table-sort-js",
5-
"version": "1.17.1",
5+
"version": "1.18.0",
66
"description": "Makes tables sortable using table-sort-js: https://github.com/LeeWannacott/table-sort-js",
77
"icons": { "48": "icons/t.png" },
88
"browser_action": {
130 Bytes
Binary file not shown.

browser-extensions/firefox/table-sort.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
5454
}
5555
}
5656

57-
function inferSortClasses(tableRows, columnIndex, th) {
57+
function inferSortClasses(tableRows, columnIndex, column, th) {
5858
const runtimeRegex = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i;
5959
const fileSizeRegex = /^([.0-9]+)\s?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)/i;
60-
// Doesn't infer dates with delimiter "."; as could capture semantic version numbers.
60+
// Don't infer dates with delimiter "."; as could capture semantic version numbers.
6161
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
6262
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
63-
const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/;
63+
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
64+
const numericRegex =
65+
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
6466
const inferableClasses = {
6567
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
6668
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
@@ -75,7 +77,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
7577
if (regexNotFoundCount >= threshold) {
7678
break;
7779
}
78-
const tableColumn = tr.querySelectorAll("td").item(columnIndex);
80+
const tableColumn = tr
81+
.querySelectorAll("td")
82+
.item(
83+
column.span[columnIndex] === 1
84+
? column.spanSum[columnIndex] - 1
85+
: column.spanSum[columnIndex] - column.span[columnIndex]
86+
);
7987
let foundMatch = false;
8088
for (let key of Object.keys(inferableClasses)) {
8189
let classRegexp = inferableClasses[key].regexp;
@@ -129,11 +137,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
129137
headerIndex++
130138
) {
131139
let columnIndexesClicked = [];
140+
const column = { span: {}, spanSum: {} };
141+
getColSpanData(table.headers[headerIndex], column);
132142
for (let [columnIndex, th] of table.headers[headerIndex].entries()) {
133143
if (!th.classList.contains("disable-sort")) {
134144
th.style.cursor = "pointer";
135145
if (!table.hasClass.noClassInfer) {
136-
inferSortClasses(table.rows[headerIndex], columnIndex, th);
146+
inferSortClasses(table.rows[headerIndex], columnIndex, column, th);
137147
}
138148
makeEachColumnSortable(
139149
th,
@@ -337,6 +347,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
337347

338348
function handleNumbers(str1, str2) {
339349
let num1, num2;
350+
str1 = str1.replaceAll(",", "");
351+
str2 = str2.replaceAll(",", "");
340352
num1 = parseNumberFromString(str1);
341353
num2 = parseNumberFromString(str2);
342354

@@ -416,17 +428,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
416428
template.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
417429
tr = template.content.firstChild;
418430
}
419-
let fileSizeInBytesHTML = column.getColumn(
420-
tr,
421-
column.spanSum,
422-
column.span
423-
).outerHTML;
424-
const fileSizeInBytesText = column.getColumn(
425-
tr,
426-
column.spanSum,
427-
column.span
428-
).textContent;
429-
431+
let getColumnTd = column.getColumn(tr, column.spanSum, column.span);
432+
let fileSizeInBytesHTML = getColumnTd.outerHTML;
433+
const fileSizeInBytesText = getColumnTd.textContent;
430434
const fileSize = column.toBeSorted[i].replace(/#[0-9]*/, "");
431435
let prefixes = ["", "Ki", "Mi", "Gi", "Ti", "Pi"];
432436
let replaced = false;

npm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "table-sort-js",
3-
"version": "1.17.1",
3+
"version": "1.18.0",
44
"description": "A JavaScript client-side HTML table sorting library with no dependencies required.",
55
"license": "MIT",
66
"repository": "LeeWannacott/table-sort-js",

npm/table-sort.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
5454
}
5555
}
5656

57-
function inferSortClasses(tableRows, columnIndex, th) {
57+
function inferSortClasses(tableRows, columnIndex, column, th) {
5858
const runtimeRegex = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i;
5959
const fileSizeRegex = /^([.0-9]+)\s?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)/i;
60-
// Doesn't infer dates with delimiter "."; as could capture semantic version numbers.
60+
// Don't infer dates with delimiter "."; as could capture semantic version numbers.
6161
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
6262
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
63-
const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/;
63+
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
64+
const numericRegex =
65+
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
6466
const inferableClasses = {
6567
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
6668
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
@@ -75,7 +77,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
7577
if (regexNotFoundCount >= threshold) {
7678
break;
7779
}
78-
const tableColumn = tr.querySelectorAll("td").item(columnIndex);
80+
const tableColumn = tr
81+
.querySelectorAll("td")
82+
.item(
83+
column.span[columnIndex] === 1
84+
? column.spanSum[columnIndex] - 1
85+
: column.spanSum[columnIndex] - column.span[columnIndex]
86+
);
7987
let foundMatch = false;
8088
for (let key of Object.keys(inferableClasses)) {
8189
let classRegexp = inferableClasses[key].regexp;
@@ -129,11 +137,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
129137
headerIndex++
130138
) {
131139
let columnIndexesClicked = [];
140+
const column = { span: {}, spanSum: {} };
141+
getColSpanData(table.headers[headerIndex], column);
132142
for (let [columnIndex, th] of table.headers[headerIndex].entries()) {
133143
if (!th.classList.contains("disable-sort")) {
134144
th.style.cursor = "pointer";
135145
if (!table.hasClass.noClassInfer) {
136-
inferSortClasses(table.rows[headerIndex], columnIndex, th);
146+
inferSortClasses(table.rows[headerIndex], columnIndex, column, th);
137147
}
138148
makeEachColumnSortable(
139149
th,
@@ -337,6 +347,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
337347

338348
function handleNumbers(str1, str2) {
339349
let num1, num2;
350+
str1 = str1.replaceAll(",", "");
351+
str2 = str2.replaceAll(",", "");
340352
num1 = parseNumberFromString(str1);
341353
num2 = parseNumberFromString(str2);
342354

@@ -416,17 +428,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
416428
template.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
417429
tr = template.content.firstChild;
418430
}
419-
let fileSizeInBytesHTML = column.getColumn(
420-
tr,
421-
column.spanSum,
422-
column.span
423-
).outerHTML;
424-
const fileSizeInBytesText = column.getColumn(
425-
tr,
426-
column.spanSum,
427-
column.span
428-
).textContent;
429-
431+
let getColumnTd = column.getColumn(tr, column.spanSum, column.span);
432+
let fileSizeInBytesHTML = getColumnTd.outerHTML;
433+
const fileSizeInBytesText = getColumnTd.textContent;
430434
const fileSize = column.toBeSorted[i].replace(/#[0-9]*/, "");
431435
let prefixes = ["", "Ki", "Mi", "Gi", "Ti", "Pi"];
432436
let replaced = false;

public/table-sort.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
6161
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
6262
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
6363
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
64-
const numericRegex = /^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/
64+
const numericRegex =
65+
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
6566
const inferableClasses = {
6667
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
6768
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },

0 commit comments

Comments
 (0)