-
Notifications
You must be signed in to change notification settings - Fork 6
/
vue-truncate.js
46 lines (33 loc) · 1.03 KB
/
vue-truncate.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
39
40
41
42
43
44
45
46
;(function () {
var vueTruncate = {};
vueTruncate.install = function (Vue) {
/**
*
* @param {String} text
* @param {Number} length
* @param {String} clamp
*
*/
Vue.filter('truncate', function (text, length, clamp) {
text = text || '';
clamp = clamp || '...';
length = length || 30;
if (text.length <= length) return text;
var tcText = text.slice(0, length - clamp.length);
var last = tcText.length - 1;
while (last > 0 && tcText[last] !== ' ' && tcText[last] !== clamp[0]) last -= 1;
// Fix for case when text dont have any `space`
last = last || length - clamp.length;
tcText = tcText.slice(0, last);
return tcText + clamp;
});
}
if (typeof exports == "object") {
module.exports = vueTruncate;
} else if (typeof define == "function" && define.amd) {
define([], function(){ return vueTruncate });
} else if (window.Vue) {
window.VueTruncate = vueTruncate;
Vue.use(VueTruncate);
}
})()