Skip to content

Commit 823e59a

Browse files
author
Lingdong Huang
committed
fix unicode problem again
1 parent 6b3477d commit 823e59a

File tree

2 files changed

+127
-127
lines changed

2 files changed

+127
-127
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
// "eslint:recommended",
1313
"prettier",
1414
"prettier/babel"
15-
]
15+
],
1616
rules:{
1717
"quote-props":[2,"always"]
1818
}

src/hanzi2num.js

Lines changed: 126 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
11
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", // 零
99

1010
// pseudo tokens
11-
BEGIN: "BEGIN", // <BEGIN>
12-
END: "END" // <END>
11+
"BEGIN": "BEGIN", // <BEGIN>
12+
"END": "END" // <END>
1313
};
1414

1515
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 }
5858
};
5959

6060
const NEG_WORD = "負";
6161
const INF_WORD = "無限大數";
6262
const NAN_WORD = "不可算數";
6363

6464
const DECIMAL_WORD = {
65-
readout: "又"
65+
"readout": "又"
6666
};
6767

6868
const DIGIT_WORDS = {
69-
readout: {
69+
"readout": {
7070
"0": "零",
7171
"1": "一",
7272
"2": "二",
@@ -81,75 +81,75 @@ const DIGIT_WORDS = {
8181
};
8282

8383
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 }
113113
]
114114
};
115115

116116
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" // 負一
121121
};
122122

123123
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 〇十)
133133
};
134134

135135
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()
139139
};
140140

141141
function hanzi2numstr(s) {
142142
// returns array of tokens on success, null on invalid string
143143
function tokenize(s) {
144-
let result = [{ type: eTokenType.BEGIN }];
144+
let result = [{ "type": eTokenType.BEGIN }];
145145
for (let i = 0; i < s.length; ++i) {
146146
let tokenStr = s.charAt(i);
147147
if (!NUM_TOKENS.hasOwnProperty(tokenStr)) {
148148
return null;
149149
}
150150
result.push(NUM_TOKENS[tokenStr]);
151151
}
152-
result.push({ type: eTokenType.END });
152+
result.push({ "type": eTokenType.END });
153153
return result;
154154
}
155155

@@ -169,16 +169,16 @@ function hanzi2numstr(s) {
169169

170170
// multiplier stack, keep track of all active multiplier exponents
171171
let multStack = {
172-
isEmpty: function() {
172+
"isEmpty": function() {
173173
return this._exps.length == 0;
174174
},
175-
total: function() {
175+
"total": function() {
176176
return this._expAdd;
177177
},
178-
top: function() {
178+
"top": function() {
179179
return this._exps[this._exps.length - 1];
180180
},
181-
state: function() {
181+
"state": function() {
182182
if (this.isEmpty()) {
183183
return eMultState.NONE;
184184
} else if (this._exps[0] < 0) {
@@ -190,44 +190,44 @@ function hanzi2numstr(s) {
190190
}
191191
},
192192

193-
push: function(exp) {
193+
"push": function(exp) {
194194
this._expAdd += exp;
195195
this._exps.push(exp);
196196
},
197-
pop: function() {
197+
"pop": function() {
198198
this._expAdd -= this.top();
199199
this._exps.pop();
200200
},
201-
clear: function() {
201+
"clear": function() {
202202
this._expAdd = 0;
203203
this._exps = [];
204204
},
205-
markDone: function() {
205+
"markDone": function() {
206206
this.clear();
207207
this.push(Infinity);
208208
},
209209

210-
_exps: [],
211-
_expAdd: 0
210+
"_exps": [],
211+
"_expAdd": 0
212212
};
213213

214214
// result, with different convension of exp for internal use
215215
let result = {
216-
sign: function() {
216+
"sign": function() {
217217
return this._sign;
218218
},
219-
exp: function() {
219+
"exp": function() {
220220
return this._exp;
221221
},
222-
digits: function() {
222+
"digits": function() {
223223
return this._digits;
224224
},
225225

226-
applySign: function(newSign) {
226+
"applySign": function(newSign) {
227227
this._sign *= newSign;
228228
},
229229
// digit: number or array of numbers
230-
push: function(digit) {
230+
"push": function(digit) {
231231
if (Array.isArray(digit)) {
232232
this._digits = this._digits.concat(digit);
233233
this._exp += digit.length;
@@ -236,17 +236,17 @@ function hanzi2numstr(s) {
236236
++this._exp;
237237
}
238238
},
239-
fillZeros: function(newExp) {
239+
"fillZeros": function(newExp) {
240240
this.push(Array(newExp - this._exp).fill("0"));
241241
},
242-
resetExp: function(newExp) {
242+
"resetExp": function(newExp) {
243243
this._exp = newExp;
244244
},
245245

246246
// 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
250250
};
251251

252252
// parses the number string backwards, discarding <END>
@@ -589,9 +589,9 @@ function hanzi2numstr(s) {
589589
return null;
590590
}
591591
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()
595595
};
596596
}
597597

@@ -739,9 +739,9 @@ function num2hanzi(n, format = "", precision = undefined) {
739739
);
740740
const fracDigits = fracStr.split("").reverse();
741741
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)
745745
};
746746
}
747747

0 commit comments

Comments
 (0)