_.truncate([string=''], [options=])
截断string字符串,如果字符串超出了限定的最大值。 被截断的字符串后面会以 omission 代替,omission 默认是 "..."。
参数
[string=''] (string): 要截断的字符串。
[options=] (Object): 选项对象。
[options.length=30] (number): 允许的最大长度。
[options.omission='...'] (string): 超出后的代替字符。
[options.separator] (RegExp|string): 截断点。
返回
(string): Returns the truncated 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 [...]'