@hyrious/string-width
v0.1.0
Published
Displayed width of strings according to UAX#11 rules
Readme
@hyrious/string-width
Get displayed width of unicode strings according to UAX#11 rules.
This package is a JS port of Rust's unicode-width.
Caveats
It does not handle ANSI escape codes and complex emoji sequences. You can use strip-ansi and emoji-regex before passing strings to this package. See test.js of commented out test cases.
Install
npm add @hyrious/string-widthUsage
import stringWidth from '@hyrious/string-width'
// const stringWidth = require('@hyrious/string-width')
stringWidth('a')
//=> 1
stringWidth('古')
//=> 2stringWidth(string, options?)
string{String} The string to get the width of.options{Object}ambiguousIsNarrow{Boolean} (default:true) Whether to treat ambiguous-width characters as narrow (count of 1) instead of wide (count of 2).
Returns a number of the column width of the string.
Alternatives
License
MIT @ hyrious
