Skip to content

Commit

Permalink
Don't truncate in the middle of an emoji
Browse files Browse the repository at this point in the history
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 9c8c597
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Libraries/Utilities/truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9c8c597

Please sign in to comment.