1
- import { AMBIGUOUS , POTENTIAL_EMOJI } from './constants.js'
2
- import { Width } from './widths.js'
3
- import emojiRegex from 'emoji-regex'
1
+ import { AMBIGUOUS , POTENTIAL_EMOJI } from './constants.js' ;
2
+ import { Width } from './widths.js' ;
3
+ import emojiRegex from 'emoji-regex' ;
4
4
5
- const ASCII_REGEX = / ^ [ \x20 - \x7e ] * $ /
6
- const DEFAULT_LOCALE = new Intl . Segmenter ( ) . resolvedOptions ( ) . locale
7
- const EMOJI_REGEX = emojiRegex ( )
5
+ const ASCII_REGEX = / ^ [ \x20 - \x7e ] * $ / ;
6
+ const DEFAULT_LOCALE = new Intl . Segmenter ( ) . resolvedOptions ( ) . locale ;
7
+ const EMOJI_REGEX = emojiRegex ( ) ;
8
8
9
9
export {
10
10
AMBIGUOUS ,
11
11
POTENTIAL_EMOJI ,
12
- }
12
+ } ;
13
13
14
14
/**
15
15
* @typedef {object } ExtraWidths
@@ -27,9 +27,9 @@ export {
27
27
*/
28
28
const NO_EXTRAS = {
29
29
get ( /** @type {number } */ _n ) {
30
- return undefined
30
+ return undefined ;
31
31
} ,
32
- }
32
+ } ;
33
33
34
34
/**
35
35
* @typedef {object } StringWidthOptions
@@ -44,9 +44,9 @@ const NO_EXTRAS = {
44
44
*/
45
45
46
46
export class StringWidth {
47
- #graphemes
48
- #isCJK
49
- #extraWidths
47
+ #graphemes;
48
+ #isCJK;
49
+ #extraWidths;
50
50
51
51
/**
52
52
* Create a StringWidth instance.
@@ -62,19 +62,19 @@ export class StringWidth {
62
62
extraWidths : NO_EXTRAS ,
63
63
isCJK : false ,
64
64
...opts ,
65
- }
65
+ } ;
66
66
this . #graphemes = new Intl . Segmenter ( options . locale , {
67
67
granularity : 'grapheme' ,
68
- } )
68
+ } ) ;
69
69
if ( typeof opts . isCJK === 'boolean' ) {
70
- this . #isCJK = opts . isCJK
70
+ this . #isCJK = opts . isCJK ;
71
71
} else {
72
- const loc = new Intl . Locale ( this . locale ) . maximize ( )
72
+ const loc = new Intl . Locale ( this . locale ) . maximize ( ) ;
73
73
// Script is never undefined after going through Segmenter
74
74
this . #isCJK = [ 'Hans' , 'Hant' , 'Kore' , 'Jpan' ]
75
- . includes ( /** @type {string } */ ( loc . script ) )
75
+ . includes ( /** @type {string } */ ( loc . script ) ) ;
76
76
}
77
- this . #extraWidths = options . extraWidths
77
+ this . #extraWidths = options . extraWidths ;
78
78
}
79
79
80
80
/**
@@ -84,7 +84,7 @@ export class StringWidth {
84
84
* @readonly
85
85
*/
86
86
get isCJK ( ) {
87
- return this . #isCJK
87
+ return this . #isCJK;
88
88
}
89
89
90
90
/**
@@ -93,7 +93,7 @@ export class StringWidth {
93
93
* @readonly
94
94
*/
95
95
get locale ( ) {
96
- return this . #graphemes. resolvedOptions ( ) . locale
96
+ return this . #graphemes. resolvedOptions ( ) . locale ;
97
97
}
98
98
99
99
/**
@@ -103,7 +103,7 @@ export class StringWidth {
103
103
*/
104
104
* graphemes ( str ) {
105
105
for ( const { segment} of this . #graphemes. segment ( str ) ) {
106
- yield segment
106
+ yield segment ;
107
107
}
108
108
}
109
109
@@ -115,16 +115,16 @@ export class StringWidth {
115
115
#segmentWidth( segment ) {
116
116
// First codepoint in the grapheme. Usually all the rest will be
117
117
// combining characters, unless this is an RI or an emoji sequence.
118
- const cp = /** @type {number } */ ( segment . codePointAt ( 0 ) )
119
- const w = this . #extraWidths. get ( cp ) ?? Width . get ( cp )
118
+ const cp = /** @type {number } */ ( segment . codePointAt ( 0 ) ) ;
119
+ const w = this . #extraWidths. get ( cp ) ?? Width . get ( cp ) ;
120
120
switch ( w ) {
121
121
case POTENTIAL_EMOJI :
122
- EMOJI_REGEX . lastIndex = 0 // Revisit this.
123
- return EMOJI_REGEX . test ( segment ) ? 2 : 1
122
+ EMOJI_REGEX . lastIndex = 0 ; // Revisit this.
123
+ return EMOJI_REGEX . test ( segment ) ? 2 : 1 ;
124
124
case AMBIGUOUS : // Ambiguous
125
- return this . #isCJK ? 2 : 1
125
+ return this . #isCJK ? 2 : 1 ;
126
126
default :
127
- return w
127
+ return w ;
128
128
}
129
129
}
130
130
@@ -138,14 +138,14 @@ export class StringWidth {
138
138
width ( str ) {
139
139
// Fast-path shortcut for all-ASCII
140
140
if ( ASCII_REGEX . test ( str ) ) {
141
- return str . length
141
+ return str . length ;
142
142
}
143
143
144
- let ret = 0
144
+ let ret = 0 ;
145
145
for ( const segment of this . graphemes ( str ) ) {
146
- ret += this . #segmentWidth( segment )
146
+ ret += this . #segmentWidth( segment ) ;
147
147
}
148
- return ret < 0 ? 0 : ret
148
+ return ret < 0 ? 0 : ret ;
149
149
}
150
150
151
151
/**
@@ -167,53 +167,53 @@ export class StringWidth {
167
167
*/
168
168
break ( str , width ) {
169
169
if ( width < 1 ) {
170
- throw new RangeError ( `Width must be >= 1. Got ${ width } .` )
170
+ throw new RangeError ( `Width must be >= 1. Got ${ width } .` ) ;
171
171
}
172
172
173
173
/** @type {WidthBreak[] } */
174
- const ret = [ ]
175
- let string = ''
176
- let cells = 0
174
+ const ret = [ ] ;
175
+ let string = '' ;
176
+ let cells = 0 ;
177
177
178
178
// Fast-path shortcut for all-ASCII
179
179
if ( ASCII_REGEX . test ( str ) ) {
180
180
for ( cells = 0 ; cells < str . length ; cells += width ) {
181
- string = str . slice ( cells , cells + width )
181
+ string = str . slice ( cells , cells + width ) ;
182
182
ret . push ( {
183
183
string,
184
184
cells : string . length , // Might be less than width for last chunk
185
185
last : false ,
186
- } )
186
+ } ) ;
187
187
}
188
188
} else {
189
189
for ( const segment of this . graphemes ( str ) ) {
190
- const w = this . #segmentWidth( segment )
190
+ const w = this . #segmentWidth( segment ) ;
191
191
if ( w > width ) {
192
192
// Skinny width, fat grapheme cluster
193
193
if ( cells > 0 ) {
194
- ret . push ( { string, cells, last : false } )
195
- string = ''
196
- cells = 0
194
+ ret . push ( { string, cells, last : false } ) ;
195
+ string = '' ;
196
+ cells = 0 ;
197
197
}
198
- ret . push ( { string : segment , cells : w , last : false } )
198
+ ret . push ( { string : segment , cells : w , last : false } ) ;
199
199
} else if ( cells + w > width ) {
200
- ret . push ( { string, cells, last : false } )
201
- string = segment
202
- cells = w
200
+ ret . push ( { string, cells, last : false } ) ;
201
+ string = segment ;
202
+ cells = w ;
203
203
} else {
204
- string += segment
205
- cells += w
204
+ string += segment ;
205
+ cells += w ;
206
206
}
207
207
}
208
208
if ( cells > 0 ) {
209
- ret . push ( { string, cells, last : true } )
209
+ ret . push ( { string, cells, last : true } ) ;
210
210
}
211
211
}
212
212
213
213
// Several paths lead here.
214
214
if ( ret . length > 0 ) {
215
- ret [ ret . length - 1 ] . last = true
215
+ ret [ ret . length - 1 ] . last = true ;
216
216
}
217
- return ret
217
+ return ret ;
218
218
}
219
219
}
0 commit comments