Skip to content

Commit 3e65f7b

Browse files
twadaazu
authored andcommitted
fix(2.2.2): use japanese-numerals-to-number to convert Japanese numerals into number (#57)
* test(2.2.2): add failing test of the `--fix` of Japanese numerals * fix(2.2.2): use japanese-numerals-to-number to convert Japanese numerals into number
1 parent bf77622 commit 3e65f7b

File tree

5 files changed

+20
-23
lines changed

5 files changed

+20
-23
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"dependencies": {
5050
"analyze-desumasu-dearu": "^2.1.2",
51+
"japanese-numerals-to-number": "^1.0.0",
5152
"match-index": "^1.0.1",
5253
"moji": "^0.5.1",
5354
"regexp.prototype.flags": "^1.1.1",

src/2.2.2.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// LICENSE : MIT
22
"use strict";
33
import {isUserWrittenNode} from "./util/node-util";
4+
import ja2num from 'japanese-numerals-to-number';
45
function matchToReplace(text, pattern, matchFn) {
56
var match = pattern.exec(text);
67
if (match) {
@@ -9,24 +10,6 @@ function matchToReplace(text, pattern, matchFn) {
910
return null
1011
}
1112

12-
const numberMap = {
13-
"一": 1,
14-
"二": 2,
15-
"三": 3,
16-
"四": 4,
17-
"五": 5,
18-
"六": 6,
19-
"七": 7,
20-
"八": 8,
21-
"九": 9,
22-
"十": 10,
23-
"壱": 1,
24-
"弐": 2,
25-
"参": 3,
26-
"拾": 10,
27-
"百": 100,
28-
"〇": 0
29-
};
3013
// http://www.drk7.jp/MT/archives/001587.html
3114
function _num2ja(num, opt) {
3215
var sign = {
@@ -117,11 +100,7 @@ function reporter(context) {
117100
const matchedString = match[0];
118101
const index = match.index;
119102
const expected = matchedString.replace(pattern, function (all, match) {
120-
let result = 0;
121-
match.split("").forEach(kanNumber => {
122-
result += numberMap[kanNumber];
123-
});
124-
return all.replace(match, result);
103+
return all.replace(match, ja2num(match));
125104
});
126105
const ruleError = new RuleError(`${matchedString} => ${expected}
127106
数量を表現し、数を数えられるものは算用数字を使用します。任意の数に置き換えても通用する語句がこれに該当します。`, {

test/2.2.2-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
99
"3つのボタン",
1010
"第3回大会",
1111
"第3章",
12+
"20回",
1213
"第3節",
1314
"4種類"
1415
],
@@ -19,6 +20,18 @@ tester.run("2.2.2.算用数字と漢数字の使い分け", rule, {
1920
errors: [
2021
{
2122
message: `第三章 => 第3章
23+
数量を表現し、数を数えられるものは算用数字を使用します。任意の数に置き換えても通用する語句がこれに該当します。`,
24+
line: 1,
25+
column: 1
26+
}
27+
]
28+
},
29+
{
30+
text: "二十回",
31+
output: "20回",
32+
errors: [
33+
{
34+
message: `二十回 => 20回
2235
数量を表現し、数を数えられるものは算用数字を使用します。任意の数に置き換えても通用する語句がこれに該当します。`,
2336
line: 1,
2437
column: 1

test/fixtures/input.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
第三章
3131

32+
二十回
33+
3234
一億百十万人
3335

3436
百八つのボタン

test/fixtures/output.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
第3章
3131

32+
20回
33+
3234
1億百十万人
3335

3436
108つのボタン

0 commit comments

Comments
 (0)