Skip to content

Commit 412dcea

Browse files
committed
docs(readme): Update README
1 parent 711fb8f commit 412dcea

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ textlint rule that found mismatch date and weekday.
77
Correct date and weekday.
88

99
2016-12-29(Thursday)
10+
2016-12-29(木曜)
1011

1112
**NG**:
1213

@@ -20,6 +21,8 @@ Incorrect date and weekday.
2021

2122
## Supported lang
2223

24+
Automatically detect language from your text!
25+
2326
- en
2427
- ja(日本語)
2528
- es
@@ -55,6 +58,42 @@ Via CLI
5558
textlint --rule date-weekday-mismatch README.md
5659
```
5760

61+
## Options
62+
63+
- `lang`: string
64+
- Default: none(Automatically detect language)
65+
- Specify language for date string
66+
67+
```json
68+
{
69+
"rules": {
70+
"date-weekday-mismatch": {
71+
"lang": "ja"
72+
}
73+
}
74+
}
75+
```
76+
77+
For example, `2016-12-30` is `en` by default.
78+
But, You can specify `2016-12-30` is `ja-JP` text by options
79+
80+
```json
81+
{
82+
"rules": {
83+
"date-weekday-mismatch": {
84+
"lang": "ja-JP"
85+
}
86+
}
87+
}
88+
```
89+
90+
language format following ISO 639-1.
91+
92+
e.g.) `en-US`, `en`, `ja` etc..
93+
94+
- [ISO 639-1 - Wikipedia](https://en.wikipedia.org/wiki/ISO_639-1 "ISO 639-1 - Wikipedia")
95+
- [Moment.js | Docs](http://momentjs.com/docs/#/i18n/changing-locale/ "Moment.js | Docs")
96+
5897
## Acknowledge
5998

6099
- [日付曜日矛盾 // Speaker Deck](https://speakerdeck.com/shirayu/ri-fu-yao-ri-mao-dun "日付曜日矛盾 // Speaker Deck")

src/textlint-rule-date-weekday-mismatch.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ const moment = require('moment');
77
* @type {[RegExp]}
88
*/
99
const supportedLang = [
10-
[/^EN/, "en"],
11-
[/^JP/, "ja"],
12-
[/^ES/, "es"],
13-
[/^FR/, "fr"],
14-
[/^ZH/, "zh"]
10+
[/^EN/i, "en"],
11+
[/^JP/i, "ja"],
12+
[/^ES/i, "es"],
13+
[/^FR/i, "fr"],
14+
[/^ZH/i, "zh"]
1515
];
1616

1717
/**
1818
* detect lang and return language string
1919
* @param {string[]} tags
20+
* @param {string} [preferLang]
2021
* @returns {string|null}
2122
*/
22-
const detectLang = (tags) => {
23+
const detectLang = (tags, preferLang) => {
24+
if (preferLang) {
25+
return preferLang;
26+
}
2327
const targetLangs = supportedLang.filter(([langRegExp]) => {
2428
return tags.some(tag => langRegExp.test(tag));
2529
});
@@ -29,7 +33,13 @@ const detectLang = (tags) => {
2933
const selectedLang = targetLangs[0];
3034
return selectedLang[1];
3135
};
32-
function reporter(context) {
36+
/**
37+
*
38+
* @param context
39+
* @param {Object} [config]
40+
*/
41+
function reporter(context, config = {}) {
42+
const preferLang = config.lang;
3343
const {Syntax, RuleError, report, fixer, getSource} = context;
3444
if (typeof Intl === "undefined") {
3545
throw new Error("Not support your Node.js/browser. should be use latest version.");
@@ -39,13 +49,11 @@ function reporter(context) {
3949
const text = getSource(node);
4050
const chronoDate = chrono.parse(text);
4151
chronoDate.forEach(chronoDate => {
42-
const lang = detectLang(Object.keys(chronoDate.tags));
52+
const lang = detectLang(Object.keys(chronoDate.tags), preferLang);
4353
if (!lang) {
4454
// not found lang
4555
return;
4656
}
47-
// change lang
48-
moment.locale(lang);
4957
// get weekday from actual date string
5058
const kV = chronoDate.start.knownValues;
5159
const $moment = moment(`${kV.year}-${kV.month}-${kV.day}`, "YYYY-MM-DD", lang);
@@ -67,9 +75,9 @@ function reporter(context) {
6775
// format http://momentjs.com/docs/#/parsing/string-format/
6876
const weekdayPatterns = [
6977
// date-format , symbols
70-
["dd", moment.weekdaysMin()],
71-
["ddd", moment.weekdaysShort()],
72-
["dddd", moment.weekdays()]
78+
["dd", moment.localeData(lang).weekdaysMin()],
79+
["ddd", moment.localeData(lang).weekdaysShort()],
80+
["dddd", moment.localeData(lang).weekdays()]
7381
];
7482
weekdayPatterns.forEach(([format, symbols]) => {
7583
if (symbols.indexOf(maybeWeekdayText) === -1) {

test/textlint-rule-date-weekday-mismatch-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ tester.run("rule", rule, {
3434
}
3535
]
3636
},
37+
// options
38+
{
39+
text: "2016-12-29(月)",
40+
output: "2016-12-29(木)",
41+
options: {
42+
lang: "ja-JP"
43+
},
44+
errors: [
45+
{
46+
message: "2016-12-29(月) mismatch weekday.\n2016-12-29(月) => 2016-12-29(木)",
47+
line: 1,
48+
column: 12
49+
}
50+
]
51+
},
3752
{
3853
text: "2016年12月29日(月曜日)",
3954
output: "2016年12月29日(木曜日)",

0 commit comments

Comments
 (0)