From 3cd310b91b88985513a109554da1f0a83fd72e3e Mon Sep 17 00:00:00 2001 From: scodes73 Date: Thu, 18 May 2023 17:57:55 +0530 Subject: [PATCH] fix: handling negative parenthesis case, and NaN cases --- public/table-sort.js | 32 ++++++++++++++++++++++++++++++-- test/table.test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/public/table-sort.js b/public/table-sort.js index 614b3ab..ecd643b 100644 --- a/public/table-sort.js +++ b/public/table-sort.js @@ -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 }, @@ -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, diff --git a/test/table.test.js b/test/table.test.js index 5dda800..1a98880 100644 --- a/test/table.test.js +++ b/test/table.test.js @@ -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)",""], + }); +}); \ No newline at end of file