Skip to content

Commit

Permalink
fix: correct range case
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Dec 29, 2016
1 parent 71343ac commit 6e7a33e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/textlint-rule-date-weekday-mismatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function reporter(context, config = {}) {
const text = getSource(node);
const chronoDate = chrono.parse(text);
// ignore "今日" text

chronoDate.filter(textIncludesNumber).forEach(chronoDate => {
const lang = detectLang(Object.keys(chronoDate.tags), preferLang);
if (!lang) {
Expand All @@ -69,27 +70,30 @@ function reporter(context, config = {}) {
try {
$moment = moment(`${kV.year}-${kV.month}-${kV.day}`, "YYYY-MM-DD", lang);
} catch (error) {
console.warn("Maybe textlint-rule-date-weekday-mismatch options was wrong language.", lang);
// parse error is ignore
return;
}
if (!$moment.isValid()) {
return;
}
const slicedText = text.slice(chronoDate.index);
// get (weekday)
const startOfPairSymbol = chronoDate.text.length + chronoDate.index;
const slicedText = text.slice(startOfPairSymbol);
// (match) or (match)
const match = slicedText.match(/\s*?([(])([^()]+)([)])/);
const match = slicedText.match(/^(\s*?[(])([^()]+)([)])/);
if (!match) {
return;
}
const actualDateText = match[0];
const actualTextAll = `${chronoDate.text}${actualDateText}`;
const pairStartSymbol = match[1];// (
const pairStartSymbol = match[1];// ( and padding-left
const pairEndSymbol = match[3]; // )
const maybeWeekdayText = match[2].trim(); // weekday
// 2016年12月30日 (金曜日)
// ^ ^ ^
// chronoDate.index match.index pairStartSymbol.length
const paddingIndex = chronoDate.index + match.index + pairStartSymbol.length;
const paddingIndex = startOfPairSymbol + match.index + pairStartSymbol.length;
// format http://momentjs.com/docs/#/parsing/string-format/
const weekdayPatterns = [
// date-format , symbols
Expand Down
17 changes: 14 additions & 3 deletions test/textlint-rule-date-weekday-mismatch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,24 @@ tester.run("rule", rule, {
]
},
{
text: "2017年1月1日(火)",
output: "2017年1月1日(日)",
text: "2017年1月1日 (火)",
output: "2017年1月1日 (日)",
errors: [
{
message: "2017年1月1日 (火) mismatch weekday.\n2017年1月1日 (火) => 2017年1月1日 (日)",
line: 1,
column: 12
}
]
},
{
text: "2016年12月30日〜2017年1月1日(火)",
output: "2016年12月30日〜2017年1月1日(日)",
errors: [
{
message: "2017年1月1日(火) mismatch weekday.\n2017年1月1日(火) => 2017年1月1日(日)",
line: 1,
column: 11
column: 23
}
]
},
Expand Down

0 comments on commit 6e7a33e

Please sign in to comment.