fix: no-op table sort should not modify document

This commit is contained in:
Tom Moor
2024-03-16 12:26:27 -04:00
parent 85c8f83e33
commit 2a30783e72

View File

@@ -119,6 +119,9 @@ export function sortTable({
// remove the header row
const header = table.shift();
// column data before sort
const columnData = table.map((row) => row[index]?.textContent ?? "");
// sort table data based on column at index
table.sort((a, b) => {
if (compareAsText) {
@@ -137,6 +140,13 @@ export function sortTable({
table.reverse();
}
// check if column data changed, if not then do not replace table
if (
columnData.join() === table.map((row) => row[index]?.textContent).join()
) {
return true;
}
// add the header row back
if (header) {
table.unshift(header);