Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong whitespace padding when formatting content with emojis #56

Open
fhemberger opened this issue Nov 14, 2023 · 0 comments
Open

Wrong whitespace padding when formatting content with emojis #56

fhemberger opened this issue Nov 14, 2023 · 0 comments

Comments

@fhemberger
Copy link

Trying to format a table with emoji results in wrong whitespace padding:

| First column | Second column | Third column |
|--------------|---------------|--------------|
| First row    | ✅             | ❌            |
| Second row   | ✅             | ❌            |

JavaScript assumes each Unicode character taking up 2 bytes, but some characters take up more bytes (for example emojis):

// This is wrong
'✅'.length === 2
> true

Using Intl.Segmenter fixes this issue:

// Much better!
[...new Intl.Segmenter().segment('✅')].length 
> 1

[...new Intl.Segmenter().segment('✅ hello world')].length
> 13

See also: How to count the correct length of a string with emojis in javascript?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant