Get the visual width of a string - the number of columns required to display it
by sindresorhusJavaScript
Last 12 weeks · 2 commits
4 of 6 standards met
In my terminal (Terminal.app), I can write the string: Which consists of 6 instances of the code point (a Korean character), as a single grapheme cluster. In my terminal, each code point takes up 2 columns visually, for a total of 12 columns. But reports the entire string as just being 2 columns wide. This seems like a bug. I looked at the source code, and the culprit seems to be here: https://github.com/sindresorhus/string-width/blob/main/index.js#L111 , where it's just looking at the first code point of the grapheme cluster, rather than counting all of them.
Using the latest version of throws a runtime error in environments where the JavaScript engine does not support the (Unicode Sets) regex flag. The following regex in the package causes a crash: Error: Environment Node.js version: OS: string-width version: Package manager: Steps to Reproduce 1. Install the package 2. Run code that imports 3. Runtime throws the regex flag error Expected Behavior The package should either: maintain compatibility with Node versions that don't support , or clearly specify the required Node.js version in . Possible Solutions Replace with where Unicode set operations are not required. Document the minimum supported Node version. Provide a fallback build for older runtimes. Additional Context** This appears to occur because the flag (Unicode Sets) is not supported in some Node.js versions still widely used in production.
strip-ansi has below note on its npm page. So this PR removes strip-ansi dependency to use node:util. [!NOTE] Node.js has this built-in now with stripVTControlCharacters. The benefit of this package is consistent behavior across Node.js versions and faster improvements. The Node.js version is actually based on this package.
Repository: sindresorhus/string-width. Description: Get the visual width of a string - the number of columns required to display it Stars: 528, Forks: 41. Primary language: JavaScript. Languages: JavaScript (98.6%), TypeScript (1.4%). License: MIT. Latest release: v8.2.1 (2mo ago). Open PRs: 0, open issues: 1. Last activity: 2mo ago. Community health: 85%. Top contributors: sindresorhus, fisker, coreyfarrell, BendingBender, adam2k, privatenumber, LitoMore, Richienb, seachicken, shinnn and others.