diff --git a/src/utl/wrap-and-indent.mjs b/src/utl/wrap-and-indent.mjs index 051c6d2af..eadcda303 100644 --- a/src/utl/wrap-and-indent.mjs +++ b/src/utl/wrap-and-indent.mjs @@ -7,40 +7,44 @@ function indentString(pString, pCount) { } /** - * + * @param {string} pLine + * @param {number} pMaxWidth + */ +function splitLine(pLine, pMaxWidth) { + const lWords = pLine.split(" "); + const lWrappedLines = []; + let lCurrentLine = ""; + let lCurrentWidth = 0; + + for (const lWord of lWords) { + if (lCurrentWidth + lWord.length > pMaxWidth) { + lWrappedLines.push(lCurrentLine.trimEnd()); + lCurrentLine = ""; + lCurrentWidth = 0; + } + + if (lCurrentLine) { + lCurrentLine += " "; + lCurrentWidth += 1; + } + + lCurrentLine += lWord; + lCurrentWidth += lWord.length; + } + + lWrappedLines.push(lCurrentLine.trimEnd()); + + return lWrappedLines.join("\n"); +} + +/** * @param {string} pString - the string to wrap * @param {number} pMaxWidth - the maximum width of the wrapped string */ function wrapString(pString, pMaxWidth) { - const lLines = pString.split(/\r?\n/); - - return lLines - .map((pLine) => { - const lWords = pLine.split(" "); - const lWrappedLines = []; - let lCurrentLine = ""; - let lCurrentWidth = 0; - - for (const lWord of lWords) { - if (lCurrentWidth + lWord.length > pMaxWidth) { - lWrappedLines.push(lCurrentLine.trimEnd()); - lCurrentLine = ""; - lCurrentWidth = 0; - } - - if (lCurrentLine) { - lCurrentLine += " "; - lCurrentWidth += 1; - } - - lCurrentLine += lWord; - lCurrentWidth += lWord.length; - } - - lWrappedLines.push(lCurrentLine.trimEnd()); - - return lWrappedLines.join("\n"); - }) + return pString + .split(/\r?\n/) + .map((pLine) => splitLine(pLine, pMaxWidth)) .join("\n"); }