TS-Scribe - v0.6.2
    Preparing search index...

    Function truncateString

    • Truncates a string to a specified maximum length, optionally adding an ellipsis and preserving whole words when truncating.

      Parameters

      • text: string

        The string to truncate.

      • maxLength: number

        The maximum length of the string, including the ellipsis.

      • Optionaloptions: { ellipsis?: string; preserveWords?: boolean }

        Optional configuration for truncation.

        • Optionalellipsis?: string

          The string to append to indicate truncation. Default is '...'.

        • OptionalpreserveWords?: boolean

          Whether to preserve whole words when truncating. Default is false.

      Returns string

      The truncated string with an ellipsis if necessary, or the original string if it's within the length limit.

      If maxLength is less than or equal to the length of the ellipsis.

      truncateString('This is a long string that should be truncated', 20); // "This is a long..."
      truncateString('This is a long string that should be truncated', 20, { preserveWords: true }); // "This is a long..."
      truncateString('Short text', 20); // "Short text"
      truncateString('Short text', 5); // "Short..."