truncate
function in meocord/common Since 4.3.0
truncate(text: string, max: number, ellipsis?: string): stringShortens text to at most max characters, ending it with an ellipsis when it was cut.
Text that fits comes back unchanged. Otherwise it is cut at the last whitespace when one falls in the last fifth of
max, else after the last character that fits, never inside an emoji, a flag or a letter and its accent, so a first
character too long to fit gives the ellipsis alone; whitespace before the ellipsis is dropped. Code is read as
splitMessage reads it: a cut inside code closes it before the ellipsis, its backticks counted in max, and
the result reads as the start of the text does, so a cut moves back before a fence, an escape or a run of backticks
in inline code it would part, and before backticks it would pair.
Parameters
| Name | Type |
|---|---|
text | string |
The text to fit. | |
max | number |
The longest result, such as | |
ellipsis? | string |
What ends a cut text: | |
Returns
stringThe text, or its start and the ellipsis.
Throws
RangeError when
maxisn't a whole number, or is shorter thanellipsis.
Examples
declare const summary: string
const embed = new EmbedBuilder().setDescription(truncate(summary, DiscordLimits.embedDescription))