hyrious / string-width

Get displayed width of unicode strings according to UAX#11 rules.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@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-width

Usage

import stringWidth from '@hyrious/string-width'
// const stringWidth = require('@hyrious/string-width')

stringWidth('a')
//=> 1

stringWidth('古')
//=> 2

stringWidth(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

About

Get displayed width of unicode strings according to UAX#11 rules.

License:MIT License


Languages

Language:JavaScript 97.5%Language:HTML 2.5%