A module of trim multibyte string by character width. This will work even if contained Unicode, emoji, and surrogate pairs.
$ npm install --save mb-trimwidth
import { mbTrimWidth } from 'mb-trimwidth';
const str1 = 'I love ๐๐ and ๐';
str1.length; // => 18
mbTrimWidth(str1, 9);
// => "I love ๐๐"
mbTrimWidth(str1, 9, 'โฆ');
// => "I love ๐โฆ"
const str2 = '๐ใฎๅคใฏ๐ ฎท้ๅฑใง๐ฉธฝใ้ฃในใใ๐';
str2.length; // => 19
mbTrimWidth(str2, 9);
// => "๐ใฎๅคใฏ๐ ฎท้ๅฑใง๐ฉธฝ"
mbTrimWidth(str2, 9, 'โฆ');
// => "๐ใฎๅคใฏ๐ ฎท้ๅฑใงโฆ"
import { mbTrimWidth } from 'mb-trimwidth';
const str = 'Lorem ipsum dolor sit amet';
mbTrimWidth(str, 10);
// => "Lorem ipsu"
mbTrimWidth(str, 10, 'โฆ');
// => "Lorem ipsโฆ"
mbTrimWidth(str, 0);
// => ""
mbTrimWidth(str, 1, 'โฆ');
// => "L"
mbTrimWidth(str, 1, '...');
// => "L"
const str = 'ๅญใใใฃใใใฎใใใใ็ซๅฃใใ็ชใใตใฟๆธๆฃใพใใงใใ';
mbTrimWidth(str, 12);
// => "ๅญใใใฃใใใฎใใใใ็ซ"
mbTrimWidth(str, 12, 'โฆ');
// => "ๅญใใใฃใใใฎใใใใโฆ"
const str = 'I love ๐๐ and ๐';
mbTrimWidth(str, 9);
// => "I love ๐๐"
mbTrimWidth(str, 9, 'โฆ');
// => "I love ๐โฆ"
const str = '็งใฏ๐๐ใจ๐ใใใพใ';
mbTrimWidth(str, 6);
// => "็งใฏ๐๐ใจ๐"
mbTrimWidth(str, 6, 'โฆ');
// => "็งใฏ๐๐ใจโฆ"
const str = '๐ใฎๅคใฏ๐ ฎท้ๅฑใง๐ฉธฝใ้ฃในใใ๐';
mbTrimWidth(str, 9);
// => "๐ใฎๅคใฏ๐ ฎท้ๅฑใง๐ฉธฝ"
mbTrimWidth(str, 9, 'โฆ');
// => "๐ใฎๅคใฏ๐ ฎท้ๅฑใงโฆ"