@@ -57,17 +57,17 @@ const NUM_TOKENS = {
57
57
"·" : { type : eTokenType . DECIMAL , exp : 0 } , // U+00B7 Middle Dot
58
58
"又" : { type : eTokenType . DELIM } ,
59
59
"有" : { type : eTokenType . DELIM } ,
60
- "零" : { type : eTokenType . ZERO , digit : 0 } ,
61
- "〇" : { type : eTokenType . DIGIT , digit : 0 } , // U+3007 Ideographic Number Zero
62
- "一" : { type : eTokenType . DIGIT , digit : 1 } ,
63
- "二" : { type : eTokenType . DIGIT , digit : 2 } ,
64
- "三" : { type : eTokenType . DIGIT , digit : 3 } ,
65
- "四" : { type : eTokenType . DIGIT , digit : 4 } ,
66
- "五" : { type : eTokenType . DIGIT , digit : 5 } ,
67
- "六" : { type : eTokenType . DIGIT , digit : 6 } ,
68
- "七" : { type : eTokenType . DIGIT , digit : 7 } ,
69
- "八" : { type : eTokenType . DIGIT , digit : 8 } ,
70
- "九" : { type : eTokenType . DIGIT , digit : 9 } ,
60
+ "零" : { type : eTokenType . ZERO , digit : "0" } ,
61
+ "〇" : { type : eTokenType . DIGIT , digit : "0" } , // U+3007 Ideographic Number Zero
62
+ "一" : { type : eTokenType . DIGIT , digit : "1" } ,
63
+ "二" : { type : eTokenType . DIGIT , digit : "2" } ,
64
+ "三" : { type : eTokenType . DIGIT , digit : "3" } ,
65
+ "四" : { type : eTokenType . DIGIT , digit : "4" } ,
66
+ "五" : { type : eTokenType . DIGIT , digit : "5" } ,
67
+ "六" : { type : eTokenType . DIGIT , digit : "6" } ,
68
+ "七" : { type : eTokenType . DIGIT , digit : "7" } ,
69
+ "八" : { type : eTokenType . DIGIT , digit : "8" } ,
70
+ "九" : { type : eTokenType . DIGIT , digit : "9" } ,
71
71
"十" : { type : eTokenType . INT_MULT , exp : 1 } ,
72
72
"百" : { type : eTokenType . INT_MULT , exp : 2 } ,
73
73
"千" : { type : eTokenType . INT_MULT , exp : 3 } ,
@@ -141,11 +141,11 @@ function hanzi2numstr(s) {
141
141
// on success: {
142
142
// sign: +1/-1/+Infinity/-Infinity/NaN,
143
143
// exp: power of 10,
144
- // digits: array of digits from lowest to highest, with leading and trailing zeros
144
+ // digits: array of digit chars from lowest to highest, with leading and trailing zeros
145
145
// }
146
146
// on invalid string: null
147
147
// result = sign * {digits[length-1..0]} * 10^exp
148
- // e.g. 負零又五毫零絲 = { sign: -1, exp: -4, digits: [0, 5, 0, 0, 0 ] } = -00050e-4 = -0.005
148
+ // e.g. 負零又五毫零絲 = { sign: -1, exp: -4, digits: ["0", "5", "0", " 0, "0" ] } = -00050e-4 = -0.005
149
149
function parse ( tokens ) {
150
150
// parses the number string backwards, from lowest to highest digit
151
151
// parser state
@@ -221,7 +221,7 @@ function hanzi2numstr(s) {
221
221
}
222
222
} ,
223
223
fillZeros : function ( newExp ) {
224
- this . push ( Array ( newExp - this . _exp ) . fill ( 0 ) ) ;
224
+ this . push ( Array ( newExp - this . _exp ) . fill ( "0" ) ) ;
225
225
} ,
226
226
resetExp : function ( newExp ) {
227
227
this . _exp = newExp ;
@@ -256,7 +256,7 @@ function hanzi2numstr(s) {
256
256
case eTokenType . DELIM :
257
257
case eTokenType . DECIMAL :
258
258
case eTokenType . FRAC_MULT :
259
- result . push ( 1 ) ;
259
+ result . push ( "1" ) ;
260
260
digitState = eDigitState . DIGIT ;
261
261
break ;
262
262
@@ -270,7 +270,7 @@ function hanzi2numstr(s) {
270
270
// 百(一?)萬 -> 百萬
271
271
case eTokenType . INT_MULT :
272
272
if ( multStack . top ( ) < token . exp ) {
273
- result . push ( 1 ) ;
273
+ result . push ( "1" ) ;
274
274
digitState = eDigitState . DIGIT ;
275
275
} else {
276
276
digitState = eDigitState . MULT ;
@@ -299,7 +299,7 @@ function hanzi2numstr(s) {
299
299
case eTokenType . DIGIT :
300
300
case eTokenType . DELIM :
301
301
case eTokenType . ZERO :
302
- result . push ( 0 ) ;
302
+ result . push ( "0" ) ;
303
303
digitState = eDigitState . DIGIT_WITH_ZERO ;
304
304
break ;
305
305
@@ -314,11 +314,11 @@ function hanzi2numstr(s) {
314
314
case eTokenType . DECIMAL :
315
315
case eTokenType . FRAC_MULT :
316
316
if ( multStack . total ( ) + 1 < token . exp ) {
317
- result . push ( 1 ) ;
318
- result . push ( 0 ) ;
317
+ result . push ( "1" ) ;
318
+ result . push ( "0" ) ;
319
319
digitState = eDigitState . ZERO ;
320
320
} else if ( multStack . total ( ) + 1 == token . exp ) {
321
- result . push ( 0 ) ;
321
+ result . push ( "0" ) ;
322
322
digitState = eDigitState . DIGIT_WITH_ZERO ;
323
323
} else {
324
324
return null ;
@@ -331,14 +331,14 @@ function hanzi2numstr(s) {
331
331
// 百(零一|零|〇)萬 -> 百零萬
332
332
case eTokenType . INT_MULT :
333
333
if ( multStack . top ( ) + 1 < token . exp ) {
334
- result . push ( 1 ) ;
335
- result . push ( 0 ) ;
334
+ result . push ( "1" ) ;
335
+ result . push ( "0" ) ;
336
336
digitState = eDigitState . ZERO ;
337
337
} else if ( multStack . top ( ) + 1 == token . exp ) {
338
- result . push ( 0 ) ;
338
+ result . push ( "0" ) ;
339
339
digitState = eDigitState . DIGIT_WITH_ZERO ;
340
340
} else {
341
- result . push ( 0 ) ;
341
+ result . push ( "0" ) ;
342
342
digitState = eDigitState . ZERO ;
343
343
}
344
344
break ;
@@ -568,7 +568,7 @@ function hanzi2numstr(s) {
568
568
if ( idx >= 0 && idx < result . digits . length ) {
569
569
return result . digits [ idx ] ;
570
570
} else {
571
- return 0 ;
571
+ return "0" ;
572
572
}
573
573
}
574
574
@@ -577,8 +577,8 @@ function hanzi2numstr(s) {
577
577
578
578
const maxExp = Math . max ( getMaxExp ( resultA ) , getMaxExp ( resultB ) ) ;
579
579
for ( let i = maxExp ; i >= resultA . exp || i >= resultB . exp ; -- i ) {
580
- const digitA = getDigit ( resultA , i ) ;
581
- const digitB = getDigit ( resultB , i ) ;
580
+ const digitA = Number ( getDigit ( resultA , i ) ) ;
581
+ const digitB = Number ( getDigit ( resultB , i ) ) ;
582
582
if ( digitA > digitB ) {
583
583
return 1 ;
584
584
} else if ( digitA < digitB ) {
@@ -615,15 +615,15 @@ function hanzi2numstr(s) {
615
615
} ( ) ;
616
616
617
617
// digit range, leading and trailing zeros trimmed
618
- const rend = result . digits . findIndex ( x => x != 0 ) ;
618
+ const rend = result . digits . findIndex ( x => x != "0" ) ;
619
619
if ( rend < 0 ) {
620
620
str += "0" ;
621
621
return str ;
622
622
}
623
623
const rendExp = result . exp + rend ;
624
624
625
625
let rbegin = result . digits . length ;
626
- while ( result . digits [ rbegin - 1 ] == 0 ) {
626
+ while ( result . digits [ rbegin - 1 ] == "0" ) {
627
627
-- rbegin ;
628
628
}
629
629
const rbeginExp = result . exp + rbegin ;
@@ -642,23 +642,23 @@ function hanzi2numstr(s) {
642
642
}
643
643
644
644
if ( printAsScientific ) {
645
- str += result . digits [ rbegin - 1 ] . toString ( ) ;
645
+ str += result . digits [ rbegin - 1 ] ;
646
646
if ( rbegin - 1 > rend ) {
647
647
str += "." ;
648
648
for ( let i = rbegin - 1 ; i > rend ; -- i ) {
649
- str += result . digits [ i - 1 ] . toString ( ) ;
649
+ str += result . digits [ i - 1 ] ;
650
650
}
651
651
}
652
652
str += expStr ;
653
653
return str ;
654
654
} else {
655
655
for ( let i = Math . max ( rbeginExp , 1 ) ; i > 0 ; -- i ) {
656
- str += getDigit ( result , i - 1 ) . toString ( ) ;
656
+ str += getDigit ( result , i - 1 ) ;
657
657
}
658
658
if ( rendExp < 0 ) {
659
659
str += "." ;
660
660
for ( let i = 0 ; i > rendExp ; -- i ) {
661
- str += getDigit ( result , i - 1 ) . toString ( ) ;
661
+ str += getDigit ( result , i - 1 ) ;
662
662
}
663
663
}
664
664
return str ;
0 commit comments