Skip to content

Commit 4ed6d4f

Browse files
committed
refactor: avoid using let
1 parent da86588 commit 4ed6d4f

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

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

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,43 @@ const addYearToDateText = (dateText, year, lang) => {
7373
// Default: prepend year and a space
7474
return `${year} ${dateText}`;
7575
}
76+
/**
77+
* Create chronoDates array, optionally supplementing year if needed.
78+
* @param {string} text
79+
* @param {Object} options
80+
* @param {string} options.preferLang
81+
* @param {boolean} options.useCurrentYearIfMissing
82+
* @param {number} options.currentYear
83+
* @returns {Array}
84+
*/
85+
function createChronoDates(text, options) {
86+
const { preferLang, useCurrentYearIfMissing, currentYear } = options;
87+
const chronoDates = chrono.parse(text);
88+
if (!useCurrentYearIfMissing) {
89+
return chronoDates;
90+
}
91+
chronoDates.forEach(chronoDate => {
92+
// If year is not specified in the parsed result
93+
if (
94+
chronoDate.start &&
95+
chronoDate.start.knownValues.year === undefined
96+
) {
97+
// Detect language for the date string
98+
const lang = detectLang(Object.keys(chronoDate.tags), preferLang);
99+
if (!lang) {
100+
return;
101+
}
102+
// Re-parse the text with the year added (using currentYear)
103+
const newText = addYearToDateText(chronoDate.text, currentYear, lang);
104+
const reparsed = chrono.parse(newText, undefined, {forwardDate: true});
105+
// If reparsed successfully, update knownValues with year/month/day
106+
if (reparsed && reparsed[0] && reparsed[0].start && reparsed[0].start.knownValues) {
107+
Object.assign(chronoDate.start.knownValues, reparsed[0].start.knownValues);
108+
}
109+
}
110+
});
111+
return chronoDates;
112+
}
76113
/**
77114
*
78115
* @param context
@@ -90,32 +127,13 @@ function reporter(context, config = {}) {
90127
return {
91128
[Syntax.Str](node){
92129
const text = getSource(node);
93-
let chronoDates = chrono.parse(text);
94-
// Add current year if missing and option is enabled
95-
if (useCurrentYearIfMissing) {
96-
// Use currentYear option if provided, otherwise use system year
97-
const currentYear = currentYearOption ?? (new Date()).getFullYear();
98-
chronoDates.forEach(chronoDate => {
99-
// If year is not specified in the parsed result
100-
if (
101-
chronoDate.start &&
102-
chronoDate.start.knownValues.year === undefined
103-
) {
104-
// Detect language for the date string
105-
const lang = detectLang(Object.keys(chronoDate.tags), preferLang);
106-
if (!lang) {
107-
return;
108-
}
109-
// Re-parse the text with the year added (using currentYear)
110-
const newText = addYearToDateText(chronoDate.text, currentYear, lang);
111-
const reparsed = chrono.parse(newText, undefined, {forwardDate: true});
112-
// If reparsed successfully, update knownValues with year/month/day
113-
if (reparsed && reparsed[0] && reparsed[0].start && reparsed[0].start.knownValues) {
114-
Object.assign(chronoDate.start.knownValues, reparsed[0].start.knownValues);
115-
}
116-
}
117-
});
118-
}
130+
// Use helper function to create chronoDates
131+
const currentYear = currentYearOption ?? (new Date()).getFullYear();
132+
const chronoDates = createChronoDates(text, {
133+
preferLang,
134+
useCurrentYearIfMissing,
135+
currentYear
136+
});
119137
// ignore "今日" text
120138
// ignore not valid data
121139
const filteredChronoDates = chronoDates.filter(textIncludesNumber).filter(yearMonthDayShouldKnownValues);

0 commit comments

Comments
 (0)