add JSDoc to string_utils

This commit is contained in:
Greyfeather
2025-05-18 18:35:38 -06:00
parent a78b56c450
commit c5b5cf4d2c
@@ -1,19 +1,40 @@
/**
* Formats a string according to the args that are passed in
* @param { string } str
* @param {...any} args
* @returns
*/
function format(str, ...args) { function format(str, ...args) {
return str.replace(/{(\d+)}/g, (match, index) => 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 }
*/
function getDomain(url) { function getDomain(url) {
const match = url.match(/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/); const match = url.match(/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/);
return match ? match[1] : null; return match ? match[1] : null;
} }
/**
* Returns the base path of the passed in url or null
* @param { string } url
* @returns { string | null }
*/
function getBaseUrl(url) { function getBaseUrl(url) {
const match = url.match(/^(https?:\/\/[^\/]+)(\/.*)?$/); const match = url.match(/^(https?:\/\/[^\/]+)(\/.*)?$/);
return match ? match[1] : null; return match ? match[1] : null;
} }
/**
* Escapes single quotes
* @param { string } str
* @returns { string }
*/
function shellSingleQuoteEscape(str) { function shellSingleQuoteEscape(str) {
// escape single quotes // escape single quotes
return String(str) return String(str)
@@ -21,8 +42,15 @@ function shellSingleQuoteEscape(str) {
.replace(/'/g, "'\\''"); .replace(/'/g, "'\\''");
} }
/**
* Splits markdown Blocks
* @param { string } markdown
*/
function splitMarkdownBlocks(markdown) { function splitMarkdownBlocks(markdown) {
const regex = /```(\w+)?\n([\s\S]*?)```|<think>([\s\S]*?)<\/think>/g; const regex = /```(\w+)?\n([\s\S]*?)```|<think>([\s\S]*?)<\/think>/g;
/**
* @type {{type: "text" | "think" | "code"; content: string; lang: string | undefined; completed: boolean | undefined}[]}
*/
let result = []; let result = [];
let lastIndex = 0; let lastIndex = 0;
let match; let match;
@@ -89,10 +117,21 @@ function splitMarkdownBlocks(markdown) {
return result; return result;
} }
/**
* Returns the original string with backslashes escaped
* @param { string } str
* @returns { string }
*/
function escapeBackslashes(str) { function escapeBackslashes(str) {
return str.replace(/\\/g, '\\\\'); return str.replace(/\\/g, '\\\\');
} }
/**
* Wraps words to supplied maximum length
* @param { string | null } str
* @param { number } maxLen
* @returns { string }
*/
function wordWrap(str, maxLen) { function wordWrap(str, maxLen) {
if (!str) return ""; if (!str) return "";
let words = str.split(" "); let words = str.split(" ");