Skip to content

Commit f30d496

Browse files
Handle percentages with numeric-sort class (#133)
* Support for percentages in numeric-sort. * Format readme and swap cdn scripts.
1 parent 8f38e5f commit f30d496

File tree

4 files changed

+71
-16
lines changed

4 files changed

+71
-16
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
- <b>Option 1</b>: Load as script from a Content Delivery Network (CDN):
2222

2323
```javascript
24-
<script src="https://cdn.jsdelivr.net/npm/table-sort-js/table-sort.js"></script>
24+
<script src="https://cdn.jsdelivr.net/npm/table-sort-js/table-sort.min.js"></script>
2525
```
2626

27-
Or Minified (smaller size, but harder to debug!):
27+
Or non-minified version (larger size, but easier to debug!):
2828

2929
```javascript
30-
<script src="https://cdn.jsdelivr.net/npm/table-sort-js/table-sort.min.js"></script>
30+
<script src="https://cdn.jsdelivr.net/npm/table-sort-js/table-sort.js"></script>
3131
```
3232

3333
Example on how to use table-sort-js with [HTML](https://leewannacott.github.io/table-sort-js/docs/html5.html)
@@ -72,7 +72,8 @@ Examples on using table-sort-js with frontend frameworks such as [React.js](http
7272

7373
| &lt;th&gt; Inferred Classes. | Description |
7474
| ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
75-
| "numeric-sort" | Sorts numbers including decimals - Positive, Negative (in both minus and parenthesis representations). Supports for common currencies e.g ($£€¥) |
75+
| "numeric-sort" | Sorts numbers including decimals - Positive, Negative (in both minus and parenthesis representations). |
76+
| | Supports common currencies e.g ($£€¥) and percentage signs e.g (0.39%) |
7677
| "dates-dmy-sort" | Sorts dates in dd/mm/yyyy format. e.g (18/10/1995). Can use "/" or "-" as separator. |
7778
| "dates-ymd-sort" | Sorts dates in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) yyyy/mm/dd format. e.g (2021/10/28). Use "/" or "-" as separator. |
7879
| "file-size-sort" | Sorts file sizes(B->TiB) uses the binary prefix. (e.g 10 B, 100 KiB, 1 MiB); optional space between number and prefix. |

public/table-sort.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
6565
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
6666
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
6767
const numericRegex =
68-
/^-?(?:[$£¥฿Ξξ¤¿\u20A1\uFFE0]\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
69-
68+
/^-?(?:[$£¥฿Ξξ¤¿\u20A1\uFFE0]\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)(?:%?)$/;
7069

7170
const inferableClasses = {
7271
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
@@ -93,11 +92,11 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
9392
for (let key of Object.keys(inferableClasses)) {
9493
let classRegexp = inferableClasses[key].regexp;
9594
let columnOfTd = testingTableSortJS
96-
? tableColumn.textContent
97-
: tableColumn.innerText;
98-
if (columnOfTd !== undefined && columnOfTd.match(classRegexp) ) {
99-
foundMatch = true;
100-
inferableClasses[key].count++;
95+
? tableColumn.textContent
96+
: tableColumn.innerText;
97+
if (columnOfTd !== undefined && columnOfTd.match(classRegexp)) {
98+
foundMatch = true;
99+
inferableClasses[key].count++;
101100
}
102101
if (inferableClasses[key].count >= threshold) {
103102
th.classList.add(inferableClasses[key].class);
@@ -360,10 +359,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
360359
}
361360

362361
function handleNumbers(str1, str2) {
363-
const currencyAndComma = /[$£¥฿Ξξ¤¿\u20A1\uFFE0, ]/g
364-
str1 = str1.replace(currencyAndComma, "");
365-
str2 = str2.replace(currencyAndComma, "");
366-
const [num1, num2] = [parseNumberFromString(str1),parseNumberFromString(str2)];
362+
const matchCurrencyCommaAndPercent = /[$£¥฿Ξξ¤¿\u20A1\uFFE0,% ]/g;
363+
str1 = str1.replace(matchCurrencyCommaAndPercent, "");
364+
str2 = str2.replace(matchCurrencyCommaAndPercent, "");
365+
const [num1, num2] = [
366+
parseNumberFromString(str1),
367+
parseNumberFromString(str2),
368+
];
367369

368370
if (!isNaN(num1) && !isNaN(num2)) {
369371
return num1 - num2;
@@ -564,7 +566,6 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
564566
}
565567
timesClickedColumn += 1;
566568

567-
568569
const hasThClass = {
569570
dataSort: th.classList.contains("data-sort"),
570571
fileSize: th.classList.contains("file-size-sort"),

test/table.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,55 @@ test("Sort all combination of negative and positive integers and decimal numbers
617617
});
618618
});
619619

620+
test("numeric-sort - percentages including negative", () => {
621+
expect(
622+
createTestTable(
623+
{
624+
col0: {
625+
td: [
626+
"0.88%",
627+
"-0.98%",
628+
"0.22%",
629+
"-0.28%",
630+
"0.27%",
631+
"0.273%",
632+
"(0.273%)",
633+
"(0.893%)",
634+
"22.273%",
635+
"100.273%",
636+
"10.273%",
637+
"-10.273%",
638+
"-81.273%",
639+
"-82.273%",
640+
"11.823%",
641+
"11.803%",
642+
],
643+
},
644+
},
645+
{ classTags: "numeric-sort" }
646+
)
647+
).toStrictEqual({
648+
col0: [
649+
"-82.273%",
650+
"-81.273%",
651+
"-10.273%",
652+
"-0.98%",
653+
"(0.893%)",
654+
"-0.28%",
655+
"(0.273%)",
656+
"0.22%",
657+
"0.27%",
658+
"0.273%",
659+
"0.88%",
660+
"10.273%",
661+
"11.803%",
662+
"11.823%",
663+
"22.273%",
664+
"100.273%",
665+
],
666+
});
667+
});
668+
620669
test("numeric-sort - various currency", () => {
621670
expect(
622671
createTestTable(

test/tagsInferenceTable.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,16 @@ test("InferSortClassesOnTH - NUMERIC", () => {
150150
"-£755,905",
151151
],
152152
},
153+
col5: {
154+
td: ["0.88%", "-0.98%", "0.22%", "0.28%", "0.57%"],
155+
},
153156
})
154157
).toStrictEqual([
155158
"numeric-sort",
156159
"numeric-sort",
157160
"numeric-sort",
158161
"numeric-sort",
159162
"numeric-sort",
163+
"numeric-sort",
160164
]);
161165
});

0 commit comments

Comments
 (0)