Skip to content

Commit

Permalink
nit: basic refactor and README update
Browse files Browse the repository at this point in the history
  • Loading branch information
scodes73 committed May 19, 2023
1 parent 94f82e9 commit 7af7fc0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Refer to the documenation for examples on how to use table-sort-js with [HTML](h
| "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. |
| "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. |
| "runtime-sort" | Sorts runtime in hours minutes and seconds e.g (10h 1m 20s). Useful for sorting the GitHub actions Run time column... |
| "numeric-sort" | Sorts numbers - Positive, Negative (Both minus and parenthesis representations), Decimals (can also be used for Semantic Versioning)|
| "numeric-sort" | Sorts numbers - Positive, Negative (Both minus and parenthesis representations), and Decimals |

| <th> Classes that change defaults. | Description |
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
Expand Down
53 changes: 27 additions & 26 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,38 +328,39 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
const isPunctSort = th.classList.contains("punct-sort");
const isAlphaSort = th.classList.contains("alpha-sort");
const isNumericSort = th.classList.contains("numeric-sort");
function sortAscending(a, b) {
function parseNumberFromString(str) {
let num;
str = str.slice(0, str.indexOf("#"));
if (str.match(/^\((\d+(?:\.\d+)?)\)$/)) {
num = -1 * Number(str.slice(1, -1));
} else {
num = Number(str);
}
return num;
}

function strLocaleCompare(str1, str2) {
return str1.localeCompare(
str2,
navigator.languages[0] || navigator.language,
{ numeric: !isAlphaSort, ignorePunctuation: !isPunctSort }
);
function parseNumberFromString(str) {
let num;
str = str.slice(0, str.indexOf("#"));
if (str.match(/^\((\d+(?:\.\d+)?)\)$/)) {
num = -1 * Number(str.slice(1, -1));
} else {
num = Number(str);
}
return num;
}

function handleNumbers(str1, str2) {
let num1, num2;
num1 = parseNumberFromString(str1);
num2 = parseNumberFromString(str2);
function strLocaleCompare(str1, str2) {
return str1.localeCompare(
str2,
navigator.languages[0] || navigator.language,
{ numeric: !isAlphaSort, ignorePunctuation: !isPunctSort }
);
}

if (!isNaN(num1) && !isNaN(num2)) {
return num1 - num2;
} else {
return strLocaleCompare(str1, str2);
}
function handleNumbers(str1, str2) {
let num1, num2;
num1 = parseNumberFromString(str1);
num2 = parseNumberFromString(str2);

if (!isNaN(num1) && !isNaN(num2)) {
return num1 - num2;
} else {
return strLocaleCompare(str1, str2);
}
}

function sortAscending(a, b) {
if (a.includes(`${fillValue}#`)) {
return 1;
} else if (b.includes(`${fillValue}#`)) {
Expand Down

0 comments on commit 7af7fc0

Please sign in to comment.