refact: Coding standard and additional checks

* Add checks regarding `totalSize` so we don't get a division by 0 exception (Infinity more like)
This commit is contained in:
Stiliyan Tonev (Bark) 2025-06-10 14:12:48 +03:00
commit f4b0c41c65

View file

@ -1567,33 +1567,39 @@ window.qBittorrent.DynamicTable ??= (() => {
} }
const size = this.getRowValue(row, 1); const size = this.getRowValue(row, 1);
const totalSize = this.getRowValue(row, 2); const totalSize = this.getRowValue(row, 2);
const percent = (size / totalSize) * 100; if (totalSize <= 0) {
if (percent === -1) {
td.textContent = "QBT_TR(N/A)QBT_TR[CONTEXT=TrackerListWidget]"; td.textContent = "QBT_TR(N/A)QBT_TR[CONTEXT=TrackerListWidget]";
td.title = "QBT_TR(N/A)QBT_TR[CONTEXT=TrackerListWidget]"; td.title = "QBT_TR(N/A)QBT_TR[CONTEXT=TrackerListWidget]";
return; return;
} }
const percent = (size / totalSize) * 100;
const value = `${window.qBittorrent.Misc.toFixedPointString(percent, 2)}%`; const value = `${window.qBittorrent.Misc.toFixedPointString(percent, 2)}%`;
td.textContent = value; td.textContent = value;
td.title = value; td.title = value;
}; };
this.columns["percent_selected"].compareRows = function(row1, row2) { this.columns["percent_selected"].compareRows = function(row1, row2) {
let percent1 = 0;
let percent2 = 0;
const hasMetadata1 = this.getRowValue(row1, 0); const hasMetadata1 = this.getRowValue(row1, 0);
if (hasMetadata1 !== false) { const hasMetadata2 = this.getRowValue(row2, 0);
if (hasMetadata1 === true && hasMetadata2 === false)
return -1;
if (hasMetadata1 === false && hasMetadata2 === true)
return 1;
const size1 = this.getRowValue(row1, 1); const size1 = this.getRowValue(row1, 1);
const totalSize1 = this.getRowValue(row1, 2); const totalSize1 = this.getRowValue(row1, 2);
percent1 = (size1 / totalSize1) * 100; if (totalSize1 <= 0)
} return 1;
const hasMetadata2 = this.getRowValue(row2, 0); const ratio1 = (size1 / totalSize1) * 100;
if (hasMetadata2 !== false) {
const size2 = this.getRowValue(row2, 1); const size2 = this.getRowValue(row2, 1);
const totalSize2 = this.getRowValue(row2, 2); const totalSize2 = this.getRowValue(row2, 2);
percent2 = (size2 / totalSize2) * 100; if (totalSize2 <= 0)
} return -1;
const ratio2 = (size2 / totalSize2) * 100;
return compareNumbers(percent1, percent2); return compareNumbers(ratio1, ratio2);
}; };
} }