Truncates a string to the specified length and adds a suffix if truncated.
Length is measured in code points, so astral characters (emoji, rare CJK) are never
cut in half, which would leave a lone surrogate. Only the retained prefix is walked, so
truncating a very large string to a small length costs O(length), not O(input length).
Parameters
str: string
The string to truncate
length: number
Maximum length of the truncated string including the suffix
suffix: string = '...'
The suffix to add to truncated strings (default: '...')
Truncates a string to the specified length and adds a suffix if truncated.
Length is measured in code points, so astral characters (emoji, rare CJK) are never cut in half, which would leave a lone surrogate. Only the retained prefix is walked, so truncating a very large string to a small length costs O(length), not O(input length).