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

to-width

v1.2.1

Published

The essential building block for command line tables: truncate & pad strings to given width, taking care of wide characters, accents and ANSI colors

Readme

To-Width

The essential building block for command line tables: truncate & pad strings to given width, taking care of wide characters, accents and ANSI colors.

Table of Contents generated with DocToc

Usage

{ to_width, width_of, } = require 'to-width'

width_of is provided by sindresorhus/string-width; it provides a fairly reliable way to determine the width of strings on character devices. All people who deal with string.length, encodings and buffers in JavaScript will enjoy the following table:

| string | string.length | Buffer.byteLength string | width_of string | | --------: | :--------: | :--------: | :--------: | | 'abcd' | 4 ✅ | 4 ✅ | 4 ✅ | | 'äöüß' | 4 ✅ | 8 ❌ | 4 ✅ | | 'äöüß' (using combining diacritics) | 7 (✅) | 11 ❌ | 7 ❌ | | '北京' | 2 (✅) | 6 ❌ | 4 ✅ | | '𪜀𪜁' | 4 ❌ | 8 ❌ | 4 ✅ |

Bugs

  • width_of doesn't correctly count combining characters.
  • 32bit Unicode glyphs (those from the 'Astral Planes') may be split by to_width, and combining diacritics may get lost:
'#' + ( to_width 'abcdabcd', 4 ) + '#' # --> #abc…#
'#' + ( to_width 'äöüßäöüß', 4 ) + '#' # --> #äöü…#
'#' + ( to_width 'äöüßäöüß', 4 ) + '#' # --> #äöu…#
'#' + ( to_width '北京北京', 4 ) + '#' # --> #北……#
'#' + ( to_width '𪜀𪜁𪜀𪜁', 4 ) + '#' # --> #𪜀�…#

Why?

When I needed tabular data display on the command line, I got dissatisfied with existing solutions. There are some promising modules for doing this on npm, but nothing satisfied me in the end.

I realized that the key requirement for doing tables in the terminal is the ability to format data so that each chunk of text (that you build table cells with) has exactly the correct visual width. Actually, string length fitting seems to have become quite the rage among people these days, at least judging by the recent left-pad hype.

How?

The core functionality of this module has been implemented using

  • https://github.com/sindresorhus/string-width
  • https://github.com/martinheidegger/wcstring

These two modules do the heavy lifting (looking for wide characters, combining characters, and ANSI color codes); to-width does only a little bit of glueing (and fixing providing a workaround for a minor bug in wcstring).

Similar

These packages have also been considered:

  • https://github.com/chalk/strip-ansi
  • https://github.com/chalk/ansi-styles
  • https://github.com/jbnicolai/ansi-256-colors
  • https://github.com/nexdrew/ansi-align
  • https://github.com/sindresorhus/boxen
  • https://github.com/sindresorhus/string-length
  • https://github.com/sindresorhus/cli-truncate
  • https://github.com/chalk/wrap-ansi
  • https://github.com/chalk/slice-ansi
  • https://github.com/substack/fixed-width-float