Don't truncate in the middle of an emoji

Reviewed By: adiphos, mantong01

Differential Revision: D7198155

fbshipit-source-id: 360955de7ed686170a23b9883058e3137e17b277
This commit is contained in:
Vince Oppedisano 2018-03-12 12:31:45 -07:00 committed by Facebook Github Bot
parent 3002c4eb98
commit 9c8c597000
1 changed files with 5 additions and 1 deletions

View File

@ -30,7 +30,11 @@ const truncate = function(
options = Object.assign({}, defaultOptions, options); options = Object.assign({}, defaultOptions, options);
if (str && str.length && if (str && str.length &&
str.length - options.minDelta + options.elipsis.length >= maxChars) { str.length - options.minDelta + options.elipsis.length >= maxChars) {
str = str.slice(0, maxChars - options.elipsis.length + 1); // If the slice is happening in the middle of a wide char, add one more char
var extraChar = str.charCodeAt(maxChars - options.elipsis.length) > 255
? 1
: 0;
str = str.slice(0, maxChars - options.elipsis.length + 1 + extraChar);
if (options.breakOnWords) { if (options.breakOnWords) {
var ii = Math.max(str.lastIndexOf(' '), str.lastIndexOf('\n')); var ii = Math.max(str.lastIndexOf(' '), str.lastIndexOf('\n'));
str = str.slice(0, ii); str = str.slice(0, ii);