textConvert
    Preparing search index...

    Function truncate

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

      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'