From f4b0c41c65eb5740bea768ee44db6440b12fb772 Mon Sep 17 00:00:00 2001 From: "Stiliyan Tonev (Bark)" Date: Tue, 10 Jun 2025 14:12:48 +0300 Subject: [PATCH] refact: Coding standard and additional checks * Add checks regarding `totalSize` so we don't get a division by 0 exception (Infinity more like) --- src/webui/www/private/scripts/dynamicTable.js | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/webui/www/private/scripts/dynamicTable.js b/src/webui/www/private/scripts/dynamicTable.js index c5b2e6390..03ffebe40 100644 --- a/src/webui/www/private/scripts/dynamicTable.js +++ b/src/webui/www/private/scripts/dynamicTable.js @@ -1567,33 +1567,39 @@ window.qBittorrent.DynamicTable ??= (() => { } const size = this.getRowValue(row, 1); const totalSize = this.getRowValue(row, 2); - const percent = (size / totalSize) * 100; - if (percent === -1) { + if (totalSize <= 0) { td.textContent = "QBT_TR(N/A)QBT_TR[CONTEXT=TrackerListWidget]"; td.title = "QBT_TR(N/A)QBT_TR[CONTEXT=TrackerListWidget]"; return; } + const percent = (size / totalSize) * 100; const value = `${window.qBittorrent.Misc.toFixedPointString(percent, 2)}%`; td.textContent = value; td.title = value; }; - this.columns["percent_selected"].compareRows = function(row1, row2) { - let percent1 = 0; - let percent2 = 0; - const hasMetadata1 = this.getRowValue(row1, 0); - if (hasMetadata1 !== false) { - const size1 = this.getRowValue(row1, 1); - const totalSize1 = this.getRowValue(row1, 2); - percent1 = (size1 / totalSize1) * 100; - } - const hasMetadata2 = this.getRowValue(row2, 0); - if (hasMetadata2 !== false) { - const size2 = this.getRowValue(row2, 1); - const totalSize2 = this.getRowValue(row2, 2); - percent2 = (size2 / totalSize2) * 100; - } - return compareNumbers(percent1, percent2); + this.columns["percent_selected"].compareRows = function(row1, row2) { + const hasMetadata1 = this.getRowValue(row1, 0); + 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 totalSize1 = this.getRowValue(row1, 2); + if (totalSize1 <= 0) + return 1; + const ratio1 = (size1 / totalSize1) * 100; + + const size2 = this.getRowValue(row2, 1); + const totalSize2 = this.getRowValue(row2, 2); + if (totalSize2 <= 0) + return -1; + const ratio2 = (size2 / totalSize2) * 100; + + return compareNumbers(ratio1, ratio2); }; }