Skip to content

@sqlrooms/utils / shorten

Function: shorten()

shorten(str, maxLength): string

Shortens a string to a specified maximum length by truncating and adding an ellipsis.

Parameters

ParameterTypeDefault valueDescription
strstringundefinedThe string to shorten
maxLengthnumber10Maximum length of the resulting string (including ellipsis). Defaults to 10.

Returns

string

The shortened string with ellipsis if truncated, or the original string if shorter than maxLength

Example

ts
shorten("Hello World", 8) // Returns "Hello..."
shorten("Hi", 8) // Returns "Hi"