I need to implement ellipsis (\"...\") in the middle of a text within a resizable element. Here is what it might look like. So,
\"Lorem ipsum do
Here's an elegant solution:
function truncateMiddle(word) {
const tooLongChars = 15; // arbitrary
if (word.length < tooLongChars) {
return word;
}
const ellipsis = '...';
const charsOnEitherSide = Math.floor(tooLongChars / 2) - ellipsis.length;
return word.slice(0, charsOnEitherSide) + ellipsis + word.slice(-charsOnEitherSide);
}