Skip to content

dependabit / utils/src / truncate

Function: truncate() ​

ts
function truncate(
   str, 
   maxLength, 
   suffix?): string;

Defined in: packages/utils/src/string.ts:65

Truncates a string to a maximum length, appending a suffix if truncated.

Parameters ​

ParameterTypeDefault valueDescription
strstringundefinedThe string to truncate.
maxLengthnumberundefinedMaximum total output length, including the suffix.
suffixstring'...'Suffix to append when truncation occurs.

Returns ​

string

The truncated string.

Default Value ​

suffix = '...'

Example ​

ts
truncate('Hello World', 8); // 'Hello...'

Released under the MIT License.