Skip to content

Commit

Permalink
fix: 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 3cd310b commit 94f82e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 68 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +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)|

| <th> Classes that change defaults. | Description |
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
Expand Down
51 changes: 25 additions & 26 deletions public/table-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,32 +329,35 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
const isAlphaSort = th.classList.contains("alpha-sort");
const isNumericSort = th.classList.contains("numeric-sort");
function sortAscending(a, b) {
function handleNumbers(str1, str2) {
let num1, num2;
str1 = str1.slice(0, str1.indexOf("#"));
str2 = str2.slice(0, str2.indexOf("#"));

if (str1.match(/^\((\d+(?:\.\d+)?)\)$/)) {
num1 = -1 * Number(str1.slice(1, -1));
} else {
num1 = Number(str1);
}

if (str2.match(/^\((\d+(?:\.\d+)?)\)$/)) {
num2 = -1 * Number(str2.slice(1, -1));
function parseNumberFromString(str) {
let num;
str = str.slice(0, str.indexOf("#"));
if (str.match(/^\((\d+(?:\.\d+)?)\)$/)) {
num = -1 * Number(str.slice(1, -1));
} else {
num2 = Number(str2);
num = Number(str);
}
return num;
}

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

function handleNumbers(str1, str2) {
let num1, num2;
num1 = parseNumberFromString(str1);
num2 = parseNumberFromString(str2);

if (!isNaN(num1) && !isNaN(num2)) {
return num1 - num2;
} else {
return str1.localeCompare(
str2,
navigator.languages[0] || navigator.language,
{ numeric: !isAlphaSort, ignorePunctuation: !isPunctSort }
);
}
return strLocaleCompare(str1, str2);
}
}

if (a.includes(`${fillValue}#`)) {
Expand All @@ -363,12 +366,8 @@ function tableSortJs(testingTableSortJS = false, domDocumentWindow = document) {
return -1;
} else if (isNumericSort) {
return handleNumbers(a, b);
} else{
return a.localeCompare(
b,
navigator.languages[0] || navigator.language,
{ numeric: !isAlphaSort, ignorePunctuation: !isPunctSort }
);
} else {
return strLocaleCompare(a, b);
}
}

Expand Down
44 changes: 2 additions & 42 deletions test/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,47 +408,7 @@ test("Sort decimal numbers", () => {
});
});

test("Sort combination of negative and positive numbers", () => {
expect(
createTestTable(
{
col0: ["1", "-2", "-3", "4", "-6", "5", "14"],
},
{ classTags: "numeric-sort" }
)
).toStrictEqual({
col0: ["-6","-3","-2","1","4","5","14"],
});
});

test("Sort all combination of negative and positive integers and decimal numbers", () => {
expect(
createTestTable(
{
col0: ["1.05", "-2.3", "-3", "1", "-6", "5", "14"],
},
{ classTags: "numeric-sort" }
)
).toStrictEqual({
col0: ["-6","-3","-2.3","1","1.05","5","14"],
});
});

test("Sort all combination of negative and positive integers and decimal numbers", () => {
expect(
createTestTable(
{
col0: ["1.05", "-2.3", "-3", "1", "-6", "", "14"],
},
{ classTags: "numeric-sort" }
)
).toStrictEqual({
col0: ["-6","-3","-2.3","1","1.05","14",""],
});
});


test("Sort all negative numbers with parenthesis as well", () => {
test("Sort all combination positive, negative numbers with parenthesis as well", () => {
expect(
createTestTable(
{
Expand All @@ -470,6 +430,6 @@ test("Sort all combination of negative and positive integers and decimal numbers
{ classTags: "numeric-sort" }
)
).toStrictEqual({
col0: ["{1}","-6","-3","-2.3","(0.5)","1","1.05","1a","b","(c)",""],
col0: ["-6","-3","-2.3","(0.5)","1","1.05","{1}","1a","b","(c)",""],
});
});

0 comments on commit 94f82e9

Please sign in to comment.