Skip to content

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

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

Returns

string

The leading normalized words, optionally suffixed with an ellipsis.

Example

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