From 3396d40f707b2dcf85561ff30df688b19209994e Mon Sep 17 00:00:00 2001 From: Lee Wannacott Date: Wed, 31 May 2023 00:03:01 +1200 Subject: [PATCH] =?UTF-8?q?numeric-sort=20handle=20number=20with=20commas?= =?UTF-8?q?=E2=80=A6=20(#116)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Handle numbers with commas when sorting with numeric sort; still need to do inference. * Change regex to capture numbers with commas. --- public/index.html | 18 ++++++++++++------ public/table-sort.js | 7 +++++-- test/table.test.js | 41 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/public/index.html b/public/index.html index b50d9a3..0bbd82a 100644 --- a/public/index.html +++ b/public/index.html @@ -19,6 +19,7 @@

Manual testing of table sort js

File Size data-sort days dates in dd/mm/yyyy + file version @@ -31,6 +32,7 @@

Manual testing of table sort js

10b Tuesday 17/6/1978 + 1.18.1 da Vinci @@ -42,6 +44,7 @@

Manual testing of table sort js

192038998987021b Wednesday 18/10/2027 + 239.123.23 Statham @@ -53,6 +56,7 @@

Manual testing of table sort js

134809b Friday 4/9/2008 + 3423.342.34 Micheal @@ -64,6 +68,7 @@

Manual testing of table sort js

30980980b Thursday 2/3/1879 + 890.93.908 @@ -76,6 +81,7 @@

Manual testing of table sort js

902938402398b Monday 8/6/1978 + 2/3/1879

Testing table containing colspan and data-sort and multiple tbodies

@@ -94,7 +100,7 @@

Testing table containing colspan and data-sort and multiple tbodies

Comedy - 1 + Show 1 18/25 72% 3/4 @@ -185,7 +191,7 @@

Testing table containing colspan and data-sort and multiple tbodies

Franklin Benjamin 1706-1-17 - 1 + 1,000.00 k-level 1h 1m 17s 10b @@ -196,7 +202,7 @@

Testing table containing colspan and data-sort and multiple tbodies

da Vinci Zarlo 1452-4-15 - 13000 + -9,000.21 1m 45s 192038998987021b @@ -207,7 +213,7 @@

Testing table containing colspan and data-sort and multiple tbodies

Statham Jason 1967-7-26 - + 55,990.23 HR 11m 40s 134809b @@ -218,7 +224,7 @@

Testing table containing colspan and data-sort and multiple tbodies

Micheal Angelo 1958-8-21 - 54 + 1,000,000.23 Marketing 29s 30980980b @@ -229,7 +235,7 @@

Testing table containing colspan and data-sort and multiple tbodies

Ben 1994/9/23 - 134 + 90102 Marketing 41s 902938402398b diff --git a/public/table-sort.js b/public/table-sort.js index 52ccf16..fded14b 100644 --- a/public/table-sort.js +++ b/public/table-sort.js @@ -57,10 +57,11 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) { 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 }, @@ -345,6 +346,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); diff --git a/test/table.test.js b/test/table.test.js index 41d9721..8a5a687 100644 --- a/test/table.test.js +++ b/test/table.test.js @@ -515,6 +515,43 @@ test("dates-ymd-sort: ISO 8601 style yyyy/mm/dd; delim . or / or -", () => { }); }); +test("Sort decimals with commas", () => { + expect( + createTestTable( + { + col0: { + td: [ + "20,000.89", + "30,000.32", + "1", + "0.111", + "21,000.92", + "19845", + "12000", + "-90", + "-10,000.39", + "-10,000.10", + ], + }, + }, + { classTags: "numeric-sort" } + ) + ).toStrictEqual({ + col0: [ + "-10,000.39", + "-10,000.10", + "-90", + "0.111", + "1", + "12000", + "19845", + "20,000.89", + "21,000.92", + "30,000.32", + ], + }); +}); + test("Sort decimal numbers", () => { expect( createTestTable( @@ -527,7 +564,7 @@ test("Sort decimal numbers", () => { col0: ["0.1", "0.11", "0.13", "0.13", "0.14", "0.2", "0.3"], }); }); - +// test("Sort all combination positive, negative numbers with parenthesis as well", () => { expect( createTestTable( @@ -540,7 +577,7 @@ test("Sort all combination positive, negative numbers with parenthesis as well", 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(