VDL Plugin SDK

firstNWords

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

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

Returns the first n normalized words from a string.

Word boundaries are determined by the shared words tokenizer, which makes this helper suitable for preview text, normalized identifiers, and compact labels derived from mixed-case or separator-heavy inputs.

When truncation happens, an ellipsis is appended by default. Non-positive and non-finite lengths return an empty string.

Parameters

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

Returns

string

The leading normalized words, optionally suffixed with an ellipsis.

Example

firstNWords("HTTPServer_URL-v2", 2);
// "HTTP Server..."