🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@nxmix/string-visual-width

Package Overview
Dependencies
Maintainers
3
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nxmix/string-visual-width

Get the visual width of the string

1.0.1
latest
Source
npm
Version published
Weekly downloads
3
-40%
Maintainers
3
Weekly downloads
 
Created
Source

string-visual-width

Build Status Coverage Status npm Greenkeeper badge

Get the visual width of the string

Why

Because of the combination of Unicode and Emoji characters, it is difficult to calculate the actual display width of a string in Terminal applciation. Strictly speaking there is no universal method to do that.

I try to figure it out in accordance with the following steps:

  • Strip out all ansi codes in string.
  • Nomalize unocide character before calculation, n\u0303 becomes ñ.
  • Use @nxmix/emoji-seq-match to match joined Emoji characters as long as possible and calculate their width as 2.
  • Use @nxmix/is-full-width determine which characters should be treated as East Asian characters and count a width of 2.

Usage

Install

npm i @nxmix/string-visual-width --save

Typescript definition file is already included.

Examples

const width = require('@nxmix/string-visual-width').default;

width('中');
//=> 2

width("👩‍👦");
//=> 2
import getMatchedLength from '@nxmix/string-visual-width';

width('中');
//=> 2

Author

Rong Shen

Keywords

string

FAQs

Package last updated on 12 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts