Skip to content

Commit 3a15d3d

Browse files
author
Wanasit Tanakitrungruang
committedOct 20, 2024·
New: (en) Add support for weekend/weekday mentioning
1 parent d10b164 commit 3a15d3d

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed
 

‎src/locales/en/parsers/ENWeekdayParser.ts

+28-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { WEEKDAY_DICTIONARY } from "../constants";
44
import { matchAnyPattern } from "../../../utils/pattern";
55
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
66
import { createParsingComponentsAtWeekday } from "../../../common/calculation/weekdays";
7+
import { Weekday } from "../../../types";
78

89
const PATTERN = new RegExp(
910
"(?:(?:\\,|\\(|\\()\\s*)?" +
1011
"(?:on\\s*?)?" +
1112
"(?:(this|last|past|next)\\s*)?" +
12-
`(${matchAnyPattern(WEEKDAY_DICTIONARY)})` +
13+
`(${matchAnyPattern(WEEKDAY_DICTIONARY)}|weekend|weekday)` +
1314
"(?:\\s*(?:\\,|\\)|\\)))?" +
1415
"(?:\\s*(this|last|past|next)\\s*week)?" +
1516
"(?=\\W|$)",
@@ -25,9 +26,7 @@ export default class ENWeekdayParser extends AbstractParserWithWordBoundaryCheck
2526
return PATTERN;
2627
}
2728

28-
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents {
29-
const dayOfWeek = match[WEEKDAY_GROUP].toLowerCase();
30-
const weekday = WEEKDAY_DICTIONARY[dayOfWeek];
29+
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents | null {
3130
const prefix = match[PREFIX_GROUP];
3231
const postfix = match[POSTFIX_GROUP];
3332
let modifierWord = prefix || postfix;
@@ -43,6 +42,31 @@ export default class ENWeekdayParser extends AbstractParserWithWordBoundaryCheck
4342
modifier = "this";
4443
}
4544

45+
const weekday_word = match[WEEKDAY_GROUP].toLowerCase();
46+
let weekday;
47+
if (WEEKDAY_DICTIONARY[weekday_word] !== undefined) {
48+
weekday = WEEKDAY_DICTIONARY[weekday_word];
49+
} else if (weekday_word == "weekend") {
50+
// This depends on what days are weekend setting, but typically:
51+
// 'This/next weekend' means the coming Saturday, 'last weekend' means last Sunday.
52+
weekday = modifier == "last" ? Weekday.SUNDAY : Weekday.SATURDAY;
53+
} else if (weekday_word == "weekday") {
54+
// In English, the "weekday" means any day of the week except weekend.
55+
// This also depends on what days are weekend setting, but typically:
56+
// - On weekend ref, this means the coming Monday or last Friday.
57+
// - On weekday ref, this means the next/last working day.
58+
const refWeekday = context.reference.getDateWithAdjustedTimezone().getDay();
59+
if (refWeekday == Weekday.SUNDAY || refWeekday == Weekday.SATURDAY) {
60+
weekday = modifier == "last" ? Weekday.FRIDAY : Weekday.MONDAY;
61+
} else {
62+
weekday = refWeekday - 1;
63+
weekday = modifier == "last" ? weekday - 1 : weekday + 1;
64+
weekday = (weekday % 5) + 1;
65+
}
66+
} else {
67+
return null;
68+
}
69+
4670
return createParsingComponentsAtWeekday(context.reference, weekday, modifier);
4771
}
4872
}

‎test/en/en_weekday.test.ts

+43
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,49 @@ test("Test - Weekday casual `Next` guessing", function () {
219219
}
220220
});
221221

222+
test("Test - Casual 'weekend' and 'weekday'", () => {
223+
{
224+
const refDateOnFriday = new Date(2024, 10 - 1, 18, 12, 0);
225+
testSingleCase(chrono.casual, "last weekend", refDateOnFriday, (result, text) => {
226+
expect(result.text).toBe(text);
227+
expect(result.start).toBeDate(new Date(2024, 10 - 1, 13, 12)); // Sunday
228+
});
229+
testSingleCase(chrono.casual, "this weekend", refDateOnFriday, (result, text) => {
230+
expect(result.text).toBe(text);
231+
expect(result.start).toBeDate(new Date(2024, 10 - 1, 19, 12)); // Saturday
232+
});
233+
testSingleCase(chrono.casual, "next weekend", refDateOnFriday, (result, text) => {
234+
expect(result.text).toBe(text);
235+
expect(result.start).toBeDate(new Date(2024, 10 - 1, 26, 12)); // Saturday
236+
});
237+
}
238+
});
239+
240+
test("Test - Casual 'weekday' mentioning", () => {
241+
{
242+
const refDateOnFriday = new Date(2024, 10 - 1, 18, 12, 0);
243+
testSingleCase(chrono.casual, "last weekday", refDateOnFriday, (result, text) => {
244+
expect(result.text).toBe(text);
245+
expect(result.start).toBeDate(new Date(2024, 10 - 1, 17, 12)); // Thursday
246+
});
247+
testSingleCase(chrono.casual, "next weekday", refDateOnFriday, (result, text) => {
248+
expect(result.text).toBe(text);
249+
expect(result.start).toBeDate(new Date(2024, 10 - 1, 21, 12)); // Monday
250+
});
251+
}
252+
{
253+
const refDateOnSaturday = new Date(2024, 10 - 1, 19, 12, 0);
254+
testSingleCase(chrono.casual, "last weekday", refDateOnSaturday, (result, text) => {
255+
expect(result.text).toBe(text);
256+
expect(result.start).toBeDate(new Date(2024, 10 - 1, 18, 12)); // Friday
257+
});
258+
testSingleCase(chrono.casual, "next weekday", refDateOnSaturday, (result, text) => {
259+
expect(result.text).toBe(text);
260+
expect(result.start).toBeDate(new Date(2024, 10 - 1, 21, 12)); // Monday
261+
});
262+
}
263+
});
264+
222265
test("Test - Weekday With Casual Time", function () {
223266
testSingleCase(chrono.casual, "Lets meet on Tuesday morning", new Date(2015, 3, 18), (result) => {
224267
expect(result.index).toBe(10);

0 commit comments

Comments
 (0)
Please sign in to comment.