Skip to content

Commit

Permalink
Fix: ForwardRefiner to activate on the same weekday
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Sep 25, 2021
1 parent a6631ed commit 699c871
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/common/refiners/ForwardDateRefiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class ForwardDateRefiner implements Refiner {
}

if (result.start.isOnlyWeekdayComponent() && refMoment.isAfter(result.start.dayjs())) {
if (refMoment.day() > result.start.get("weekday")) {
if (refMoment.day() >= result.start.get("weekday")) {
refMoment = refMoment.day(result.start.get("weekday") + 7);
} else {
refMoment = refMoment.day(<number>result.start.get("weekday"));
Expand Down
74 changes: 49 additions & 25 deletions test/en/en_weekday.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,6 @@ test("Test - Single Expression", function () {
expect(result.start).toBeDate(new Date(2012, 7, 6, 12));
});

testSingleCase(
chrono.casual,
"Monday (forward dates only)",
new Date(2012, 7, 9),
{ forwardDate: true },
(result) => {
expect(result.index).toBe(0);
expect(result.text).toBe("Monday");

expect(result.start).not.toBeNull();
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(13);
expect(result.start.get("weekday")).toBe(1);

expect(result.start.isCertain("day")).toBe(false);
expect(result.start.isCertain("month")).toBe(false);
expect(result.start.isCertain("year")).toBe(false);
expect(result.start.isCertain("weekday")).toBe(true);

expect(result.start).toBeDate(new Date(2012, 7, 13, 12));
}
);

testSingleCase(chrono.casual, "Thursday", new Date(2012, 7, 9), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe("Thursday");
Expand Down Expand Up @@ -182,7 +158,31 @@ test("Test - Weekday Overlap", function () {
});
});

test("Test - forward dates only option", function () {
test("Test - forward dates only option", () => {
testSingleCase(
chrono.casual,
"Monday (forward dates only)",
new Date(2012, 7, 9),
{ forwardDate: true },
(result) => {
expect(result.index).toBe(0);
expect(result.text).toBe("Monday");

expect(result.start).not.toBeNull();
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(13);
expect(result.start.get("weekday")).toBe(1);

expect(result.start.isCertain("day")).toBe(false);
expect(result.start.isCertain("month")).toBe(false);
expect(result.start.isCertain("year")).toBe(false);
expect(result.start.isCertain("weekday")).toBe(true);

expect(result.start).toBeDate(new Date(2012, 7, 13, 12));
}
);

testSingleCase(
chrono.casual,
"this Friday to this Monday",
Expand Down Expand Up @@ -219,4 +219,28 @@ test("Test - forward dates only option", function () {
expect(result.end).toBeDate(new Date(2016, 8 - 1, 8, 12));
}
);

testSingleCase(
chrono.casual,
"sunday morning",
new Date("August 15, 2021, 20:00"),
{ forwardDate: true },
(result) => {
expect(result.index).toBe(0);
expect(result.text).toBe("sunday morning");

expect(result.start).not.toBeNull();
expect(result.start.get("year")).toBe(2021);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(22);
expect(result.start.get("weekday")).toBe(0);

expect(result.start.isCertain("day")).toBe(false);
expect(result.start.isCertain("month")).toBe(false);
expect(result.start.isCertain("year")).toBe(false);
expect(result.start.isCertain("weekday")).toBe(true);

expect(result.start).toBeDate(new Date(2021, 8 - 1, 22, 6));
}
);
});

0 comments on commit 699c871

Please sign in to comment.