Skip to content

Commit 91b514a

Browse files
Version 1.21.0 - currency signs and percentages.
1 parent f30d496 commit 91b514a

File tree

9 files changed

+56
-46
lines changed

9 files changed

+56
-46
lines changed

browser-extensions/chrome/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 3,
33
"author": "Lee Wannacott",
44
"name": "table-sort-js",
5-
"version": "1.20.0",
5+
"version": "1.21.0",
66
"description": "Makes tables sortable using table-sort-js: https://github.com/LeeWannacott/table-sort-js",
77
"icons": { "48": "icons/t.png" },
88
"browser_action": {
102 Bytes
Binary file not shown.

browser-extensions/chrome/table-sort.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
6464
// Don't infer dates with delimiter "."; as could capture semantic version numbers.
6565
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
6666
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
67-
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
6867
const numericRegex =
69-
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
68+
/^-?(?:[$£¥฿Ξξ¤¿\u20A1\uFFE0]\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)(?:%?)$/;
69+
7070
const inferableClasses = {
7171
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
7272
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
@@ -91,11 +91,12 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
9191
let foundMatch = false;
9292
for (let key of Object.keys(inferableClasses)) {
9393
let classRegexp = inferableClasses[key].regexp;
94-
if (tableColumn.innerText !== undefined) {
95-
if (tableColumn.innerText.match(classRegexp)) {
96-
foundMatch = true;
97-
inferableClasses[key].count++;
98-
}
94+
let columnOfTd = testingTableSortJS
95+
? tableColumn.textContent
96+
: tableColumn.innerText;
97+
if (columnOfTd !== undefined && columnOfTd.match(classRegexp)) {
98+
foundMatch = true;
99+
inferableClasses[key].count++;
99100
}
100101
if (inferableClasses[key].count >= threshold) {
101102
th.classList.add(inferableClasses[key].class);
@@ -341,7 +342,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
341342
function parseNumberFromString(str) {
342343
let num;
343344
str = str.slice(0, str.indexOf("#"));
344-
if (str.match(/^\((\d+(?:\.\d+)?)\)$/)) {
345+
if (str.match(/^\(-?(\d+(?:\.\d+)?)\)$/)) {
345346
num = -1 * Number(str.slice(1, -1));
346347
} else {
347348
num = Number(str);
@@ -358,11 +359,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
358359
}
359360

360361
function handleNumbers(str1, str2) {
361-
let num1, num2;
362-
str1 = str1.replaceAll(",", "");
363-
str2 = str2.replaceAll(",", "");
364-
num1 = parseNumberFromString(str1);
365-
num2 = 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+
];
366369

367370
if (!isNaN(num1) && !isNaN(num2)) {
368371
return num1 - num2;

browser-extensions/firefox/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"author": "Lee Wannacott",
44
"name": "table-sort-js",
5-
"version": "1.20.0",
5+
"version": "1.21.0",
66
"description": "Makes tables sortable using table-sort-js: https://github.com/LeeWannacott/table-sort-js",
77
"icons": { "48": "icons/t.png" },
88
"browser_action": {
102 Bytes
Binary file not shown.

browser-extensions/firefox/table-sort.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
6464
// Don't infer dates with delimiter "."; as could capture semantic version numbers.
6565
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
6666
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
67-
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
6867
const numericRegex =
69-
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
68+
/^-?(?:[$£¥฿Ξξ¤¿\u20A1\uFFE0]\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)(?:%?)$/;
69+
7070
const inferableClasses = {
7171
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
7272
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
@@ -91,11 +91,12 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
9191
let foundMatch = false;
9292
for (let key of Object.keys(inferableClasses)) {
9393
let classRegexp = inferableClasses[key].regexp;
94-
if (tableColumn.innerText !== undefined) {
95-
if (tableColumn.innerText.match(classRegexp)) {
96-
foundMatch = true;
97-
inferableClasses[key].count++;
98-
}
94+
let columnOfTd = testingTableSortJS
95+
? tableColumn.textContent
96+
: tableColumn.innerText;
97+
if (columnOfTd !== undefined && columnOfTd.match(classRegexp)) {
98+
foundMatch = true;
99+
inferableClasses[key].count++;
99100
}
100101
if (inferableClasses[key].count >= threshold) {
101102
th.classList.add(inferableClasses[key].class);
@@ -341,7 +342,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
341342
function parseNumberFromString(str) {
342343
let num;
343344
str = str.slice(0, str.indexOf("#"));
344-
if (str.match(/^\((\d+(?:\.\d+)?)\)$/)) {
345+
if (str.match(/^\(-?(\d+(?:\.\d+)?)\)$/)) {
345346
num = -1 * Number(str.slice(1, -1));
346347
} else {
347348
num = Number(str);
@@ -358,11 +359,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
358359
}
359360

360361
function handleNumbers(str1, str2) {
361-
let num1, num2;
362-
str1 = str1.replaceAll(",", "");
363-
str2 = str2.replaceAll(",", "");
364-
num1 = parseNumberFromString(str1);
365-
num2 = 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+
];
366369

367370
if (!isNaN(num1) && !isNaN(num2)) {
368371
return num1 - num2;

npm/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) |
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. |

npm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "table-sort-js",
3-
"version": "1.20.0",
3+
"version": "1.21.0",
44
"description": "A JavaScript client-side HTML table sorting library with no dependencies required.",
55
"license": "MIT",
66
"repository": "LeeWannacott/table-sort-js",

npm/table-sort.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
6464
// Don't infer dates with delimiter "."; as could capture semantic version numbers.
6565
const dmyRegex = /^(\d\d?)[/-](\d\d?)[/-]((\d\d)?\d\d)/;
6666
const ymdRegex = /^(\d\d\d\d)[/-](\d\d?)[/-](\d\d?)/;
67-
// const numericRegex = /^(?:\(\d+(?:\.\d+)?\)|-?\d+(?:\.\d+)?)$/; doesn't handle commas
6867
const numericRegex =
69-
/^-?(?:\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)$/;
68+
/^-?(?:[$£¥฿Ξξ¤¿\u20A1\uFFE0]\d{1,3}(?:[',]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?(?:[',]\d{3})*?)(?:%?)$/;
69+
7070
const inferableClasses = {
7171
runtime: { regexp: runtimeRegex, class: "runtime-sort", count: 0 },
7272
filesize: { regexp: fileSizeRegex, class: "file-size-sort", count: 0 },
@@ -91,11 +91,12 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
9191
let foundMatch = false;
9292
for (let key of Object.keys(inferableClasses)) {
9393
let classRegexp = inferableClasses[key].regexp;
94-
if (tableColumn.innerText !== undefined) {
95-
if (tableColumn.innerText.match(classRegexp)) {
96-
foundMatch = true;
97-
inferableClasses[key].count++;
98-
}
94+
let columnOfTd = testingTableSortJS
95+
? tableColumn.textContent
96+
: tableColumn.innerText;
97+
if (columnOfTd !== undefined && columnOfTd.match(classRegexp)) {
98+
foundMatch = true;
99+
inferableClasses[key].count++;
99100
}
100101
if (inferableClasses[key].count >= threshold) {
101102
th.classList.add(inferableClasses[key].class);
@@ -341,7 +342,7 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
341342
function parseNumberFromString(str) {
342343
let num;
343344
str = str.slice(0, str.indexOf("#"));
344-
if (str.match(/^\((\d+(?:\.\d+)?)\)$/)) {
345+
if (str.match(/^\(-?(\d+(?:\.\d+)?)\)$/)) {
345346
num = -1 * Number(str.slice(1, -1));
346347
} else {
347348
num = Number(str);
@@ -358,11 +359,13 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
358359
}
359360

360361
function handleNumbers(str1, str2) {
361-
let num1, num2;
362-
str1 = str1.replaceAll(",", "");
363-
str2 = str2.replaceAll(",", "");
364-
num1 = parseNumberFromString(str1);
365-
num2 = 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+
];
366369

367370
if (!isNaN(num1) && !isNaN(num2)) {
368371
return num1 - num2;

0 commit comments

Comments
 (0)