Wordwrap a string with ANSI escape codes
Overall
score
100%
Build a text wrapping utility that intelligently handles long words by minimizing the total number of line breaks when text must be forcibly split.
Your implementation should:
Input: "abc verylongword" with width 10
Option A (start on current line):
"abc verylo"
"ngword"
Total: 2 lines, 1 break in the word
Option B (start on next line):
"abc"
"verylongwo"
"rd"
Total: 3 lines, 1 break in the word
Choose Option A (fewer total lines)Input: "abcdefgh verylongword" with width 10
Option A (start on current line):
"abcdefgh v"
"erylongwor"
"d"
Total: 3 lines, 2 breaks in the word
Option B (start on next line):
"abcdefgh"
"verylongwo"
"rd"
Total: 3 lines, 1 break in the word
Choose Option B (same lines, fewer breaks in word)/**
* Wraps text to the specified column width with optimized line break handling.
*
* @param {string} text - The text to wrap
* @param {number} width - The maximum column width
* @returns {string} The wrapped text with newlines
*/
function wrapText(text, width) {
// Implementation here
}
module.exports = { wrapText };@generates
Provides text wrapping functionality with support for optimized hard wrapping algorithm that minimizes line breaks.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-wrap-ansidocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10