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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@panzi/print-table

v1.0.1

Published

Print tabular data in a nice unicode table.

Downloads

4

Readme

Print Table

License: MIT Documentation GitHub

A library to print a nicely formatted table, because I didn't like console.table(). This library has no dependencies whatsoever, though when colors are enabled it prints ANSI escape sequences (which are supported by the console of Chromium based browsers).

The table style (what kind of border line characters are used) and the colors (e.g. ANSI escape sequences) can be overridden to your liking.

Example Usage

function aFunc (a: number, b: number) {
    return a + b;
}

printTable([
    ['number',     123,         123.4],
    ['',             4.001,       0.567],
    ['',            -1.2e-10,     2.3e32],
    ['',           NaN,        -100.5],
    ['',      Infinity,   -Infinity],
    ['bigint', 123567890n,  -1234567890n],
    ['boolean', true, false],
    ['string', 'Special chars:\n\0 \r \v \f \u001b \x7f', 
    `Tabs:
		2 tabs
1		2 tabs
12		2 tabs
123		2 tabs
1234	1 tab
12345	1 tab
123456	1 tab
1234567	1 tab
`],
    ['symbol',
     Symbol("special chars:\n\0 \r \v \f \u001b \x7f"),
     Symbol.iterator],
    ['function', ()=>{}, aFunc],
    ['object', null, {
        key: "value",
        array: [1, 2n, 12.34, true, false, null,
                "\0 \r \v \f", "\u001b \x7f\n",
                new Date()],
        aFunc,
        anonFunc: () => {},
        undef: undefined,
        sym: Symbol("foo bar"),
    }],
    ['undefined', undefined],
    ['Date', new Date(), new Date(0)],
], {
    header: [
        'Type',
        'Value 1',
        'Value 2',
    ],
    columnBorders: true,
});

Screenshot illustrating printing with colors:

Screenshot

Plaintext output:

┌───────────┬──────────────────────────────────┬──────────────────────────────────────────┐
│   Type    │             Value 1              │                 Value 2                  │
├───────────┼──────────────────────────────────┼──────────────────────────────────────────┤
│ number    │                        123       │                                123.4     │
│           │                          4.001   │                                  0.567   │
│           │                         -1.2e-10 │                                  2.3e+32 │
│           │                        NaN       │                               -100.5     │
│           │                   Infinity       │                          -Infinity       │
│ bigint    │                  123567890       │                        -1234567890       │
│ boolean   │                       true       │                              false       │
│ string    │ Special chars:                   │ Tabs:                                    │
│           │ \0 \r \v \f \u001b \u007f        │         2 tabs                           │
│           │                                  │ 1       2 tabs                           │
│           │                                  │ 12      2 tabs                           │
│           │                                  │ 123     2 tabs                           │
│           │                                  │ 1234    1 tab                            │
│           │                                  │ 12345   1 tab                            │
│           │                                  │ 123456  1 tab                            │
│           │                                  │ 1234567 1 tab                            │
│           │                                  │                                          │
│ symbol    │ Symbol(special chars:            │ Symbol(Symbol.iterator)                  │
│           │ \0 \r \v \f \u001b \u007f)       │                                          │
│ function  │ [Function (anonymous)]           │ [Function: aFunc]                        │
│ object    │ null                             │ {                                        │
│           │                                  │     "key": "value",                      │
│           │                                  │     "array": [                           │
│           │                                  │         1,                               │
│           │                                  │         "2",                             │
│           │                                  │         12.34,                           │
│           │                                  │         true,                            │
│           │                                  │         false,                           │
│           │                                  │         null,                            │
│           │                                  │         "\u0000 \r \u000b \f",           │
│           │                                  │         "\u001b \u007f\n",               │
│           │                                  │         "2023-05-19T01:50:11.308Z"       │
│           │                                  │     ],                                   │
│           │                                  │     "sym": "Symbol(foo bar)"             │
│           │                                  │ }                                        │
│ undefined │ undefined                        │                                          │
│ Date      │        2023-05-19T01:50:11.308Z  │                1970-01-01T00:00:00.000Z  │
└───────────┴──────────────────────────────────┴──────────────────────────────────────────┘

See the examples folder for more.

Character Width

This library tries to guess the correct width of any printted character. This is error prone, since it actually would depend on the used terminal emulator, installed fonts, and supported Unicode version of those and of NodeJS. Emojis and certain natural languages have characters that are printed as double wide in most terminals. These characters are detected via regular expressions. \p{Emoji_Presentation} is used for emojis and these character ranges for certain Asian scripts are used:

  • U+0AC00 ... U+0D7FF Korean Hangul
  • U+03040 ... U+0A4CF other Asian characters, probably inprecise
  • U+0FF01 ... U+0FF60 Asian full-width characters
  • U+0FFE0 ... U+0FFE6 Asian full-width characters
  • U+20000 ... U+323AF CJK Unified Ideographs Extensions

TODO: There might be more ranges that need to be added here.

For zero-width characters and other code-points that aren't directly printed the regular expressions \p{Nonspacing_Mark} and \p{Default_Ignorable_Code_Point} and these character ranges are used:

  • U+200B ... U+200D zero width space, zero-width non-joiner, zero-width joiner
  • U+2060 word joiner
  • U+FE00 ... U+FE0F variant selectors
  • U+FEFF zero-width no-break space

Because how this all is done using regular expressions it is probably not extremely efficient (I did not do any profiling or speed comparison to other similar libraries). But since this is for displaying a couple of values to humans it is in any case fast enough.