VDL Plugin SDK

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

ParameterTypeDefault valueDescription
strstringundefinedSource string to shorten.
nnumberundefinedMaximum number of leading characters to keep.
ellipsis?booleantrueWhen true, appends ... if truncation occurs.

Returns

string

The leading character slice, optionally suffixed with an ellipsis.

Example

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