1 | var toString = require('../lang/toString');
|
2 | var trim = require('./trim');
|
3 | |
4 |
|
5 |
|
6 | function truncate(str, maxChars, append, onlyFullWords){
|
7 | str = toString(str);
|
8 | append = append || '...';
|
9 | maxChars = onlyFullWords? maxChars + 1 : maxChars;
|
10 |
|
11 | str = trim(str);
|
12 | if(str.length <= maxChars){
|
13 | return str;
|
14 | }
|
15 | str = str.substr(0, maxChars - append.length);
|
16 |
|
17 | str = onlyFullWords? str.substr(0, str.lastIndexOf(' ')) : trim(str);
|
18 | return str + append;
|
19 | }
|
20 | module.exports = truncate;
|
21 |
|