Skip to content
GitHub

truncate

function in meocord/common Since 4.3.0

truncate(text: string, max: number, ellipsis?: string): string

Shortens 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

NameType
textstring

The text to fit.

maxnumber

The longest result, such as DiscordLimits.embedDescription.

ellipsis?string

What ends a cut text: … unless given.

Returns

string

The text, or its start and the ellipsis.

Throws

  • RangeError when max isn't a whole number, or is shorter than ellipsis.

Examples

TypeScript
declare const summary: string

const embed = new EmbedBuilder().setDescription(truncate(summary, DiscordLimits.embedDescription))