@supuwoerc/toolkit
    Preparing search index...

    Function truncate

    • 截断字符串并在超长时添加省略号

      Truncate a string and append an ellipsis when it exceeds the specified maximum length.

      规则:

      • str.length 小于或等于 maxLength 则返回原字符串。
      • maxLength 小于或等于 0 则返回空字符串。
      • maxLength 小于等于 ellipsis 的长度,则返回 ellipsis 的前 maxLength 个字符(保证结果长度不超过 maxLength)。
      • 否则返回 str 的前 (maxLength - ellipsis.length) 个字符 + ellipsis

      Rules:

      • If str.length <= maxLength, return original string.
      • If maxLength <= 0, return an empty string.
      • If maxLength <= ellipsis.length, return the first maxLength characters of ellipsis.
      • Otherwise return the first (maxLength - ellipsis.length) characters of str plus ellipsis.

      Parameters

      • str: string

        待处理的字符串 / input string

      • maxLength: number

        最大长度(包含省略号)/ maximum length including ellipsis

      • Optionalellipsis: string = '...'

        省略符,默认为 '...' / ellipsis string, default '...'

      Returns string

      截断并可能包含省略号的字符串 / truncated string possibly with ellipsis

      // truncate('abcdef', 4) -> 'a...'
      // truncate('hello', 10) -> 'hello'