Skip to content

Commit 20f85bb

Browse files
committed
superior algorithm
1 parent 4871e00 commit 20f85bb

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-truncate-filter",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "A filter for VueJs to truncate string",
55
"main": "vue-truncate.js",
66
"repository": {

vue-truncate.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
clamp = clamp || '...';
1717
length = length || 30;
1818

19-
return text.length > length
20-
? text.slice(0, length - clamp.length) + clamp
21-
: text
22-
});
19+
let truncateText = text.slice(0, length - clamp.length)
20+
let posLast = truncateText.length - 1
21+
22+
while (truncateText[posLast] === ' ' || truncateText[posLast] === clamp[0])
23+
--posLast
24+
25+
truncateText = truncateText.slice(0, posLast + 1)
26+
27+
return truncateText + (text.length > length ? clamp : '')
28+
});
2329
}
2430

2531
if (typeof exports == "object") {

0 commit comments

Comments
 (0)