Skip to content

Commit 9c8c597

Browse files
Vince0613facebook-github-bot
authored andcommitted
Don't truncate in the middle of an emoji
Reviewed By: adiphos, mantong01 Differential Revision: D7198155 fbshipit-source-id: 360955de7ed686170a23b9883058e3137e17b277
1 parent 3002c4e commit 9c8c597

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Libraries/Utilities/truncate.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ const truncate = function(
3030
options = Object.assign({}, defaultOptions, options);
3131
if (str && str.length &&
3232
str.length - options.minDelta + options.elipsis.length >= maxChars) {
33-
str = str.slice(0, maxChars - options.elipsis.length + 1);
33+
// If the slice is happening in the middle of a wide char, add one more char
34+
var extraChar = str.charCodeAt(maxChars - options.elipsis.length) > 255
35+
? 1
36+
: 0;
37+
str = str.slice(0, maxChars - options.elipsis.length + 1 + extraChar);
3438
if (options.breakOnWords) {
3539
var ii = Math.max(str.lastIndexOf(' '), str.lastIndexOf('\n'));
3640
str = str.slice(0, ii);

0 commit comments

Comments
 (0)