Skip to content

Commit e037825

Browse files
Change type of hanzi2num.token.digit to string
1 parent 815ab1e commit e037825

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

src/hanzi2num.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ const NUM_TOKENS = {
5757
"·": { type: eTokenType.DECIMAL, exp: 0 }, // U+00B7 Middle Dot
5858
"又": { type: eTokenType.DELIM },
5959
"有": { 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" },
7171
"十": { type: eTokenType.INT_MULT, exp: 1 },
7272
"百": { type: eTokenType.INT_MULT, exp: 2 },
7373
"千": { type: eTokenType.INT_MULT, exp: 3 },
@@ -141,11 +141,11 @@ function hanzi2numstr(s) {
141141
// on success: {
142142
// sign: +1/-1/+Infinity/-Infinity/NaN,
143143
// 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
145145
// }
146146
// on invalid string: null
147147
// 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
149149
function parse(tokens) {
150150
// parses the number string backwards, from lowest to highest digit
151151
// parser state
@@ -221,7 +221,7 @@ function hanzi2numstr(s) {
221221
}
222222
},
223223
fillZeros: function (newExp) {
224-
this.push(Array(newExp - this._exp).fill(0));
224+
this.push(Array(newExp - this._exp).fill("0"));
225225
},
226226
resetExp: function (newExp) {
227227
this._exp = newExp;
@@ -256,7 +256,7 @@ function hanzi2numstr(s) {
256256
case eTokenType.DELIM:
257257
case eTokenType.DECIMAL:
258258
case eTokenType.FRAC_MULT:
259-
result.push(1);
259+
result.push("1");
260260
digitState = eDigitState.DIGIT;
261261
break;
262262

@@ -270,7 +270,7 @@ function hanzi2numstr(s) {
270270
// 百(一?)萬 -> 百萬
271271
case eTokenType.INT_MULT:
272272
if (multStack.top() < token.exp) {
273-
result.push(1);
273+
result.push("1");
274274
digitState = eDigitState.DIGIT;
275275
} else {
276276
digitState = eDigitState.MULT;
@@ -299,7 +299,7 @@ function hanzi2numstr(s) {
299299
case eTokenType.DIGIT:
300300
case eTokenType.DELIM:
301301
case eTokenType.ZERO:
302-
result.push(0);
302+
result.push("0");
303303
digitState = eDigitState.DIGIT_WITH_ZERO;
304304
break;
305305

@@ -314,11 +314,11 @@ function hanzi2numstr(s) {
314314
case eTokenType.DECIMAL:
315315
case eTokenType.FRAC_MULT:
316316
if (multStack.total() + 1 < token.exp) {
317-
result.push(1);
318-
result.push(0);
317+
result.push("1");
318+
result.push("0");
319319
digitState = eDigitState.ZERO;
320320
} else if (multStack.total() + 1 == token.exp) {
321-
result.push(0);
321+
result.push("0");
322322
digitState = eDigitState.DIGIT_WITH_ZERO;
323323
} else {
324324
return null;
@@ -331,14 +331,14 @@ function hanzi2numstr(s) {
331331
// 百(零一|零|〇)萬 -> 百零萬
332332
case eTokenType.INT_MULT:
333333
if (multStack.top() + 1 < token.exp) {
334-
result.push(1);
335-
result.push(0);
334+
result.push("1");
335+
result.push("0");
336336
digitState = eDigitState.ZERO;
337337
} else if (multStack.top() + 1 == token.exp) {
338-
result.push(0);
338+
result.push("0");
339339
digitState = eDigitState.DIGIT_WITH_ZERO;
340340
} else {
341-
result.push(0);
341+
result.push("0");
342342
digitState = eDigitState.ZERO;
343343
}
344344
break;
@@ -568,7 +568,7 @@ function hanzi2numstr(s) {
568568
if (idx >= 0 && idx < result.digits.length) {
569569
return result.digits[idx];
570570
} else {
571-
return 0;
571+
return "0";
572572
}
573573
}
574574

@@ -577,8 +577,8 @@ function hanzi2numstr(s) {
577577

578578
const maxExp = Math.max(getMaxExp(resultA), getMaxExp(resultB));
579579
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));
582582
if (digitA > digitB) {
583583
return 1;
584584
} else if (digitA < digitB) {
@@ -615,15 +615,15 @@ function hanzi2numstr(s) {
615615
}();
616616

617617
// 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");
619619
if (rend < 0) {
620620
str += "0";
621621
return str;
622622
}
623623
const rendExp = result.exp + rend;
624624

625625
let rbegin = result.digits.length;
626-
while (result.digits[rbegin - 1] == 0) {
626+
while (result.digits[rbegin - 1] == "0") {
627627
--rbegin;
628628
}
629629
const rbeginExp = result.exp + rbegin;
@@ -642,23 +642,23 @@ function hanzi2numstr(s) {
642642
}
643643

644644
if (printAsScientific) {
645-
str += result.digits[rbegin - 1].toString();
645+
str += result.digits[rbegin - 1];
646646
if (rbegin - 1 > rend) {
647647
str += ".";
648648
for (let i = rbegin - 1; i > rend; --i) {
649-
str += result.digits[i - 1].toString();
649+
str += result.digits[i - 1];
650650
}
651651
}
652652
str += expStr;
653653
return str;
654654
} else {
655655
for (let i = Math.max(rbeginExp, 1); i > 0; --i) {
656-
str += getDigit(result, i - 1).toString();
656+
str += getDigit(result, i - 1);
657657
}
658658
if (rendExp < 0) {
659659
str += ".";
660660
for (let i = 0; i > rendExp; --i) {
661-
str += getDigit(result, i - 1).toString();
661+
str += getDigit(result, i - 1);
662662
}
663663
}
664664
return str;

0 commit comments

Comments
 (0)