Skip to content

Commit

Permalink
New: (en) Add support for weekend/weekday mentioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Oct 20, 2024
1 parent d10b164 commit 3a15d3d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/locales/en/parsers/ENWeekdayParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { WEEKDAY_DICTIONARY } from "../constants";
import { matchAnyPattern } from "../../../utils/pattern";
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
import { createParsingComponentsAtWeekday } from "../../../common/calculation/weekdays";
import { Weekday } from "../../../types";

const PATTERN = new RegExp(
"(?:(?:\\,|\\(|\\()\\s*)?" +
"(?:on\\s*?)?" +
"(?:(this|last|past|next)\\s*)?" +
`(${matchAnyPattern(WEEKDAY_DICTIONARY)})` +
`(${matchAnyPattern(WEEKDAY_DICTIONARY)}|weekend|weekday)` +
"(?:\\s*(?:\\,|\\)|\\)))?" +
"(?:\\s*(this|last|past|next)\\s*week)?" +
"(?=\\W|$)",
Expand All @@ -25,9 +26,7 @@ export default class ENWeekdayParser extends AbstractParserWithWordBoundaryCheck
return PATTERN;
}

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

const weekday_word = match[WEEKDAY_GROUP].toLowerCase();
let weekday;
if (WEEKDAY_DICTIONARY[weekday_word] !== undefined) {
weekday = WEEKDAY_DICTIONARY[weekday_word];
} else if (weekday_word == "weekend") {
// This depends on what days are weekend setting, but typically:
// 'This/next weekend' means the coming Saturday, 'last weekend' means last Sunday.
weekday = modifier == "last" ? Weekday.SUNDAY : Weekday.SATURDAY;
} else if (weekday_word == "weekday") {
// In English, the "weekday" means any day of the week except weekend.
// This also depends on what days are weekend setting, but typically:
// - On weekend ref, this means the coming Monday or last Friday.
// - On weekday ref, this means the next/last working day.
const refWeekday = context.reference.getDateWithAdjustedTimezone().getDay();
if (refWeekday == Weekday.SUNDAY || refWeekday == Weekday.SATURDAY) {
weekday = modifier == "last" ? Weekday.FRIDAY : Weekday.MONDAY;
} else {
weekday = refWeekday - 1;
weekday = modifier == "last" ? weekday - 1 : weekday + 1;
weekday = (weekday % 5) + 1;
}
} else {
return null;
}

return createParsingComponentsAtWeekday(context.reference, weekday, modifier);
}
}
43 changes: 43 additions & 0 deletions test/en/en_weekday.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,49 @@ test("Test - Weekday casual `Next` guessing", function () {
}
});

test("Test - Casual 'weekend' and 'weekday'", () => {
{
const refDateOnFriday = new Date(2024, 10 - 1, 18, 12, 0);
testSingleCase(chrono.casual, "last weekend", refDateOnFriday, (result, text) => {
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2024, 10 - 1, 13, 12)); // Sunday
});
testSingleCase(chrono.casual, "this weekend", refDateOnFriday, (result, text) => {
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2024, 10 - 1, 19, 12)); // Saturday
});
testSingleCase(chrono.casual, "next weekend", refDateOnFriday, (result, text) => {
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2024, 10 - 1, 26, 12)); // Saturday
});
}
});

test("Test - Casual 'weekday' mentioning", () => {
{
const refDateOnFriday = new Date(2024, 10 - 1, 18, 12, 0);
testSingleCase(chrono.casual, "last weekday", refDateOnFriday, (result, text) => {
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2024, 10 - 1, 17, 12)); // Thursday
});
testSingleCase(chrono.casual, "next weekday", refDateOnFriday, (result, text) => {
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2024, 10 - 1, 21, 12)); // Monday
});
}
{
const refDateOnSaturday = new Date(2024, 10 - 1, 19, 12, 0);
testSingleCase(chrono.casual, "last weekday", refDateOnSaturday, (result, text) => {
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2024, 10 - 1, 18, 12)); // Friday
});
testSingleCase(chrono.casual, "next weekday", refDateOnSaturday, (result, text) => {
expect(result.text).toBe(text);
expect(result.start).toBeDate(new Date(2024, 10 - 1, 21, 12)); // Monday
});
}
});

test("Test - Weekday With Casual Time", function () {
testSingleCase(chrono.casual, "Lets meet on Tuesday morning", new Date(2015, 3, 18), (result) => {
expect(result.index).toBe(10);
Expand Down

0 comments on commit 3a15d3d

Please sign in to comment.