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 🙏

© 2025 – Pkg Stats / Ryan Hefner

wcwidth-o1

v2.0.5

Published

glibc wcwidth/wcswidth for Node.js & TypeScript.

Downloads

182

Readme

Wcwidth-O1

npm

A TypeScript/JavaScript implementation of glibc’s wcwidth(3) and wcswidth(3), optimized to O(1).
Conforms to POSIX.1-2008 (IEEE Std 1003.1) for terminal column width calculation.

Superior Performance

  • ⚡️ Instant O(1) lookup time
  • 🌏 Full Unicode 17.0 coverage

References:

Getting Started

Install Wcwidth-O1 via npm:

npm i wcwidth-o1

Usage

import wcwidth from 'wcwidth-o1';

const example1 = wcwidth('a'); // 1
const example2 = wcwidth('好'); // 2
const example3 = wcwidth('😊'); // 2

or

import { wcwidth, wcswidth } from 'wcwidth-o1';

const example = wcwidth('a'); // 1

const example1 = wcswidth('hi'); // 2
const example2 = wcswidth('안녕하세요'); // 10
const example3 = wcswidth('😊こんにちは'); // 12

Function Parameters:

wcwidth():

  • char: A single-character string to measure.

wcswidth():

  • str: Input string to evaluate.
  • n: Max characters to process (defaults to full length).

Updating Lookup Table

When a new Unicode version is released, the lookup table must be regenerated to follow the latest character width definitions.

1. Prerequisites

  • glibc-based Linux distro (e.g. Debian).

2. Generate new lookup table

./genTable.sh

If your environment is not glibc-based, you'll see:

[ WARNING ] Please compile on a glibc-based Linux distro (e.g. Debian).

Once the generation is complete, you should see:

[ SUCCESS ] table.ts generated successfully.

3. Replace files

Copy the generated table.ts into src/:

cp table.ts src/

The lookup table update is then complete.

Behind Wcwidth

In fixed-width terminals, most Latin characters take up one column, while East Asian (CJK) ideographs usually take up two. The challenge is deciding how many “cells” each Unicode character should occupy so that text aligns correctly.

The Unicode standard defines width classes:

  • Wide (W) and Fullwidth (F) - always 2 columns
  • Halfwidth (H) and Narrow (Na) - always 1 column
  • Ambiguous (A) - 1 column normally, but 2 in CJK compatibility mode
  • Neutral (N) - treated as 1 column here for simplicity

Other rules include:

  • U+0000 (null) - width 0
  • Control characters - -1
  • Combining marks - width 0
  • Soft hyphen (U+00AD) - width 1
  • Zero width space (U+200B) - width 0

This logic originates from Markus Kuhn’s reference implementation and is widely used in terminal emulators to ensure consistent alignment.

See Unicode TR#11 for more details.

Feedback

Found something odd?
Feel free to open an issue.

Acknowledgments

License

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