feat(Text): add truncate method for string length limitation

This commit is contained in:
Nawaz Dhandala
2025-11-11 21:32:57 +00:00
parent 40ca9dc04c
commit 9fdf46889c

View File

@@ -297,4 +297,19 @@ export default class Text {
): string {
return sentence.split(search).join(replaceBy);
}
public static truncate(
value: string | null | undefined,
maxLength: number,
): string | undefined {
if (value === null || value === undefined) {
return undefined;
}
if (maxLength <= 0) {
return "";
}
return value.length > maxLength ? value.slice(0, maxLength) : value;
}
}