• Truncates a string to a maximum length.

    suffix length does not influence maxLength.

    If separator is provided, it will look for the closest separator to truncate to

    Example

    Truncate to 3 characters

    const truncateTo3 = truncate(3);
    const s = truncateTo3("12345");
    console.log(s); // '123'

    Example

    Truncate to 3 characters, and add "..." suffix

    const truncateTo3WithSuffix = truncate(3, '...');
    const s = truncateTo3WithSuffix("12345");
    console.log(s); // '123...'

    Example

    Truncate to the closest space (" ") up to 9 characters

    const truncateTo9AtSpace = truncate(9, "", " ");
    const s = truncateTo9AtSpace("123 45 6 789");
    console.log(s); // '123 45 6'

    Returns

    the truncated String

    Parameters

    • maxLength: number
    • suffix: string = ""
    • separator: string = ""

    Returns ((str: string, length?: number) => string)

      • (str: string, length?: number): string
      • Parameters

        • str: string
        • length: number = str.length

        Returns string

Generated using TypeDoc