Skip to content

Commit

Permalink
NPM 1.18.0; handle commas in numeric sort.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-wannacott committed May 30, 2023
1 parent 3396d40 commit 4c33cd8
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 52 deletions.
2 changes: 1 addition & 1 deletion browser-extensions/chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"author": "Lee Wannacott",
"name": "table-sort-js",
"version": "1.17.1",
"version": "1.18.0",
"description": "Makes tables sortable using table-sort-js: https://github.com/LeeWannacott/table-sort-js",
"icons": { "48": "icons/t.png" },
"browser_action": {
Expand Down
Binary file modified browser-extensions/chrome/table-sort-js.zip
Binary file not shown.
36 changes: 20 additions & 16 deletions browser-extensions/chrome/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}
}

function inferSortClasses(tableRows, columnIndex, th) {
function inferSortClasses(tableRows, columnIndex, column, th) {
const runtimeRegex = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i;
const fileSizeRegex = /^([.0-9]+)\s?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)/i;
// Doesn't infer dates with delimiter "."; as could capture semantic version numbers.
// Don't infer dates with delimiter "."; as could capture semantic version numbers.
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/;
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
const numericRegex =
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
const inferableClasses = {
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
Expand All @@ -75,7 +77,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
if (regexNotFoundCount >= threshold) {
break;
}
const tableColumn = tr.querySelectorAll("td").item(columnIndex);
const tableColumn = tr
.querySelectorAll("td")
.item(
column.span[columnIndex] === 1
? column.spanSum[columnIndex] - 1
: column.spanSum[columnIndex] - column.span[columnIndex]
);
let foundMatch = false;
for (let key of Object.keys(inferableClasses)) {
let classRegexp = inferableClasses[key].regexp;
Expand Down Expand Up @@ -129,11 +137,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
headerIndex++
) {
let columnIndexesClicked = [];
const column = { span: {}, spanSum: {} };
getColSpanData(table.headers[headerIndex], column);
for (let [columnIndex, th] of table.headers[headerIndex].entries()) {
if (!th.classList.contains("disable-sort")) {
th.style.cursor = "pointer";
if (!table.hasClass.noClassInfer) {
inferSortClasses(table.rows[headerIndex], columnIndex, th);
inferSortClasses(table.rows[headerIndex], columnIndex, column, th);
}
makeEachColumnSortable(
th,
Expand Down Expand Up @@ -337,6 +347,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {

function handleNumbers(str1, str2) {
let num1, num2;
str1 = str1.replaceAll(",", "");
str2 = str2.replaceAll(",", "");
num1 = parseNumberFromString(str1);
num2 = parseNumberFromString(str2);

Expand Down Expand Up @@ -416,17 +428,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
template.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
tr = template.content.firstChild;
}
let fileSizeInBytesHTML = column.getColumn(
tr,
column.spanSum,
column.span
).outerHTML;
const fileSizeInBytesText = column.getColumn(
tr,
column.spanSum,
column.span
).textContent;

let getColumnTd = column.getColumn(tr, column.spanSum, column.span);
let fileSizeInBytesHTML = getColumnTd.outerHTML;
const fileSizeInBytesText = getColumnTd.textContent;
const fileSize = column.toBeSorted[i].replace(/#[0-9]*/, "");
let prefixes = ["", "Ki", "Mi", "Gi", "Ti", "Pi"];
let replaced = false;
Expand Down
2 changes: 1 addition & 1 deletion browser-extensions/firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"author": "Lee Wannacott",
"name": "table-sort-js",
"version": "1.17.1",
"version": "1.18.0",
"description": "Makes tables sortable using table-sort-js: https://github.com/LeeWannacott/table-sort-js",
"icons": { "48": "icons/t.png" },
"browser_action": {
Expand Down
Binary file modified browser-extensions/firefox/table-sort-js.zip
Binary file not shown.
36 changes: 20 additions & 16 deletions browser-extensions/firefox/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}
}

function inferSortClasses(tableRows, columnIndex, th) {
function inferSortClasses(tableRows, columnIndex, column, th) {
const runtimeRegex = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i;
const fileSizeRegex = /^([.0-9]+)\s?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)/i;
// Doesn't infer dates with delimiter "."; as could capture semantic version numbers.
// Don't infer dates with delimiter "."; as could capture semantic version numbers.
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/;
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
const numericRegex =
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
const inferableClasses = {
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
Expand All @@ -75,7 +77,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
if (regexNotFoundCount >= threshold) {
break;
}
const tableColumn = tr.querySelectorAll("td").item(columnIndex);
const tableColumn = tr
.querySelectorAll("td")
.item(
column.span[columnIndex] === 1
? column.spanSum[columnIndex] - 1
: column.spanSum[columnIndex] - column.span[columnIndex]
);
let foundMatch = false;
for (let key of Object.keys(inferableClasses)) {
let classRegexp = inferableClasses[key].regexp;
Expand Down Expand Up @@ -129,11 +137,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
headerIndex++
) {
let columnIndexesClicked = [];
const column = { span: {}, spanSum: {} };
getColSpanData(table.headers[headerIndex], column);
for (let [columnIndex, th] of table.headers[headerIndex].entries()) {
if (!th.classList.contains("disable-sort")) {
th.style.cursor = "pointer";
if (!table.hasClass.noClassInfer) {
inferSortClasses(table.rows[headerIndex], columnIndex, th);
inferSortClasses(table.rows[headerIndex], columnIndex, column, th);
}
makeEachColumnSortable(
th,
Expand Down Expand Up @@ -337,6 +347,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {

function handleNumbers(str1, str2) {
let num1, num2;
str1 = str1.replaceAll(",", "");
str2 = str2.replaceAll(",", "");
num1 = parseNumberFromString(str1);
num2 = parseNumberFromString(str2);

Expand Down Expand Up @@ -416,17 +428,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
template.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
tr = template.content.firstChild;
}
let fileSizeInBytesHTML = column.getColumn(
tr,
column.spanSum,
column.span
).outerHTML;
const fileSizeInBytesText = column.getColumn(
tr,
column.spanSum,
column.span
).textContent;

let getColumnTd = column.getColumn(tr, column.spanSum, column.span);
let fileSizeInBytesHTML = getColumnTd.outerHTML;
const fileSizeInBytesText = getColumnTd.textContent;
const fileSize = column.toBeSorted[i].replace(/#[0-9]*/, "");
let prefixes = ["", "Ki", "Mi", "Gi", "Ti", "Pi"];
let replaced = false;
Expand Down
2 changes: 1 addition & 1 deletion npm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "table-sort-js",
"version": "1.17.1",
"version": "1.18.0",
"description": "A JavaScript client-side HTML table sorting library with no dependencies required.",
"license": "MIT",
"repository": "LeeWannacott/table-sort-js",
Expand Down
36 changes: 20 additions & 16 deletions npm/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
}
}

function inferSortClasses(tableRows, columnIndex, th) {
function inferSortClasses(tableRows, columnIndex, column, th) {
const runtimeRegex = /^(\d+h)?\s?(\d+m)?\s?(\d+s)?$/i;
const fileSizeRegex = /^([.0-9]+)\s?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)/i;
// Doesn't infer dates with delimiter "."; as could capture semantic version numbers.
// Don't infer dates with delimiter "."; as could capture semantic version numbers.
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/;
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
const numericRegex =
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
const inferableClasses = {
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
Expand All @@ -75,7 +77,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
if (regexNotFoundCount >= threshold) {
break;
}
const tableColumn = tr.querySelectorAll("td").item(columnIndex);
const tableColumn = tr
.querySelectorAll("td")
.item(
column.span[columnIndex] === 1
? column.spanSum[columnIndex] - 1
: column.spanSum[columnIndex] - column.span[columnIndex]
);
let foundMatch = false;
for (let key of Object.keys(inferableClasses)) {
let classRegexp = inferableClasses[key].regexp;
Expand Down Expand Up @@ -129,11 +137,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
headerIndex++
) {
let columnIndexesClicked = [];
const column = { span: {}, spanSum: {} };
getColSpanData(table.headers[headerIndex], column);
for (let [columnIndex, th] of table.headers[headerIndex].entries()) {
if (!th.classList.contains("disable-sort")) {
th.style.cursor = "pointer";
if (!table.hasClass.noClassInfer) {
inferSortClasses(table.rows[headerIndex], columnIndex, th);
inferSortClasses(table.rows[headerIndex], columnIndex, column, th);
}
makeEachColumnSortable(
th,
Expand Down Expand Up @@ -337,6 +347,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {

function handleNumbers(str1, str2) {
let num1, num2;
str1 = str1.replaceAll(",", "");
str2 = str2.replaceAll(",", "");
num1 = parseNumberFromString(str1);
num2 = parseNumberFromString(str2);

Expand Down Expand Up @@ -416,17 +428,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
template.innerHTML = columnIndexAndTableRow[column.toBeSorted[i]];
tr = template.content.firstChild;
}
let fileSizeInBytesHTML = column.getColumn(
tr,
column.spanSum,
column.span
).outerHTML;
const fileSizeInBytesText = column.getColumn(
tr,
column.spanSum,
column.span
).textContent;

let getColumnTd = column.getColumn(tr, column.spanSum, column.span);
let fileSizeInBytesHTML = getColumnTd.outerHTML;
const fileSizeInBytesText = getColumnTd.textContent;
const fileSize = column.toBeSorted[i].replace(/#[0-9]*/, "");
let prefixes = ["", "Ki", "Mi", "Gi", "Ti", "Pi"];
let replaced = false;
Expand Down
3 changes: 2 additions & 1 deletion public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
const numericRegex = /^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/
const numericRegex =
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
const inferableClasses = {
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
Expand Down

0 comments on commit 4c33cd8

Please sign in to comment.