npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

char-width

v1.0.0

Published

Rust's sequence-aware char width lookup for Node.js & TypeScript.

Readme

char-width

npm

A TypeScript/JavaScript port of Rust's unicode-width: sequence-aware Unicode display width for terminals, with O(1) work per code point.

Attention to Accuracy

  • 🎯 Verified against every output of Rust's unicode-width crate
  • 🧬 Sequence-aware: flags, keycaps, emoji ZWJ and presentation/modifier sequences, etc.
  • 🌏 Unicode 17.0, with a cjk mode for East Asian (ambiguous-wide) contexts

Getting Started

Install char-width via npm:

npm i char-width

Usage

import { charWidth, strWidth } from 'char-width';

charWidth('a'); // 1
charWidth('好'); // 2
charWidth('\x1b'); // undefined (control character)

strWidth('hello'); // 5
strWidth('❤️'); // 2 (emoji presentation sequence)
strWidth('👩‍👩‍👧‍👦'); // 2 (ZWJ sequence)
strWidth('🇦🇺'); // 2 (flag)

Function Parameters:

charWidth():

  • char: The string whose first code point is measured.
  • cjk: Optional; treats East Asian Ambiguous characters as wide.

strWidth():

  • str: The string to measure.
  • cjk: Optional; treats East Asian Ambiguous characters as wide.

Scope of Output

charWidth():

  • 0, 1, 2, or 3: width of the first code point of char.
  • undefined: when it's a control character (C0, DEL, C1) or the string is empty.

strWidth():

  • non-negative width of str: control characters and "\r\n" each count as width 1.

Documentation

The behavior exactly follows that of the unicode-width crate — see its documentation.

TL;DR:

  • A character's width can depend on what follows it (VS16, ZWJ), so strWidth scans once, back to front — O(1) work per code point.
  • Canonically equivalent strings get the same width.
  • Widths predict terminal behavior; no library matches every terminal.

⚠️ One deliberate divergence

A lone surrogate — possible in a JS string, impossible in a Rust one — has width 1.

Updating to a new Unicode version

git clone https://github.com/unicode-rs/unicode-width # reference crate
(cd unicode-width && python3 scripts/unicode.py)      # regenerate Rust tables from the UCD
npm run gen                                           # convert them to src/gen/*.ts
npm run gen:truth                                     # re-dump ground truth (needs cargo)
npm test                                              # 1.1M-code-point conformance check

Feedback

Found something odd?
Feel free to open an issue.

Acknowledgments

  • Width algorithm, state machine, and tables derived from unicode-width by the Rust Project Developers and the unicode-rs maintainers (MIT OR Apache-2.0, used under the MIT option).
  • Character data from the Unicode Character Database (Unicode License v3).

See THIRD-PARTY-NOTICES.md for more.

License

Distributed under the MIT License. See LICENSE for more information.

Looking for a POSIX-compliant port?

Try wcwidth-o1.