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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
const {
ambiguousIsNarrow = true,
countAnsiEscapeCodes = false,
skipEmojis = false,
} = options;

if (!countAnsiEscapeCodes) {
Expand Down Expand Up @@ -70,8 +71,10 @@
}

// TODO: Use `/\p{RGI_Emoji}/v` when targeting Node.js 20.
if (emojiRegex().test(character)) {
width += 2;
if (!skipEmojis && emojiRegex().test(character)) {
width += stringWidth(character, {
skipEmojis: true

Check failure on line 76 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Missing trailing comma.

Check failure on line 76 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Missing trailing comma.
})

Check failure on line 77 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 20

Missing semicolon.

Check failure on line 77 in index.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Missing semicolon.
continue;
}

Expand All @@ -80,3 +83,4 @@

return width;
}

2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('main', t => {
t.is(stringWidth('\u001B[31m\u001B[39m', {countAnsiEscapeCodes: true}), 8);
t.is(stringWidth('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007'), 5);
t.is(stringWidth('\u{231A}'), 2, '⌚ default emoji presentation character (Emoji_Presentation)');
t.is(stringWidth('\u{2194}\u{FE0F}'), 2, '↔️ default text presentation character rendered as emoji');
t.is(stringWidth('\u{2194}\u{FE0F}'), 1, '↔️ default text presentation character rendered as emoji');
t.is(stringWidth('\u{1F469}'), 2, 'πŸ‘© emoji modifier base (Emoji_Modifier_Base)');
t.is(stringWidth('\u{1F469}\u{1F3FF}'), 2, 'πŸ‘©πŸΏ emoji modifier base followed by a modifier');
t.is(stringWidth('\u{845B}\u{E0100}'), 2, 'Variation Selectors');
Expand Down
Loading