Skip to main content

Truncator

An object used to truncate text, either by characters or words.

Constructor

Signature

def Truncator(
text: Any
) - > null

Parameters

NameTypeDescription
textAnyThe text content to be truncated.

Methods


chars()

@classmethod
def chars(
num: int,
truncate: string = None,
html: boolean = False
) - > string

Return the text truncated to be no longer than the specified number of characters.

Parameters

NameTypeDescription
numintThe maximum number of characters allowed in the output string.
truncatestring = NoneThe string used to notify that the text has been truncated, such as an ellipsis.
htmlboolean = FalseA flag indicating whether to treat the input as HTML and preserve tag integrity during truncation.

Returns

TypeDescription
stringThe truncated string, optionally including an ellipsis or custom replacement string.

words()

@classmethod
def words(
num: int,
truncate: string = None,
html: boolean = False
) - > string

Truncate a string after a certain number of words. truncate specifies what should be used to notify that the string has been truncated, defaulting to ellipsis.

Parameters

NameTypeDescription
numintThe maximum number of words allowed in the output string.
truncatestring = NoneThe string used to notify that the text has been truncated.
htmlboolean = FalseA flag indicating whether to parse the input as HTML to avoid breaking tags.

Returns

TypeDescription
stringThe text truncated to the specified word count, formatted as plain text or HTML.