-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (31 loc) · 963 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var GraphemeBreaker = require('grapheme-breaker');
var assign = require('object-assign');
/**
* Extracts a section of a string and returns a new string.
*/
function slice(str, beginSlice, endSlice) {
return GraphemeBreaker.break(str)
.slice(beginSlice, endSlice)
.join('');
}
/**
* 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 "…".
* Similar to https://lodash.com/docs#trunc but doesn't have the separator option yet.
*/
function truncate(str, options) {
options = assign({
length: 24,
omission: '...'
}, options);
if (GraphemeBreaker.countBreaks(str) <= options.length) {
return str;
}
var length = options.length - options.omission.length;
if (length < 0) {
length = 0;
}
return slice(str, 0, length) + options.omission;
}
exports.slice = slice;
exports.truncate = truncate;