Files
2026-02-08 18:28:39 +08:00

35 lines
760 B
JavaScript

const MAX_CHARS_PER_LINE = 45
function normalizeLineBreaks(text) {
return String(text || '').replace(/\r\n?/g, '\n')
}
function wrapTextByChars(text, maxCharsPerLine = MAX_CHARS_PER_LINE) {
if (maxCharsPerLine <= 0) {
return normalizeLineBreaks(text)
}
const normalized = normalizeLineBreaks(text)
const lines = normalized.split('\n')
const wrappedLines = []
for (const line of lines) {
const chars = Array.from(line)
if (!chars.length) {
wrappedLines.push('')
continue
}
for (let i = 0; i < chars.length; i += maxCharsPerLine) {
wrappedLines.push(chars.slice(i, i + maxCharsPerLine).join(''))
}
}
return wrappedLines.join('\n')
}
module.exports = {
MAX_CHARS_PER_LINE,
wrapTextByChars,
}