@bnidev/js-utils
    Preparing search index...

    Function truncate

    • Truncates a string to a specified maximum length and appends a suffix if truncated.

      Parameters

      • str: string

        The string to truncate.

      • maxLength: number

        Maximum length of the truncated string including suffix.

      • suffix: string = '...'

        String to append after truncation (default: '...').

      Returns string

      The truncated string with suffix if truncated, otherwise the original string.

      // ES Module
      import { truncate } from '@bnidev/js-utils'

      // CommonJS
      const { truncate } = require('@bnidev/js-utils')
      truncate('Hello World', 8) // → 'Hello...'
      truncate('Hello', 10) // → 'Hello'
      truncate('Hello World', 5, '~~') // → 'Hel~~'