78532bc7bb
Co-authored-by: Dennis Simon <dennism.simon94@gmail.com>
15 lines
407 B
TypeScript
15 lines
407 B
TypeScript
|
|
export function useTruncateMiddle({ text, maxLength }: {
|
|
text: string;
|
|
maxLength: number;
|
|
}) {
|
|
if (!text || text.length <= maxLength || maxLength < 3) { return text; }
|
|
|
|
const charsLeft = Math.floor((maxLength - 1) / 2);
|
|
const charsRight = maxLength - 1 - charsLeft;
|
|
const start = text.slice(0, charsLeft);
|
|
const end = text.slice(-charsRight);
|
|
return `${start}…${end}`;
|
|
}
|
|
|