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:
parent
3002c4eb98
commit
9c8c597000
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue