Skip to content

Use emoji regex to compute emoji width #64

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dmail
Copy link

@dmail dmail commented Mar 25, 2025

Before this PR

import stringWidth from 'string-width'l

stringWidth("βœ”"); // 2
stringWidth("πŸ‘©β€πŸ‘©β€πŸ‘¦β€πŸ‘¦"); // 2

After this PR

import stringWidth from 'string-width'l

stringWidth("βœ”"); // 1
stringWidth("πŸ‘©β€πŸ‘©β€πŸ‘¦β€πŸ‘¦"); // 7

Fixes #56.

index.js Outdated
const emojiMatch = character.match(emojiRegex());
if (emojiMatch) {
const emoji = emojiMatch[0];
const codePoints = [...emoji].length;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the idea about [...emoji].length from

https://github.com/mathiasbynens/emoji-regex/blob/8a6871a787a9c9441ed5a341951dcdb1fb3c1d1f/README.md?plain=1#L34

Maybe there is a better/more performant approach but that looks good to me.

@sindresorhus
Copy link
Owner

Length is not the same as width. [...emoji].length counts length, not width.

@dmail
Copy link
Author

dmail commented Mar 26, 2025

😫, any guidance to get the width from the emoji?

@dmail
Copy link
Author

dmail commented Mar 26, 2025

Maybe I could recall stringwidth on the segment when the emoji regexp matches? πŸ€”

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

Successfully merging this pull request may close these issues.

Some characters do false-positively count as 2 while occupying 1 column in terminal
2 participants