Skip to content

truncate()

truncate(input, maxLength): string

Defined in: src/utils/stringUtils.ts:52

Truncates a string to a specified length, appending '...' if truncated.

Parameters

input

string

The string to truncate.

maxLength

number

The maximum length of the truncated string.

Returns

string

The truncated string. If maxLength is 0 or negative, returns an empty string.

Example

tsx
truncate('This is a long string', 10); // 'This is a...'
truncate('Short', 10); // 'Short'
truncate('', 5); // ''

Released under the MIT License.