src / engine / normalization / TextNormalizer.ts
/**
* Pure helper utility for text whitespace normalization and boundary truncation.
*/
export class TextNormalizer {
/**
* Collapses multiple consecutive spaces, tabs, and newlines into single spaces.
*/
public static normalizeWhitespace(str: string): string {
if (!str) return "";
return str.replace(/\s+/g, " ").trim();
}
/**
* Enforces character length truncation on string input.
* Guarantees that the returned string's total character length never exceeds `maxLength`.
* If truncated and `maxLength > 3`, appends an ellipsis ("...") within the `maxLength` budget.
*
* @param str Raw string input.
* @param maxLength Maximum allowed total length of the returned string.
* @returns Truncated string strictly <= `maxLength` characters.
*/
public static truncate(str: string, maxLength: number): string {
if (!str || maxLength <= 0) return "";
const clean = str.trim();
if (clean.length <= maxLength) {
return clean;
}
if (maxLength <= 3) {
return clean.substring(0, maxLength);
}
return clean.substring(0, maxLength - 3).trim() + "...";
}
}
src / engine / normalization / TextNormalizer.ts
/**
* Pure helper utility for text whitespace normalization and boundary truncation.
*/
export class TextNormalizer {
/**
* Collapses multiple consecutive spaces, tabs, and newlines into single spaces.
*/
public static normalizeWhitespace(str: string): string {
if (!str) return "";
return str.replace(/\s+/g, " ").trim();
}
/**
* Enforces character length truncation on string input.
* Guarantees that the returned string's total character length never exceeds `maxLength`.
* If truncated and `maxLength > 3`, appends an ellipsis ("...") within the `maxLength` budget.
*
* @param str Raw string input.
* @param maxLength Maximum allowed total length of the returned string.
* @returns Truncated string strictly <= `maxLength` characters.
*/
public static truncate(str: string, maxLength: number): string {
if (!str || maxLength <= 0) return "";
const clean = str.trim();
if (clean.length <= maxLength) {
return clean;
}
if (maxLength <= 3) {
return clean.substring(0, maxLength);
}
return clean.substring(0, maxLength - 3).trim() + "...";
}
}