textConvert
    Preparing search index...

    Function truncate

    • Shortens text to a maximum length, appending an ellipsis when truncation happens. maxLength includes the ellipsis itself.

      Truncation never splits a grapheme cluster (surrogate pairs, combining marks, ZWJ emoji sequences) in half: the cut snaps back to the last cluster boundary that fits the length budget.

      Parameters

      • text: string

        Text to truncate.

      • maxLength: number

        Maximum length of the returned string, including the ellipsis.

      • options: { byWords?: boolean; ellipsis?: string } = {}
        • OptionalbyWords?: boolean

          When true, truncates at the last full word instead of cutting mid-word. Default is false.

        • Optionalellipsis?: string

          String appended when the text is truncated. Default is '...'.

      Returns string

      The truncated string, or the original string unchanged if it's already within maxLength.

      truncate('The quick brown fox jumps over the lazy dog', 20); // 'The quick brown f...'
      truncate('The quick brown fox jumps over the lazy dog', 20, { byWords: true }); // 'The quick brown...'
      truncate('Short text', 20); // 'Short text'