281 lines
8.8 KiB
JavaScript
Raw Normal View History

2022-10-18 22:39:32 -04:00
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014 Gabriele <pmzqla.git@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
"use strict";
2022-10-18 22:39:32 -04:00
window.qBittorrent ??= {};
window.qBittorrent.Misc ??= (() => {
const exports = () => {
2022-10-18 22:39:32 -04:00
return {
genHash: genHash,
getHost: getHost,
createDebounceHandler: createDebounceHandler,
2022-10-18 22:39:32 -04:00
friendlyUnit: friendlyUnit,
friendlyDuration: friendlyDuration,
friendlyPercentage: friendlyPercentage,
friendlyFloat: friendlyFloat,
parseHtmlLinks: parseHtmlLinks,
parseVersion: parseVersion,
2022-10-18 22:39:32 -04:00
escapeHtml: escapeHtml,
naturalSortCollator: naturalSortCollator,
2022-10-18 22:39:32 -04:00
safeTrim: safeTrim,
toFixedPointString: toFixedPointString,
containsAllTerms: containsAllTerms,
sleep: sleep,
// variables
FILTER_INPUT_DELAY: 400,
2022-10-18 22:39:32 -04:00
MAX_ETA: 8640000
};
};
const genHash = function(string) {
// origins:
// https://stackoverflow.com/a/8831937
// https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0
let hash = 0;
for (let i = 0; i < string.length; ++i)
hash = ((Math.imul(hash, 31) + string.charCodeAt(i)) | 0);
return hash;
};
// getHost emulate the GUI version `QString getHost(const QString &url)`
const getHost = function(url) {
// We want the hostname.
// If failed to parse the domain, original input should be returned
if (!/^(?:https?|udp):/i.test(url))
return url;
try {
// hack: URL can not get hostname from udp protocol
const parsedUrl = new URL(url.replace(/^udp:/i, "https:"));
// host: "example.com:8443"
// hostname: "example.com"
const host = parsedUrl.hostname;
if (!host)
return url;
return host;
}
catch (error) {
return url;
}
};
const createDebounceHandler = (delay, func) => {
let timer = -1;
return (...params) => {
clearTimeout(timer);
timer = setTimeout(() => {
func(...params);
timer = -1;
}, delay);
};
};
2022-10-18 22:39:32 -04:00
/*
* JS counterpart of the function in src/misc.cpp
*/
const friendlyUnit = function(value, isSpeed) {
const units = [
"B",
"KiB",
"MiB",
"GiB",
"TiB",
"PiB",
"EiB"
2022-10-18 22:39:32 -04:00
];
if ((value === undefined) || (value === null) || (value < 0))
return "Unknown";
2022-10-18 22:39:32 -04:00
let i = 0;
while ((value >= 1024.0) && (i < 6)) {
2022-10-18 22:39:32 -04:00
value /= 1024.0;
++i;
}
function friendlyUnitPrecision(sizeUnit) {
if (sizeUnit <= 2) // KiB, MiB
return 1;
else if (sizeUnit === 3) // GiB
return 2;
else // TiB, PiB, EiB
return 3;
}
let ret;
if (i === 0) {
2022-10-18 22:39:32 -04:00
ret = value + " " + units[i];
}
2022-10-18 22:39:32 -04:00
else {
const precision = friendlyUnitPrecision(i);
const offset = Math.pow(10, precision);
// Don't round up
ret = (Math.floor(offset * value) / offset).toFixed(precision) + " " + units[i];
}
if (isSpeed)
ret += "/s";
2022-10-18 22:39:32 -04:00
return ret;
};
/*
* JS counterpart of the function in src/misc.cpp
*/
const friendlyDuration = function(seconds, maxCap = -1) {
if ((seconds < 0) || ((seconds >= maxCap) && (maxCap >= 0)))
2022-10-18 22:39:32 -04:00
return "∞";
if (seconds === 0)
return "0";
if (seconds < 60)
return "< 1m";
2022-10-18 22:39:32 -04:00
let minutes = seconds / 60;
if (minutes < 60)
return "%1m".replace("%1", Math.floor(minutes));
2022-10-18 22:39:32 -04:00
let hours = minutes / 60;
minutes %= 60;
2022-10-18 22:39:32 -04:00
if (hours < 24)
return "%1h %2m".replace("%1", Math.floor(hours)).replace("%2", Math.floor(minutes));
2022-10-18 22:39:32 -04:00
let days = hours / 24;
hours %= 24;
2022-10-18 22:39:32 -04:00
if (days < 365)
return "%1d %2h".replace("%1", Math.floor(days)).replace("%2", Math.floor(hours));
2022-10-18 22:39:32 -04:00
const years = days / 365;
days %= 365;
return "%1y %2d".replace("%1", Math.floor(years)).replace("%2", Math.floor(days));
2022-10-18 22:39:32 -04:00
};
const friendlyPercentage = function(value) {
let percentage = (value * 100).round(1);
if (isNaN(percentage) || (percentage < 0))
percentage = 0;
if (percentage > 100)
percentage = 100;
return percentage.toFixed(1) + "%";
};
const friendlyFloat = function(value, precision) {
return parseFloat(value).toFixed(precision);
};
/*
* JS counterpart of the function in src/misc.cpp
*/
const parseHtmlLinks = function(text) {
const exp = /(\b(https?|ftp|file):\/\/[-\w+&@#/%?=~|!:,.;]*[-\w+&@#/%=~|])/gi;
2022-10-18 22:39:32 -04:00
return text.replace(exp, "<a target='_blank' rel='noopener noreferrer' href='$1'>$1</a>");
};
const parseVersion = function(versionString) {
const failure = {
valid: false
};
if (typeof versionString !== "string")
return failure;
const tryToNumber = (str) => {
const num = Number(str);
return (isNaN(num) ? str : num);
};
const ver = versionString.split(".", 4).map(val => tryToNumber(val));
return {
valid: true,
major: ver[0],
minor: ver[1],
fix: ver[2],
patch: ver[3]
};
2022-10-18 22:39:32 -04:00
};
const escapeHtml = (() => {
const div = document.createElement("div");
return (str) => {
div.textContent = str;
return div.innerHTML;
};
})();
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#parameters
const naturalSortCollator = new Intl.Collator(undefined, { numeric: true, usage: "sort" });
2022-10-18 22:39:32 -04:00
const safeTrim = function(value) {
try {
return value.trim();
}
catch (e) {
if (e instanceof TypeError)
return "";
throw e;
}
};
const toFixedPointString = function(number, digits) {
// Do not round up number
const power = Math.pow(10, digits);
return (Math.floor(power * number) / power).toFixed(digits);
};
/**
*
* @param {String} text the text to search
* @param {Array<String>} terms terms to search for within the text
* @returns {Boolean} true if all terms match the text, false otherwise
*/
const containsAllTerms = function(text, terms) {
const textToSearch = text.toLowerCase();
return terms.every((term) => {
const isTermRequired = (term[0] === "+");
const isTermExcluded = (term[0] === "-");
2022-10-18 22:39:32 -04:00
if (isTermRequired || isTermExcluded) {
// ignore lonely +/-
if (term.length === 1)
return true;
term = term.substring(1);
}
const textContainsTerm = (textToSearch.indexOf(term) !== -1);
return isTermExcluded ? !textContainsTerm : textContainsTerm;
});
};
const sleep = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
2022-10-18 22:39:32 -04:00
return exports();
})();
Object.freeze(window.qBittorrent.Misc);