Skip to content

A module of trim multibyte string by character width. This will work even if contained Unicode, emoji, and surrogate pairs.

Notifications You must be signed in to change notification settings

KiKiKi-KiKi/mbTrimWidth

Repository files navigation

mbTrimWidth

NPM version Build Status Coverage Status ISC License

A module of trim multibyte string by character width. This will work even if contained Unicode, emoji, and surrogate pairs.

install

$ npm install --save mb-trimwidth

usage

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, '…');
// => "🌕の夜は𠮷野屋で…"

Test

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, '…');
// => "🌕の夜は𠮷野屋で…"

About

A module of trim multibyte string by character width. This will work even if contained Unicode, emoji, and surrogate pairs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published