Skip to content

Commit

Permalink
fix: handling negative parenthesis case, and NaN cases
Browse files Browse the repository at this point in the history
  • Loading branch information
scodes73 committed May 18, 2023
1 parent cc15c14 commit 3cd310b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
32 changes: 30 additions & 2 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
// Doesn'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+$/;
const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/;
const inferableClasses = {
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
Expand Down Expand Up @@ -329,12 +329,40 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
const isAlphaSort = th.classList.contains("alpha-sort");
const isNumericSort = th.classList.contains("numeric-sort");
function sortAscending(a, b) {
function handleNumbers(str1, str2) {
let num1, num2;
str1 = str1.slice(0, str1.indexOf("#"));
str2 = str2.slice(0, str2.indexOf("#"));

if (str1.match(/^\((\d+(?:\.\d+)?)\)$/)) {
num1 = -1 * Number(str1.slice(1, -1));
} else {
num1 = Number(str1);
}

if (str2.match(/^\((\d+(?:\.\d+)?)\)$/)) {
num2 = -1 * Number(str2.slice(1, -1));
} else {
num2 = Number(str2);
}

if (!isNaN(num1) && !isNaN(num2)) {
return num1 - num2;
} else {
return str1.localeCompare(
str2,
navigator.languages[0] || navigator.language,
{ numeric: !isAlphaSort, ignorePunctuation: !isPunctSort }
);
}
}

if (a.includes(`${fillValue}#`)) {
return 1;
} else if (b.includes(`${fillValue}#`)) {
return -1;
} else if (isNumericSort) {
return Number(a.substring(0, a.indexOf("#"))) - Number(b.substring(0, b.indexOf("#")))
return handleNumbers(a, b);
} else{
return a.localeCompare(
b,
Expand Down
40 changes: 40 additions & 0 deletions test/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,43 @@ test("Sort all combination of negative and positive integers and decimal numbers
col0: ["-6","-3","-2.3","1","1.05","5","14"],
});
});

test("Sort all combination of negative and positive integers and decimal numbers", () => {
expect(
createTestTable(
{
col0: ["1.05", "-2.3", "-3", "1", "-6", "", "14"],
},
{ classTags: "numeric-sort" }
)
).toStrictEqual({
col0: ["-6","-3","-2.3","1","1.05","14",""],
});
});


test("Sort all negative numbers with parenthesis as well", () => {
expect(
createTestTable(
{
col0: ["1.05", "-2.3", "-3", "1", "-6", "(1.4)", "14"],
},
{ classTags: "numeric-sort" }
)
).toStrictEqual({
col0: ["-6","-3","-2.3","(1.4)","1","1.05","14"],
});
});

test("Sort all combination of negative and positive integers and decimal numbers and even alphabetical random", () => {
expect(
createTestTable(
{
col0: ["1.05", "-2.3", "-3", "1", "-6", "","(0.5)","1a","b","(c)","{1}"],
},
{ classTags: "numeric-sort" }
)
).toStrictEqual({
col0: ["{1}","-6","-3","-2.3","(0.5)","1","1.05","1a","b","(c)",""],
});
});

0 comments on commit 3cd310b

Please sign in to comment.