Trim Text
1 min read
Description
Intelligently truncates text at word boundaries to maintain readability. Unlike basic string truncation that cuts words mid-character, this function finds the last complete word within the specified length and adds ellipsis appropriately. Returns both the processed text and a boolean indicating whether truncation occurred.
Code
const trimText = (
input: string,
length: number = 80,
): [text: string, trimmed: boolean] => {
const trimmed = input.length >= length
const text = trimmed
? `${input.slice(0, input.lastIndexOf(' ', length))}…`
: input
return [text, trimmed]
}
TSUsage
trimText(
'This is some trimmed text that will not cut off half way through a word.',
35,
)
// => ['This is some trimmed text that will…', true]
TS