summaryrefslogtreecommitdiff
path: root/utils/string_utils.js
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2026-08-23 14:25:16 +0800
committerlittleblack111 <littleblack11111@gmail.com>2026-08-23 14:25:16 +0800
commit08f000c9c14f34f20ec0b2c77f1741d37f5e9fa7 (patch)
treed5f7029ba2ae4861621bbb697232a2cfc06c8c27 /utils/string_utils.js
parent044e2dfeaf50148ee15fa47fe784d522c930fde9 (diff)
fmt: js
Diffstat (limited to 'utils/string_utils.js')
-rw-r--r--utils/string_utils.js101
1 files changed, 60 insertions, 41 deletions
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, '&amp;')
- .replace(/</g, '&lt;')
- .replace(/>/g, '&gt;')
- .replace(/"/g, '&quot;')
- .replace(/'/g, '&#39;');
+ .replace(/&/g, "&amp;")
+ .replace(/</g, "&lt;")
+ .replace(/>/g, "&gt;")
+ .replace(/"/g, "&quot;")
+ .replace(/'/g, "&#39;");
}