Function truncate

  • Truncates string if it's longer than the given maximum string length. The last characters of the truncated string are replaced with the omission string which defaults to "...".

    Parameters

    • Optionalstring: string

      The string to truncate.

    • Optionaloptions: {
          length?: number;
          omission?: string;
          separator?: string | RegExp;
      }

      The options object.

      • Optionallength?: number

        The maximum string length.

      • Optionalomission?: string

        The string to indicate text is omitted.

      • Optionalseparator?: string | RegExp

        The separator pattern to truncate to.

    Returns string

    Returns the truncated string.

    4.0.0

    replace

    truncate('hi-diddly-ho there, neighborino')
    // => 'hi-diddly-ho there, neighbo...'

    truncate('hi-diddly-ho there, neighborino', {
    'length': 24,
    'separator': ' '
    })
    // => 'hi-diddly-ho there,...'

    truncate('hi-diddly-ho there, neighborino', {
    'length': 24,
    'separator': /,? +/
    })
    // => 'hi-diddly-ho there...'

    truncate('hi-diddly-ho there, neighborino', {
    'omission': ' [...]'
    })
    // => 'hi-diddly-ho there, neig [...]'