diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2026-08-23 14:25:16 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2026-08-23 14:25:16 +0800 |
| commit | 08f000c9c14f34f20ec0b2c77f1741d37f5e9fa7 (patch) | |
| tree | d5f7029ba2ae4861621bbb697232a2cfc06c8c27 | |
| parent | 044e2dfeaf50148ee15fa47fe784d522c930fde9 (diff) | |
fmt: js
| -rw-r--r-- | utils/networkUsage.js | 32 | ||||
| -rw-r--r-- | utils/string_utils.js | 101 |
2 files changed, 77 insertions, 56 deletions
diff --git a/utils/networkUsage.js b/utils/networkUsage.js index 221e7af..4d33074 100644 --- a/utils/networkUsage.js +++ b/utils/networkUsage.js @@ -1,30 +1,32 @@ function defaultInterface(route) { - var lines = route.split(/\r?\n/) + var lines = route.split(/\r?\n/); for (var i = 0; i < lines.length; i++) { - var l = lines[i] - if (l.indexOf("default via") === 0) return l.split(/\s+/)[4] + var l = lines[i]; + if (l.indexOf("default via") === 0) return l.split(/\s+/)[4]; } - throw "No default interface" + throw "No default interface"; } function getRxTxBytes(dev, iface) { - var lines = dev.split(/\r?\n/) + var lines = dev.split(/\r?\n/); for (var i = 0; i < lines.length; i++) { - var l = lines[i].trim() - if (!l.startsWith(iface + ":")) continue - var p = l.split(/\s+/) - return { rx: parseInt(p[1]), tx: parseInt(p[9]) } + var l = lines[i].trim(); + if (!l.startsWith(iface + ":")) continue; + var p = l.split(/\s+/); + return { rx: parseInt(p[1]), tx: parseInt(p[9]) }; } - throw "Interface not found" + throw "Interface not found"; } function formatSize(size, unit) { - const labels = ["B", "K", "M", "G", "T"] - let power = 0, kilo = 2 ** 10 + const labels = ["B", "K", "M", "G", "T"]; + let power = 0, + kilo = 2 ** 10; while (size > kilo && power < labels.length - 1) { - size /= kilo; power++ + size /= kilo; + power++; } if (unit) { - return labels[power] + return labels[power]; } else { - return size.toFixed(0) + return size.toFixed(0); } } diff --git a/utils/string_utils.js b/utils/string_utils.js index e0bf8ef..2e15f4b 100644 --- a/utils/string_utils.js +++ b/utils/string_utils.js @@ -1,19 +1,19 @@ /** * Formats a string according to the args that are passed in - * @param { string } str - * @param {...any} args - * @returns + * @param { string } str + * @param {...any} args + * @returns */ function format(str, ...args) { return str.replace(/{(\d+)}/g, (match, index) => - typeof args[index] !== 'undefined' ? args[index] : match + typeof args[index] !== "undefined" ? args[index] : match, ); } /** * Returns the domain of the passed in url or null - * @param { string } url - * @returns { string| null } + * @param { string } url + * @returns { string| null } */ function getDomain(url) { const match = url.match(/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/); @@ -22,8 +22,8 @@ function getDomain(url) { /** * Returns the base url of the passed in url or null - * @param { string } url - * @returns { string | null } + * @param { string } url + * @returns { string | null } */ function getBaseUrl(url) { const match = url.match(/^(https?:\/\/[^\/]+)(\/.*)?$/); @@ -32,19 +32,21 @@ function getBaseUrl(url) { /** * Escapes single quotes in shell commands - * @param { string } str + * @param { string } str * @returns { string } */ function shellSingleQuoteEscape(str) { // escape single quotes - return String(str) - // .replace(/\\/g, '\\\\') - .replace(/'/g, "'\\''"); + return ( + String(str) + // .replace(/\\/g, '\\\\') + .replace(/'/g, "'\\''") + ); } /** * Splits markdown blocks into three different types: text, think, and code. - * @param { string } markdown + * @param { string } markdown */ function splitMarkdownBlocks(markdown) { const regex = /```(\w+)?\n([\s\S]*?)```|<think>([\s\S]*?)<\/think>/g; @@ -61,13 +63,22 @@ function splitMarkdownBlocks(markdown) { result.push({ type: "text", content: text }); } } - if (match[0].startsWith('```')) { + if (match[0].startsWith("```")) { if (match[2] && match[2].trim()) { - result.push({ type: "code", lang: match[1] || "", content: match[2], completed: true }); + result.push({ + type: "code", + lang: match[1] || "", + content: match[2], + completed: true, + }); } - } else if (match[0].startsWith('<think>')) { + } else if (match[0].startsWith("<think>")) { if (match[3] && match[3].trim()) { - result.push({ type: "think", content: match[3], completed: true }); + result.push({ + type: "think", + content: match[3], + completed: true, + }); } } lastIndex = regex.lastIndex; @@ -76,19 +87,20 @@ function splitMarkdownBlocks(markdown) { if (lastIndex < markdown.length) { const text = markdown.slice(lastIndex); // Check for unfinished <think> block - const thinkStart = text.indexOf('<think>'); - const codeStart = text.indexOf('```'); - if ( - thinkStart !== -1 && - (codeStart === -1 || thinkStart < codeStart) - ) { + const thinkStart = text.indexOf("<think>"); + const codeStart = text.indexOf("```"); + if (thinkStart !== -1 && (codeStart === -1 || thinkStart < codeStart)) { const beforeThink = text.slice(0, thinkStart); if (beforeThink.trim()) { result.push({ type: "text", content: beforeThink }); } const thinkContent = text.slice(thinkStart + 7); if (thinkContent.trim()) { - result.push({ type: "think", content: thinkContent, completed: false }); + result.push({ + type: "think", + content: thinkContent, + completed: false, + }); } } else if (codeStart !== -1) { const beforeCode = text.slice(0, codeStart); @@ -102,12 +114,17 @@ function splitMarkdownBlocks(markdown) { if (codeLangMatch) { lang = codeLangMatch[1] || ""; codeContentStart += codeLangMatch[0].length; - } else if (text[codeStart + 3] === '\n') { + } else if (text[codeStart + 3] === "\n") { codeContentStart += 1; } const codeContent = text.slice(codeContentStart); if (codeContent.trim()) { - result.push({ type: "code", lang, content: codeContent, completed: false }); + result.push({ + type: "code", + lang, + content: codeContent, + completed: false, + }); } } else if (text.trim()) { result.push({ type: "text", content: text }); @@ -119,17 +136,17 @@ function splitMarkdownBlocks(markdown) { /** * Returns the original string with backslashes escaped - * @param { string } str + * @param { string } str * @returns { string } */ function escapeBackslashes(str) { - return str.replace(/\\/g, '\\\\'); + return str.replace(/\\/g, "\\\\"); } /** * Wraps words to supplied maximum length - * @param { string | null } str - * @param { number } maxLen + * @param { string | null } str + * @param { number } maxLen * @returns { string } */ function wordWrap(str, maxLen) { @@ -138,7 +155,10 @@ function wordWrap(str, maxLen) { let lines = []; let current = ""; for (let i = 0; i < words.length; ++i) { - if ((current + (current.length > 0 ? " " : "") + words[i]).length > maxLen) { + if ( + (current + (current.length > 0 ? " " : "") + words[i]).length > + maxLen + ) { if (current.length > 0) lines.push(current); current = words[i]; } else { @@ -150,8 +170,7 @@ function wordWrap(str, maxLen) { } function cleanMusicTitle(title) { - if (!title) - return ""; + if (!title) return ""; title = title.replace(/\s*\([^)]*\)/g, " "); title = title.replace(/\s*\[[^\]]*\]/g, " "); title = title.replace(/\s*\{[^\}]*\}/g, " "); @@ -169,18 +188,18 @@ function friendlyTimeForSeconds(seconds) { const m = Math.floor((seconds % 3600) / 60); const s = seconds % 60; if (h > 0) { - return `${h}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`; + return `${h}:${m.toString().padStart(2, "0")}:${s.toString().padStart(2, "0")}`; } else { - return `${m}:${s.toString().padStart(2, '0')}`; + return `${m}:${s.toString().padStart(2, "0")}`; } } function escapeHtml(str) { - if (typeof str !== 'string') return str; + if (typeof str !== "string") return str; return str - .replace(/&/g, '&') - .replace(/</g, '<') - .replace(/>/g, '>') - .replace(/"/g, '"') - .replace(/'/g, '''); + .replace(/&/g, "&") + .replace(/</g, "<") + .replace(/>/g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); } |
