Files
protocol/sites/dev0x-portal/app/hooks/useTruncateMiddle.ts
2023-04-04 21:44:14 +02:00

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}`;
}