@@ -75,30 +75,42 @@ export type Validation = {
7575
7676const adjustedFullwidthPunctuations = `“”‘’`
7777
78- const generateMarker = ( str : string , index : number ) : string => {
79- const prefix = str . substring ( 0 , index )
78+ export const generateMarker = ( str : string , index : number ) : string => {
79+ const prefix = Array . from ( str ) . slice ( 0 , index )
8080 let fullwidthCount = 0
8181 let halfwidthCount = 0
82- for ( let i = 0 ; i < prefix . length ; i ++ ) {
83- const charType = checkCharType ( prefix [ i ] )
82+ let emojiWidthCount = 0
83+ const EMOJI_PLACEHOLDER = '\u2B1C' // ⬜
84+ const FULLWIDTH_SPACE = '\u3000' // 全角空格
85+ function isEmoji ( char : string ) : boolean {
86+ const cp = char . codePointAt ( 0 )
87+ return cp !== undefined && cp >= 0x1F600 && cp <= 0x1F64F
88+ }
89+ for ( const char of prefix ) {
90+ if ( isEmoji ( char ) ) {
91+ emojiWidthCount ++
92+ continue
93+ }
94+ const charType = checkCharType ( char )
8495 if (
8596 charType === CharType . CJK_CHAR ||
8697 ( isFullwidthPunctuationType ( charType ) &&
87- adjustedFullwidthPunctuations . indexOf ( prefix [ i ] ) === - 1 )
98+ adjustedFullwidthPunctuations . indexOf ( char ) === - 1 )
8899 ) {
89100 fullwidthCount ++
90101 } else if (
91102 charType === CharType . WESTERN_LETTER ||
92- ( isHalfwidthPunctuationType ( charType ) &&
93- adjustedFullwidthPunctuations . indexOf ( prefix [ i ] ) !== - 1 ) ||
103+ isHalfwidthPunctuationType ( charType ) ||
104+ adjustedFullwidthPunctuations . indexOf ( char ) !== - 1 ||
94105 charType === CharType . SPACE
95106 ) {
96107 halfwidthCount ++
97108 }
98109 }
99110 return (
100111 ' ' . repeat ( halfwidthCount ) +
101- ' ' . repeat ( fullwidthCount ) +
112+ FULLWIDTH_SPACE . repeat ( fullwidthCount ) +
113+ EMOJI_PLACEHOLDER . repeat ( emojiWidthCount ) +
102114 `${ chalk . red ( '^' ) } `
103115 )
104116}
0 commit comments