Function truncate

  • Category

    Truncate text to a certain length, optionally appending a suffix when truncated.

    Time complexity: O(n)

    Space complexity: O(n)

    Type Parameters

    • T extends string

    Parameters

    • text: T

      Text to truncate

    • maxLength: number

      Maximum length before text is truncated

    • suffix: string = ''

      Suffix to append if text was truncated

    Returns T | `${string}${typeof suffix}`

    Truncated text

    String

    Example

    truncate('hello, world', 5, '...'); // 'hello...'