mirror of https://github.com/openclaw/openclaw.git
24 lines
675 B
TypeScript
24 lines
675 B
TypeScript
export function sanitizeForConsole(text: string | undefined, maxChars = 200): string | undefined {
|
|
const trimmed = text?.trim();
|
|
if (!trimmed) {
|
|
return undefined;
|
|
}
|
|
const withoutControlChars = Array.from(trimmed)
|
|
.filter((char) => {
|
|
const code = char.charCodeAt(0);
|
|
return !(
|
|
code <= 0x08 ||
|
|
code === 0x0b ||
|
|
code === 0x0c ||
|
|
(code >= 0x0e && code <= 0x1f) ||
|
|
code === 0x7f
|
|
);
|
|
})
|
|
.join("");
|
|
const sanitized = withoutControlChars
|
|
.replace(/[\r\n\t]+/g, " ")
|
|
.replace(/\s+/g, " ")
|
|
.trim();
|
|
return sanitized.length > maxChars ? `${sanitized.slice(0, maxChars)}…` : sanitized;
|
|
}
|