Skip to content

Commit

Permalink
numeric-sort handle number with commas… (#116)
Browse files Browse the repository at this point in the history
* Handle numbers with commas when sorting with numeric sort; still need to do inference.

* Change regex to capture numbers with commas.
  • Loading branch information
LeeWannacott committed May 30, 2023
1 parent 9463e25 commit 3396d40
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
18 changes: 12 additions & 6 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ <h1>Manual testing of table sort js</h1>
<th class="onload-sort">File Size</th>
<th class="data-sort">data-sort days</th>
<th>dates in dd/mm/yyyy</th>
<th>file version</th>
</tr>
</thead>
<tr class="table-row-1">
Expand All @@ -31,6 +32,7 @@ <h1>Manual testing of table sort js</h1>
<td>10b</td>
<td data-sort="2">Tuesday</td>
<td>17/6/1978</td>
<td>1.18.1</td>
</tr>
<tr class="table-row-2">
<td>da Vinci</td>
Expand All @@ -42,6 +44,7 @@ <h1>Manual testing of table sort js</h1>
<td>192038998987021b</td>
<td data-sort="3">Wednesday</td>
<td>18/10/2027</td>
<td>239.123.23</td>
</tr>
<tr>
<td>Statham</td>
Expand All @@ -53,6 +56,7 @@ <h1>Manual testing of table sort js</h1>
<td>134809b</td>
<td data-sort="5">Friday</td>
<td>4/9/2008</td>
<td>3423.342.34</td>
</tr>
<tr>
<td>Micheal</td>
Expand All @@ -64,6 +68,7 @@ <h1>Manual testing of table sort js</h1>
<td>30980980b</td>
<td data-sort="4">Thursday</td>
<td>2/3/1879</td>
<td>890.93.908</td>
</tr>

<tr>
Expand All @@ -76,6 +81,7 @@ <h1>Manual testing of table sort js</h1>
<td>902938402398b</td>
<td data-sort="1">Monday</td>
<td>8/6/1978</td>
<td>2/3/1879</td>
</tr>
</table>
<h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
Expand All @@ -94,7 +100,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
</thead>
<td class="tags"></td>
<td class="category">Comedy</td>
<td class="show_name">1</td>
<td class="show_name">Show 1</td>
<td class="ratio all" data-sort="72">18/25</td>
<td class="pct all">72%</td>
<td class="ratio ours" data-sort="75">3/4</td>
Expand Down Expand Up @@ -185,7 +191,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
<td>Franklin</td>
<td>Benjamin</td>
<td>1706-1-17</td>
<td>1</td>
<td>1,000.00</td>
<td>k-level</td>
<td>1h 1m 17s</td>
<td>10b</td>
Expand All @@ -196,7 +202,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
<td>da Vinci</td>
<td>Zarlo</td>
<td>1452-4-15</td>
<td>13000</td>
<td>-9,000.21</td>
<td></td>
<td>1m 45s</td>
<td>192038998987021b</td>
Expand All @@ -207,7 +213,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
<td>Statham</td>
<td>Jason</td>
<td>1967-7-26</td>
<td></td>
<td>55,990.23</td>
<td>HR</td>
<td>11m 40s</td>
<td>134809b</td>
Expand All @@ -218,7 +224,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
<td>Micheal</td>
<td>Angelo</td>
<td>1958-8-21</td>
<td>54</td>
<td>1,000,000.23</td>
<td>Marketing</td>
<td>29s</td>
<td>30980980b</td>
Expand All @@ -229,7 +235,7 @@ <h2>Testing table containing colspan and data-sort and multiple tbodies</h2>
<td>Ben</td>
<td></td>
<td>1994/9/23</td>
<td>134</td>
<td>90102</td>
<td>Marketing</td>
<td>41s</td>
<td>902938402398b</td>
Expand Down
7 changes: 5 additions & 2 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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);

Expand Down
41 changes: 39 additions & 2 deletions test/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 3396d40

Please sign in to comment.