Skip to content

Commit

Permalink
fix: ignore relative word like "今日", "明日"
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Dec 29, 2016
1 parent 626ca00 commit 9663f96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 18 additions & 3 deletions src/textlint-rule-date-weekday-mismatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ const supportedLang = [
[/^FR/i, "fr"],
[/^ZH/i, "zh"]
];

/**
* text should be includes number
* @param {Object} chronoDate
* @returns {boolean}
*/
const textIncludesNumber = (chronoDate) => {
return /[0-9-]/.test(chronoDate.text);
};
/**
* detect lang and return language string
* @param {string[]} tags
Expand Down Expand Up @@ -44,19 +51,27 @@ function reporter(context, config = {}) {
if (typeof Intl === "undefined") {
throw new Error("Not support your Node.js/browser. should be use latest version.");
}

return {
[Syntax.Str](node){
const text = getSource(node);
const chronoDate = chrono.parse(text);
chronoDate.forEach(chronoDate => {
// ignore "今日" text
chronoDate.filter(textIncludesNumber).forEach(chronoDate => {
const lang = detectLang(Object.keys(chronoDate.tags), preferLang);
if (!lang) {
// not found lang
return;
}
// get weekday from actual date string
const kV = chronoDate.start.knownValues;
const $moment = moment(`${kV.year}-${kV.month}-${kV.day}`, "YYYY-MM-DD", lang);
let $moment;
try {
$moment = moment(`${kV.year}-${kV.month}-${kV.day}`, "YYYY-MM-DD", lang);
} catch (error) {
// parse error is ignore
return;
}
if (!$moment.isValid()) {
return;
}
Expand Down
4 changes: 3 additions & 1 deletion test/textlint-rule-date-weekday-mismatch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ tester.run("rule", rule, {
"2016-12-29(Thursday)",
"2016-12-29(Tsu)",
// invalid date should be ignored
"11月 25日 (火曜日) "
"11月 25日 (火曜日) ",
// ignore relative word
"今日は日曜 (火曜日) "
],
invalid: [
// single match
Expand Down

0 comments on commit 9663f96

Please sign in to comment.