truncate source npm

_.truncate([string=''], [options])

截断字符串,如果字符串超出了限定的最大值。 被截断的字符串后面会以 omission 代替,omission 默认是 "..."。

参数

  1. [string=''] (string)

    要截断的字符串

  2. [options] (Object)

    选项对象

  3. [options.length=30] (number)

    允许的最大长度

  4. [options.omission='...'] (string)

    超出后的代替字符

  5. [options.separator] (RegExp|string)

    截断点

返回值 (string)

返回截断后的字符串

示例

_.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 [...]'