Skip to content

firstNChars

Import from @varavel/vdl-plugin-sdk/utils/strings.

function firstNChars(
   str, 
   n, 
   ellipsis?): string;

Returns the first n characters from a string.

When truncation happens, an ellipsis is appended by default so generated labels and previews can communicate that more content exists beyond the returned segment.

Non-positive and non-finite lengths return an empty string.

Parameters

Parameter Type Default value Description
str string undefined Source string to shorten.
n number undefined Maximum number of leading characters to keep.
ellipsis boolean true When true, appends ... if truncation occurs.

Returns

string

The leading character slice, optionally suffixed with an ellipsis.

Example

firstNChars("Hello world", 5);
// "Hello..."