Properly truncate text in Typescript
• words • min read
I’ve often dealt with a situation where I’m trying to display a description in a card but I want to cap the length of that text.
So the first version always look something like text.slice(0, MAX_LENGTH). But then you get this:
— insert image
So I rolled up my sleeves and built a smarter utility:
function truncate(text: string) {}
And here’s what it looks now:
— insert image