Skip to content

Commit d4c8f16

Browse files
Ensuring emojis are matched properly, if your version of Node recognizes them as such
1 parent bd8324d commit d4c8f16

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import type {TruncationOptions, WidthOptions, Result} from './types';
99
const ANSI_RE = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/y;
1010
const CONTROL_RE = /[\x00-\x08\x0A-\x1F\x7F-\x9F]{1,1000}/y;
1111
const TAB_RE = /\t{1,1000}/y;
12-
const EMOJI_RE = /(?:\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}\uFE0F)(?:\u200d(?:\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}\uFE0F))*/yu;
13-
const LATIN_RE = /[\x20-\x7E\xA0-\xFF]{1,1000}/y;
12+
const EMOJI_RE = /[\u{1F1E6}-\u{1F1FF}]{2}|\u{1F3F4}[\u{E0061}-\u{E007A}]{2}[\u{E0030}-\u{E0039}\u{E0061}-\u{E007A}]{1,3}\u{E007F}|(?:\p{Emoji}\uFE0F\u20E3?|\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation})(?:\u200D(?:\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}\uFE0F\u20E3?))*/yu;
13+
const LATIN_RE = /(?:[\x20-\x7E\xA0-\xFF](?!\uFE0F)){1,1000}/y;
1414
const MODIFIER_RE = /\p{M}+/gu;
1515
const NO_TRUNCATION: TruncationOptions = { limit: Infinity, ellipsis: '' };
1616

test/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,35 @@ describe ( 'Fast String Width', () => {
9696
t.is ( getWidth ( '👶🏽' ), 2 );
9797
t.is ( getWidth ( '👩‍👩‍👦‍👦' ), 2 );
9898
t.is ( getWidth ( '👨‍❤️‍💋‍👨' ), 2 );
99+
t.is ( getWidth ( '🏴‍☠️' ), 2 );
100+
t.is ( getWidth ( '🏴󠁧󠁢󠁷󠁬󠁳󠁿' ), 2 );
101+
t.is ( getWidth ( '🇸🇪' ), 2 );
102+
t.is ( getWidth ( '🇺🇳' ), 2 );
99103

100104
t.is ( getWidth ( '👶'.repeat ( 2 ) ), 4 );
101105
t.is ( getWidth ( '👶🏽'.repeat ( 2 ) ), 4 );
102106
t.is ( getWidth ( '👩‍👩‍👦‍👦'.repeat ( 2 ) ), 4 );
103107
t.is ( getWidth ( '👨‍❤️‍💋‍👨'.repeat ( 2 ) ), 4 );
108+
t.is ( getWidth ( '🏴‍☠️'.repeat ( 2 ) ), 4 );
109+
t.is ( getWidth ( '🏴󠁧󠁢󠁷󠁬󠁳󠁿'.repeat ( 2 ) ), 4 );
110+
t.is ( getWidth ( '🇸🇪'.repeat ( 2 ) ), 4 );
111+
t.is ( getWidth ( '🇺🇳'.repeat ( 2 ) ), 4 );
112+
113+
});
114+
115+
it.skip ( 'supports all basic emojis', async t => {
116+
117+
const response = await fetch ( 'https://raw.githubusercontent.com/muan/unicode-emoji-json/main/data-by-group.json' );
118+
const data = await response.json ();
119+
const emojis = data.flatMap ( ({ emojis }) => emojis.map ( ({ emoji }) => emoji ) );
120+
121+
const failures = emojis.filter ( emoji => {
122+
if ( getWidth ( emoji ) !== 2 ) {
123+
return true;
124+
}
125+
});
126+
127+
t.deepEqual ( failures, [] );
104128

105129
});
106130

0 commit comments

Comments
 (0)