File tree 1 file changed +6
-12
lines changed
1 file changed +6
-12
lines changed Original file line number Diff line number Diff line change @@ -2,15 +2,6 @@ import stripAnsi from 'strip-ansi';
2
2
import eastAsianWidth from 'eastasianwidth' ;
3
3
import emojiRegex from 'emoji-regex' ;
4
4
5
- let segmenter ;
6
- function * splitString ( string ) {
7
- segmenter ??= new Intl . Segmenter ( ) ;
8
-
9
- for ( const { segment : character } of segmenter . segment ( string ) ) {
10
- yield character ;
11
- }
12
- }
13
-
14
5
export default function stringWidth ( string , options ) {
15
6
if ( typeof string !== 'string' || string . length === 0 ) {
16
7
return 0 ;
@@ -30,12 +21,10 @@ export default function stringWidth(string, options) {
30
21
return 0 ;
31
22
}
32
23
33
- string = string . replace ( emojiRegex ( ) , ' ' ) ;
34
-
35
24
const ambiguousCharacterWidth = options . ambiguousIsNarrow ? 1 : 2 ;
36
25
let width = 0 ;
37
26
38
- for ( const character of splitString ( string ) ) {
27
+ for ( const { segment : character } of new Intl . Segmenter ( ) . segment ( string ) ) {
39
28
const codePoint = character . codePointAt ( 0 ) ;
40
29
41
30
// Ignore control characters
@@ -48,6 +37,11 @@ export default function stringWidth(string, options) {
48
37
continue ;
49
38
}
50
39
40
+ if ( emojiRegex ( ) . test ( character ) ) {
41
+ width += 2 ;
42
+ continue ;
43
+ }
44
+
51
45
const code = eastAsianWidth . eastAsianWidth ( character ) ;
52
46
switch ( code ) {
53
47
case 'F' :
You can’t perform that action at this time.
0 commit comments