Skip to content

Function: truncate() ​

truncate(value, maxLength): string

Defined in: string.ts:31

Shortens a string to a maximum length, appending an ellipsis (…) if truncated. The maxLength includes the ellipsis character.

Parameters ​

value ​

string

The string to truncate.

maxLength ​

number

The maximum length of the returned string, including the ellipsis.

Returns ​

string

The truncated string, or the original string if it fits within maxLength.

Example ​

ts
truncate('Hello, World!', 8)  // 'Hello, …'
truncate('Hi', 8)             // 'Hi'
truncate('Hello, World!', 1)  // '…'