1
1
const eTokenType = {
2
- SIGN : "SIGN" , // 負
3
- DIGIT : "DIGIT" , // 一二三...
4
- DECIMAL : "DECIMAL" , // ·
5
- INT_MULT : "INT_MULT" , // 十百千萬億...
6
- FRAC_MULT : "FRAC_MULT" , // 分釐毫...
7
- DELIM : "DELIM" , // 又
8
- ZERO : "ZERO" , // 零
2
+ " SIGN" : "SIGN" , // 負
3
+ " DIGIT" : "DIGIT" , // 一二三...
4
+ " DECIMAL" : "DECIMAL" , // ·
5
+ " INT_MULT" : "INT_MULT" , // 十百千萬億...
6
+ " FRAC_MULT" : "FRAC_MULT" , // 分釐毫...
7
+ " DELIM" : "DELIM" , // 又
8
+ " ZERO" : "ZERO" , // 零
9
9
10
10
// pseudo tokens
11
- BEGIN : "BEGIN" , // <BEGIN>
12
- END : "END" // <END>
11
+ " BEGIN" : "BEGIN" , // <BEGIN>
12
+ " END" : "END" // <END>
13
13
} ;
14
14
15
15
const NUM_TOKENS = {
16
- 負 : { type : eTokenType . SIGN , sign : - 1 } ,
17
- "·" : { type : eTokenType . DECIMAL , exp : 0 } , // U+00B7 Middle Dot
18
- 又 : { type : eTokenType . DELIM } ,
19
- 有 : { type : eTokenType . DELIM } ,
20
- 零 : { type : eTokenType . ZERO , digit : "0" } ,
21
- 〇 : { type : eTokenType . DIGIT , digit : "0" } , // U+3007 Ideographic Number Zero
22
- 一 : { type : eTokenType . DIGIT , digit : "1" } ,
23
- 二 : { type : eTokenType . DIGIT , digit : "2" } ,
24
- 三 : { type : eTokenType . DIGIT , digit : "3" } ,
25
- 四 : { type : eTokenType . DIGIT , digit : "4" } ,
26
- 五 : { type : eTokenType . DIGIT , digit : "5" } ,
27
- 六 : { type : eTokenType . DIGIT , digit : "6" } ,
28
- 七 : { type : eTokenType . DIGIT , digit : "7" } ,
29
- 八 : { type : eTokenType . DIGIT , digit : "8" } ,
30
- 九 : { type : eTokenType . DIGIT , digit : "9" } ,
31
- 十 : { type : eTokenType . INT_MULT , exp : 1 } ,
32
- 百 : { type : eTokenType . INT_MULT , exp : 2 } ,
33
- 千 : { type : eTokenType . INT_MULT , exp : 3 } ,
34
- 萬 : { type : eTokenType . INT_MULT , exp : 4 } ,
35
- 億 : { type : eTokenType . INT_MULT , exp : 8 } ,
36
- 兆 : { type : eTokenType . INT_MULT , exp : 12 } ,
37
- 京 : { type : eTokenType . INT_MULT , exp : 16 } ,
38
- 垓 : { type : eTokenType . INT_MULT , exp : 20 } ,
39
- 秭 : { type : eTokenType . INT_MULT , exp : 24 } ,
40
- 穰 : { type : eTokenType . INT_MULT , exp : 28 } ,
41
- 溝 : { type : eTokenType . INT_MULT , exp : 32 } ,
42
- 澗 : { type : eTokenType . INT_MULT , exp : 36 } ,
43
- 正 : { type : eTokenType . INT_MULT , exp : 40 } ,
44
- 載 : { type : eTokenType . INT_MULT , exp : 44 } ,
45
- 極 : { type : eTokenType . INT_MULT , exp : 48 } ,
46
- 分 : { type : eTokenType . FRAC_MULT , exp : - 1 } ,
47
- 釐 : { type : eTokenType . FRAC_MULT , exp : - 2 } ,
48
- 毫 : { type : eTokenType . FRAC_MULT , exp : - 3 } ,
49
- 絲 : { type : eTokenType . FRAC_MULT , exp : - 4 } ,
50
- 忽 : { type : eTokenType . FRAC_MULT , exp : - 5 } ,
51
- 微 : { type : eTokenType . FRAC_MULT , exp : - 6 } ,
52
- 纖 : { type : eTokenType . FRAC_MULT , exp : - 7 } ,
53
- 沙 : { type : eTokenType . FRAC_MULT , exp : - 8 } ,
54
- 塵 : { type : eTokenType . FRAC_MULT , exp : - 9 } ,
55
- 埃 : { type : eTokenType . FRAC_MULT , exp : - 10 } ,
56
- 渺 : { type : eTokenType . FRAC_MULT , exp : - 11 } ,
57
- 漠 : { type : eTokenType . FRAC_MULT , exp : - 12 }
16
+ "負" : { " type" : eTokenType . SIGN , " sign" : - 1 } ,
17
+ "·" : { " type" : eTokenType . DECIMAL , " exp" : 0 } , // U+00B7 Middle Dot
18
+ "又" : { " type" : eTokenType . DELIM } ,
19
+ "有" : { " type" : eTokenType . DELIM } ,
20
+ "零" : { " type" : eTokenType . ZERO , " digit" : "0" } ,
21
+ "〇" : { " type" : eTokenType . DIGIT , " digit" : "0" } , // U+3007 Ideographic Number Zero
22
+ "一" : { " type" : eTokenType . DIGIT , " digit" : "1" } ,
23
+ "二" : { " type" : eTokenType . DIGIT , " digit" : "2" } ,
24
+ "三" : { " type" : eTokenType . DIGIT , " digit" : "3" } ,
25
+ "四" : { " type" : eTokenType . DIGIT , " digit" : "4" } ,
26
+ "五" : { " type" : eTokenType . DIGIT , " digit" : "5" } ,
27
+ "六" : { " type" : eTokenType . DIGIT , " digit" : "6" } ,
28
+ "七" : { " type" : eTokenType . DIGIT , " digit" : "7" } ,
29
+ "八" : { " type" : eTokenType . DIGIT , " digit" : "8" } ,
30
+ "九" : { " type" : eTokenType . DIGIT , " digit" : "9" } ,
31
+ "十" : { " type" : eTokenType . INT_MULT , " exp" : 1 } ,
32
+ "百" : { " type" : eTokenType . INT_MULT , " exp" : 2 } ,
33
+ "千" : { " type" : eTokenType . INT_MULT , " exp" : 3 } ,
34
+ "萬" : { " type" : eTokenType . INT_MULT , " exp" : 4 } ,
35
+ "億" : { " type" : eTokenType . INT_MULT , " exp" : 8 } ,
36
+ "兆" : { " type" : eTokenType . INT_MULT , " exp" : 12 } ,
37
+ "京" : { " type" : eTokenType . INT_MULT , " exp" : 16 } ,
38
+ "垓" : { " type" : eTokenType . INT_MULT , " exp" : 20 } ,
39
+ "秭" : { " type" : eTokenType . INT_MULT , " exp" : 24 } ,
40
+ "穰" : { " type" : eTokenType . INT_MULT , " exp" : 28 } ,
41
+ "溝" : { " type" : eTokenType . INT_MULT , " exp" : 32 } ,
42
+ "澗" : { " type" : eTokenType . INT_MULT , " exp" : 36 } ,
43
+ "正" : { " type" : eTokenType . INT_MULT , " exp" : 40 } ,
44
+ "載" : { " type" : eTokenType . INT_MULT , " exp" : 44 } ,
45
+ "極" : { " type" : eTokenType . INT_MULT , " exp" : 48 } ,
46
+ "分" : { " type" : eTokenType . FRAC_MULT , " exp" : - 1 } ,
47
+ "釐" : { " type" : eTokenType . FRAC_MULT , " exp" : - 2 } ,
48
+ "毫" : { " type" : eTokenType . FRAC_MULT , " exp" : - 3 } ,
49
+ "絲" : { " type" : eTokenType . FRAC_MULT , " exp" : - 4 } ,
50
+ "忽" : { " type" : eTokenType . FRAC_MULT , " exp" : - 5 } ,
51
+ "微" : { " type" : eTokenType . FRAC_MULT , " exp" : - 6 } ,
52
+ "纖" : { " type" : eTokenType . FRAC_MULT , " exp" : - 7 } ,
53
+ "沙" : { " type" : eTokenType . FRAC_MULT , " exp" : - 8 } ,
54
+ "塵" : { " type" : eTokenType . FRAC_MULT , " exp" : - 9 } ,
55
+ "埃" : { " type" : eTokenType . FRAC_MULT , " exp" : - 10 } ,
56
+ "渺" : { " type" : eTokenType . FRAC_MULT , " exp" : - 11 } ,
57
+ "漠" : { " type" : eTokenType . FRAC_MULT , " exp" : - 12 }
58
58
} ;
59
59
60
60
const NEG_WORD = "負" ;
61
61
const INF_WORD = "無限大數" ;
62
62
const NAN_WORD = "不可算數" ;
63
63
64
64
const DECIMAL_WORD = {
65
- readout : "又"
65
+ " readout" : "又"
66
66
} ;
67
67
68
68
const DIGIT_WORDS = {
69
- readout : {
69
+ " readout" : {
70
70
"0" : "零" ,
71
71
"1" : "一" ,
72
72
"2" : "二" ,
@@ -81,75 +81,75 @@ const DIGIT_WORDS = {
81
81
} ;
82
82
83
83
const MULT_WORDS = {
84
- readout : [
85
- { str : "極" , exp : 48 } ,
86
- { str : "載" , exp : 44 } ,
87
- { str : "正" , exp : 40 } ,
88
- { str : "澗" , exp : 36 } ,
89
- { str : "溝" , exp : 32 } ,
90
- { str : "穰" , exp : 28 } ,
91
- { str : "秭" , exp : 24 } ,
92
- { str : "垓" , exp : 20 } ,
93
- { str : "京" , exp : 16 } ,
94
- { str : "兆" , exp : 12 } ,
95
- { str : "億" , exp : 8 } ,
96
- { str : "萬" , exp : 4 } ,
97
- { str : "千" , exp : 3 } ,
98
- { str : "百" , exp : 2 } ,
99
- { str : "十" , exp : 1 } ,
100
- { str : "" , exp : 0 } ,
101
- { str : "分" , exp : - 1 } ,
102
- { str : "釐" , exp : - 2 } ,
103
- { str : "毫" , exp : - 3 } ,
104
- { str : "絲" , exp : - 4 } ,
105
- { str : "忽" , exp : - 5 } ,
106
- { str : "微" , exp : - 6 } ,
107
- { str : "纖" , exp : - 7 } ,
108
- { str : "沙" , exp : - 8 } ,
109
- { str : "塵" , exp : - 9 } ,
110
- { str : "埃" , exp : - 10 } ,
111
- { str : "渺" , exp : - 11 } ,
112
- { str : "漠" , exp : - 12 }
84
+ " readout" : [
85
+ { " str" : "極" , " exp" : 48 } ,
86
+ { " str" : "載" , " exp" : 44 } ,
87
+ { " str" : "正" , " exp" : 40 } ,
88
+ { " str" : "澗" , " exp" : 36 } ,
89
+ { " str" : "溝" , " exp" : 32 } ,
90
+ { " str" : "穰" , " exp" : 28 } ,
91
+ { " str" : "秭" , " exp" : 24 } ,
92
+ { " str" : "垓" , " exp" : 20 } ,
93
+ { " str" : "京" , " exp" : 16 } ,
94
+ { " str" : "兆" , " exp" : 12 } ,
95
+ { " str" : "億" , " exp" : 8 } ,
96
+ { " str" : "萬" , " exp" : 4 } ,
97
+ { " str" : "千" , " exp" : 3 } ,
98
+ { " str" : "百" , " exp" : 2 } ,
99
+ { " str" : "十" , " exp" : 1 } ,
100
+ { " str" : "" , " exp" : 0 } ,
101
+ { " str" : "分" , " exp" : - 1 } ,
102
+ { " str" : "釐" , " exp" : - 2 } ,
103
+ { " str" : "毫" , " exp" : - 3 } ,
104
+ { " str" : "絲" , " exp" : - 4 } ,
105
+ { " str" : "忽" , " exp" : - 5 } ,
106
+ { " str" : "微" , " exp" : - 6 } ,
107
+ { " str" : "纖" , " exp" : - 7 } ,
108
+ { " str" : "沙" , " exp" : - 8 } ,
109
+ { " str" : "塵" , " exp" : - 9 } ,
110
+ { " str" : "埃" , " exp" : - 10 } ,
111
+ { " str" : "渺" , " exp" : - 11 } ,
112
+ { " str" : "漠" , " exp" : - 12 }
113
113
]
114
114
} ;
115
115
116
116
const eMultState = {
117
- NONE : "NONE" , // <END>, 一 (ambiguous: 一萬一 or 一十一 or 一·一 or 一絲一)
118
- FRAC : "FRAC" , // ...微
119
- INT : "INT" , // ...萬, ...·,
120
- DONE : "DONE" // 負一
117
+ " NONE" : "NONE" , // <END>, 一 (ambiguous: 一萬一 or 一十一 or 一·一 or 一絲一)
118
+ " FRAC" : "FRAC" , // ...微
119
+ " INT" : "INT" , // ...萬, ...·,
120
+ " DONE" : "DONE" // 負一
121
121
} ;
122
122
123
123
const eDigitState = {
124
- NONE : "NONE" , // <END>, ·
125
- MULT : "MULT" , // 微
126
- MULT_AMBIG : "MULT_AMBIG" , // 十 (ambiguous: ...十 or 一十)
127
- DIGIT : "DIGIT" , // 一
128
- DIGIT_WITH_ZERO : "DIGIT_WITH_ZERO" , // 一...零, 零零, 零一...零,
129
- DELIM : "DELIM" , // 又
130
- ZERO : "ZERO" , // 零<END>, 零·, 零又, 零微, 零一
131
- SIGN : "SIGN" , // 負
132
- ZERO_MULT_AMBIG : "ZERO_MULT_AMBIG" // 零十 (ambiguous: 零一十 or 零十 or 〇十)
124
+ " NONE" : "NONE" , // <END>, ·
125
+ " MULT" : "MULT" , // 微
126
+ " MULT_AMBIG" : "MULT_AMBIG" , // 十 (ambiguous: ...十 or 一十)
127
+ " DIGIT" : "DIGIT" , // 一
128
+ " DIGIT_WITH_ZERO" : "DIGIT_WITH_ZERO" , // 一...零, 零零, 零一...零,
129
+ " DELIM" : "DELIM" , // 又
130
+ " ZERO" : "ZERO" , // 零<END>, 零·, 零又, 零微, 零一
131
+ " SIGN" : "SIGN" , // 負
132
+ " ZERO_MULT_AMBIG" : "ZERO_MULT_AMBIG" // 零十 (ambiguous: 零一十 or 零十 or 〇十)
133
133
} ;
134
134
135
135
const RESULT_2_TO_63 = {
136
- sign : 1 ,
137
- exp : 0 ,
138
- digits : "9223372036854775808" . split ( "" ) . reverse ( )
136
+ " sign" : 1 ,
137
+ " exp" : 0 ,
138
+ " digits" : "9223372036854775808" . split ( "" ) . reverse ( )
139
139
} ;
140
140
141
141
function hanzi2numstr ( s ) {
142
142
// returns array of tokens on success, null on invalid string
143
143
function tokenize ( s ) {
144
- let result = [ { type : eTokenType . BEGIN } ] ;
144
+ let result = [ { " type" : eTokenType . BEGIN } ] ;
145
145
for ( let i = 0 ; i < s . length ; ++ i ) {
146
146
let tokenStr = s . charAt ( i ) ;
147
147
if ( ! NUM_TOKENS . hasOwnProperty ( tokenStr ) ) {
148
148
return null ;
149
149
}
150
150
result . push ( NUM_TOKENS [ tokenStr ] ) ;
151
151
}
152
- result . push ( { type : eTokenType . END } ) ;
152
+ result . push ( { " type" : eTokenType . END } ) ;
153
153
return result ;
154
154
}
155
155
@@ -169,16 +169,16 @@ function hanzi2numstr(s) {
169
169
170
170
// multiplier stack, keep track of all active multiplier exponents
171
171
let multStack = {
172
- isEmpty : function ( ) {
172
+ " isEmpty" : function ( ) {
173
173
return this . _exps . length == 0 ;
174
174
} ,
175
- total : function ( ) {
175
+ " total" : function ( ) {
176
176
return this . _expAdd ;
177
177
} ,
178
- top : function ( ) {
178
+ " top" : function ( ) {
179
179
return this . _exps [ this . _exps . length - 1 ] ;
180
180
} ,
181
- state : function ( ) {
181
+ " state" : function ( ) {
182
182
if ( this . isEmpty ( ) ) {
183
183
return eMultState . NONE ;
184
184
} else if ( this . _exps [ 0 ] < 0 ) {
@@ -190,44 +190,44 @@ function hanzi2numstr(s) {
190
190
}
191
191
} ,
192
192
193
- push : function ( exp ) {
193
+ " push" : function ( exp ) {
194
194
this . _expAdd += exp ;
195
195
this . _exps . push ( exp ) ;
196
196
} ,
197
- pop : function ( ) {
197
+ " pop" : function ( ) {
198
198
this . _expAdd -= this . top ( ) ;
199
199
this . _exps . pop ( ) ;
200
200
} ,
201
- clear : function ( ) {
201
+ " clear" : function ( ) {
202
202
this . _expAdd = 0 ;
203
203
this . _exps = [ ] ;
204
204
} ,
205
- markDone : function ( ) {
205
+ " markDone" : function ( ) {
206
206
this . clear ( ) ;
207
207
this . push ( Infinity ) ;
208
208
} ,
209
209
210
- _exps : [ ] ,
211
- _expAdd : 0
210
+ " _exps" : [ ] ,
211
+ " _expAdd" : 0
212
212
} ;
213
213
214
214
// result, with different convension of exp for internal use
215
215
let result = {
216
- sign : function ( ) {
216
+ " sign" : function ( ) {
217
217
return this . _sign ;
218
218
} ,
219
- exp : function ( ) {
219
+ " exp" : function ( ) {
220
220
return this . _exp ;
221
221
} ,
222
- digits : function ( ) {
222
+ " digits" : function ( ) {
223
223
return this . _digits ;
224
224
} ,
225
225
226
- applySign : function ( newSign ) {
226
+ " applySign" : function ( newSign ) {
227
227
this . _sign *= newSign ;
228
228
} ,
229
229
// digit: number or array of numbers
230
- push : function ( digit ) {
230
+ " push" : function ( digit ) {
231
231
if ( Array . isArray ( digit ) ) {
232
232
this . _digits = this . _digits . concat ( digit ) ;
233
233
this . _exp += digit . length ;
@@ -236,17 +236,17 @@ function hanzi2numstr(s) {
236
236
++ this . _exp ;
237
237
}
238
238
} ,
239
- fillZeros : function ( newExp ) {
239
+ " fillZeros" : function ( newExp ) {
240
240
this . push ( Array ( newExp - this . _exp ) . fill ( "0" ) ) ;
241
241
} ,
242
- resetExp : function ( newExp ) {
242
+ " resetExp" : function ( newExp ) {
243
243
this . _exp = newExp ;
244
244
} ,
245
245
246
246
// the result is sign * 0.{digits[length-1..0]} * 10^exp
247
- _sign : 1 , // +1/-1
248
- _exp : 0 , // one plus exponent of the highest digit
249
- _digits : [ ] // lowest to highest
247
+ " _sign" : 1 , // +1/-1
248
+ " _exp" : 0 , // one plus exponent of the highest digit
249
+ " _digits" : [ ] // lowest to highest
250
250
} ;
251
251
252
252
// parses the number string backwards, discarding <END>
@@ -589,9 +589,9 @@ function hanzi2numstr(s) {
589
589
return null ;
590
590
}
591
591
return {
592
- sign : result . sign ( ) ,
593
- exp : result . exp ( ) - result . digits ( ) . length ,
594
- digits : result . digits ( )
592
+ " sign" : result . sign ( ) ,
593
+ " exp" : result . exp ( ) - result . digits ( ) . length ,
594
+ " digits" : result . digits ( )
595
595
} ;
596
596
}
597
597
@@ -739,9 +739,9 @@ function num2hanzi(n, format = "", precision = undefined) {
739
739
) ;
740
740
const fracDigits = fracStr . split ( "" ) . reverse ( ) ;
741
741
return {
742
- sign : sign ,
743
- exp : scientificExp - fracDigits . length ,
744
- digits : fracDigits . concat ( intDigits )
742
+ " sign" : sign ,
743
+ " exp" : scientificExp - fracDigits . length ,
744
+ " digits" : fracDigits . concat ( intDigits )
745
745
} ;
746
746
}
747
747
0 commit comments