truncate
Truncates a string to maxLength characters, appending an ellipsis when cut.
The ellipsis counts toward maxLength, so the result is always at most
maxLength characters long. If the string is already within the limit, it
is returned unchanged (no ellipsis appended). null and undefined inputs
are returned as-is to align with other string helpers.
Available since v2.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
input | undefined | The string to truncate. |
maxLength | number | Maximum number of characters in the output (including ellipsis). |
ellipsis | string | Appended when the string is cut. Defaults to '…'. (optional) |
Returns
Section titled “Returns”undefined — The (possibly truncated) string, or the input itself when null/undefined.
Examples
Section titled “Examples”Truncate with default ellipsis
Section titled “Truncate with default ellipsis”Appends … when the string exceeds the limit.
Truncate with custom ellipsis
Section titled “Truncate with custom ellipsis”The ellipsis counts toward the maxLength.
String within limit
Section titled “String within limit”Returned unchanged when already short enough.
