mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
feat(Text): add truncate method for string length limitation
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user