Skip to content

@sqlrooms/utils / truncate

Function: truncate()

truncate(text, maxWords): string

Truncates text to a specified word limit

Parameters

ParameterTypeDefault valueDescription
textstringundefinedThe text to truncate
maxWordsnumber10The maximum number of words to keep (default: 10)

Returns

string

The truncated text with "..." appended if truncated

Example

ts
truncate("This is a very long sentence that should be truncated", 5) // returns "This is a very long..."
truncate("Short text", 10) // returns "Short text"