JoshuaKGoldberg / console-table-without-index

The built-in Node.js console.table, but without the annoying (index) column. 📊
MIT License
7 stars 1 forks source link

🐛 Bug: Can break on certain non-ASCII cases #171

Open noinkling opened 2 weeks ago

noinkling commented 2 weeks ago

Standard console.table behaviour

console.table({'好好好好好': {a:'foo',b:'bar'}, '𝖮𝖧 𝖭𝖮': {a:'foo',b:'bar'}})

good

This library

console.log(table({'好好好好好': {a:'foo',b:'bar'}, '𝖮𝖧 𝖭𝖮': {a:'foo',b:'bar'}}))

bad

Additional Info

Some characters (like 好) display as double width despite being having .length === 1. Other characters have .length === 2 because they're outside the unicode BMP (i.e. they're surrogate pairs), but display as single width (like those in 𝖮𝖧 𝖭𝖮).

console.table() accounts for these cases, but since you're using string .slice() based on an index obtained from the top line, it throws out the calculation. Also I haven't demonstrated it here, but combining characters can cause the same issue.

You could change it to look for the second on a row-by-row basis, however it would then break if that character was in the object key, so honestly I don't know how to fix it robustly without replicating Node's internal width calculation (which as you can see delegates to ICU by default).

github-actions[bot] commented 2 weeks ago

Uh oh! @noinkling, the image you shared is missing helpful alt text. Check your issue body.

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

JoshuaKGoldberg commented 2 weeks ago

Yeah, this is tricky. I bet there's a package out there that already does the width calculation. https://www.npmjs.com/package/string-width, maybe?

Accepting PRs with the note that we should take on a dependency rather than reimplement all that logic in this package. Thanks!

JoshuaKGoldberg commented 2 weeks ago

@all-contributors please add @noinkling for bug.

🤖 Beep boop! This comment was added automatically by all-contributors-auto-action. Not all contributions can be detected from Git & GitHub alone. Please comment any missing contribution types this bot missed. ...and of course, thank you for contributing! 💙

allcontributors[bot] commented 2 weeks ago

@JoshuaKGoldberg

I've put up a pull request to add @noinkling! :tada: