limitString()function

limitString(str: string, maxLength: number, append = "…"): void
ParamType
strstring
The string to limit. required
maxLengthnumber
The maximum length of the returned string (before append is added). required
appendunknown
The string to append when a limit is applied (defaults to "…"). Defaults to "…"
Return
unknown
str unchanged if it's shorter than maxLength, otherwise truncated with append added.

Limit a string to a given length.

  • Stops at the last space inside maxLength
  • Appends an ellipses after the string (but only if a limit is applied).

Examples

limitString("the quick brown fox", 9) // "the quick…"