• Truncates a string to a specified length and appends ellipsis if needed.

    Parameters

    • str: string

      The string to truncate.

    • length: number

      The maximum length of the string (including ellipsis).

    • ellipsis: string = "…"

      Optional. The ellipsis string to append. Defaults to "…".

    Returns string

    The truncated string with ellipsis if needed.

    Example

    truncate("Hello world", 8); // Returns: "Hello w…"
    truncate("Short", 10); // Returns: "Short"
    truncate("Very long string that needs truncation", 15); // Returns: "Very long str…"
    truncate("Test", 3, "!!!"); // Returns: "!!!"

Generated using TypeDoc