Permalink
Browse files

Don't truncate in the middle of an emoji

Reviewed By: adiphos, mantong01

Differential Revision: D7198155

fbshipit-source-id: 360955de7ed686170a23b9883058e3137e17b277
  • Loading branch information...
Vince0613 authored and facebook-github-bot committed Mar 12, 2018
1 parent 3002c4e commit 9c8c5970002d048e8b18088f7c63b39431def50b
Showing with 5 additions and 1 deletion.
  1. +5 −1 Libraries/Utilities/truncate.js
@@ -30,7 +30,11 @@ const truncate = function(
options = Object.assign({}, defaultOptions, options);
if (str && str.length &&
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) {
var ii = Math.max(str.lastIndexOf(' '), str.lastIndexOf('\n'));
str = str.slice(0, ii);

0 comments on commit 9c8c597

Please sign in to comment.