11'use strict'
22
33const wordWith = require ( 'word-width' )
4- const isChinese = require ( 'is-chinese' )
4+
5+ const calWords = function ( text ) {
6+ const pins = [ "’" , "√" ]
7+ let len = wordWith ( text )
8+ for ( let i = 0 ; i < text . length ; i ++ ) {
9+ if ( pins . includes ( text [ i ] ) ) {
10+ len ++
11+ }
12+ }
13+ return len
14+ }
15+
16+ exports . calWords = calWords
517
618exports . converter = function ( ...args ) {
719 const key = args . join ( '-' )
@@ -30,14 +42,10 @@ exports.converter = function (...args) {
3042 return valueMap [ key ]
3143}
3244
33- exports . calWords = function ( text ) {
34- return wordWith ( text )
35- }
36-
3745const wordWrap = function ( text , width = 1 ) {
3846 let chunk = [ ]
3947 let lineStr = ''
40- let tokenRe = / .+ ?( \b | $ ) / g
48+ let tokenRe = / .+ ?( \s | $ ) / g
4149 let words = text . match ( tokenRe )
4250
4351 if ( width <= 0 ) {
@@ -51,8 +59,8 @@ const wordWrap = function (text, width = 1) {
5159 for ( let index = 0 ; index < words . length ; index ++ ) {
5260 let word = words [ index ]
5361 let preAppendStr = lineStr + word
54- let chunkSize = wordWith ( preAppendStr )
55- let wordSize = wordWith ( word )
62+ let chunkSize = calWords ( preAppendStr )
63+ let wordSize = calWords ( word )
5664
5765 if ( chunkSize < width ) {
5866 lineStr += word
@@ -70,11 +78,11 @@ const wordWrap = function (text, width = 1) {
7078 } else {
7179 let slices = sliceText ( word , width )
7280 if ( slices . length === 1 ) {
73- wordWith ( slices [ 0 ] ) < width ? lineStr += slices [ 0 ] : chunk . push ( slices [ 0 ] )
81+ calWords ( slices [ 0 ] ) < width ? lineStr += slices [ 0 ] : chunk . push ( slices [ 0 ] )
7482 } else if ( slices . length > 1 ) {
7583 chunk = chunk . concat ( slices . slice ( 0 , slices . length - 1 ) )
7684 let last = slices [ slices . length - 1 ]
77- wordWith ( last ) < width ? lineStr += last : chunk . push ( last )
85+ calWords ( last ) < width ? lineStr += last : chunk . push ( last )
7886 }
7987 }
8088 }
@@ -93,9 +101,9 @@ const sliceText = function (text, width = 1) {
93101 let preAppendStr = lineStr + char
94102 if ( width === 1 ) {
95103 char . length && chunk . push ( char )
96- } else if ( wordWith ( preAppendStr ) < width ) {
104+ } else if ( calWords ( preAppendStr ) < width ) {
97105 lineStr += char
98- } else if ( wordWith ( preAppendStr ) === width ) {
106+ } else if ( calWords ( preAppendStr ) === width ) {
99107 preAppendStr . length && chunk . push ( preAppendStr )
100108 lineStr = ''
101109 } else {
0 commit comments