Skip to content

Commit

Permalink
Removing edge-case time checking for [midnight] and [tomorrow]
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Jan 15, 2021
1 parent ea32abb commit d898037
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/common/parsers/ISOFormatParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { AbstractParserWithWordBoundaryChecking } from "./AbstractParserWithWord
// - TZD = (Z or +hh:mm or -hh:mm)
const PATTERN = new RegExp(
"([0-9]{4})\\-([0-9]{1,2})\\-([0-9]{1,2})" +
"(?:T" + //..
"([0-9]{1,2}):([0-9]{1,2})" + // hh:mm
"(?::([0-9]{1,2})(?:\\.(\\d{1,4}))?)?" + // :ss.s
"(?:Z|([+-]\\d{2}):?(\\d{2})?)?" + // TZD (Z or ±hh:mm or ±hhmm or ±hh)
")?" + //..
"(?:T" + //..
"([0-9]{1,2}):([0-9]{1,2})" + // hh:mm
"(?::([0-9]{1,2})(?:\\.(\\d{1,4}))?)?" + // :ss.s
"(?:Z|([+-]\\d{2}):?(\\d{2})?)?" + // TZD (Z or ±hh:mm or ±hhmm or ±hh)
")?" + //..
"(?=\\W|$)",
"i"
);
Expand Down
17 changes: 3 additions & 14 deletions src/utils/dayjs.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import { ParsingComponents } from "../results";
import dayjs from "dayjs";

/**
* Assign the next day or 'tomorrow'.
* This includes checking for edge cases e.g. mentioned 'tomorrow' after midnight
*/
export function assignTheNextDay(component: ParsingComponents, targetDayJs: dayjs.Dayjs) {
if (targetDayJs.hour() > 1) {
targetDayJs = targetDayJs.add(1, "day");
assignSimilarDate(component, targetDayJs);
implySimilarTime(component, targetDayJs);
} else {
assignSimilarDate(component, targetDayJs);
component.imply("hour", 12);
component.imply("minute", 0);
component.imply("second", 0);
}
targetDayJs = targetDayJs.add(1, "day");
assignSimilarDate(component, targetDayJs);
implySimilarTime(component, targetDayJs);
}

export function assignSimilarDate(component: ParsingComponents, targetDayJs: dayjs.Dayjs) {
Expand Down
7 changes: 3 additions & 4 deletions test/de/de_casual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ test("Test - Casual date expression", function () {
//expect(result.start.get("hour")).toBe(8);
});

// Say.."Tomorrow" in the late night (1 AM)
testSingleCase(chrono.de, "Die Deadline ist morgen", new Date(2012, 7, 10, 1), (result) => {
testSingleCase(chrono.de, "Die Deadline ist morgen", new Date(2012, 8 - 1, 10, 1), (result) => {
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(10);
//expect(result.start.get("hour")).toBe(8);
expect(result.start.get("day")).toBe(11);
expect(result.start.get("hour")).toBe(1);
});

testSingleCase(chrono.de, "Die Deadline war gestern", new Date(2012, 7, 10, 12), (result) => {
Expand Down
7 changes: 4 additions & 3 deletions test/en/en_casual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ test("Test - Single Expression", () => {
expect(result.start).toBeDate(new Date(2012, 7, 11, 17, 10));
});

// Say.."Tomorrow" in the late night (1 AM)
testSingleCase(chrono.casual, "The Deadline is Tomorrow", new Date(2012, 7, 10, 1), (result) => {
expect(result.start).toBeDate(new Date(2012, 7, 10, 12));
expect(result.start).toBeDate(new Date(2012, 7, 11, 1));
});

testSingleCase(chrono.casual, "The Deadline was yesterday", new Date(2012, 7, 10, 12), (result) => {
Expand Down Expand Up @@ -122,11 +121,13 @@ test("Test - Single Expression", () => {
expect(result.start.get("hour")).toBe(0);
});

// "Midnight" at 0~1AM, assume it's the coming midnight of following day
// This is similar to "Tomorrow" at 0~1AM
testSingleCase(chrono.casual, "The Deadline was midnight ", new Date(2012, 7, 10, 1), (result) => {
expect(result.text).toBe("midnight");
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(10);
expect(result.start.get("day")).toBe(11);
expect(result.start.get("hour")).toBe(0);
});
});
Expand Down
3 changes: 1 addition & 2 deletions test/fr/fr_casual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ test("Test - Single Expression", function () {
expect(result.start).toBeDate(new Date(2012, 7, 11, 12));
});

// Say.."Demain" in the late night (1 AM)
testSingleCase(chrono.fr, "La deadline est demain", new Date(2012, 7, 10, 1), (result) => {
expect(result.start).toBeDate(new Date(2012, 7, 10, 12));
expect(result.start).toBeDate(new Date(2012, 7, 11, 1));
});

testSingleCase(chrono.fr, "La deadline était hier", new Date(2012, 7, 10, 12), (result) => {
Expand Down
2 changes: 1 addition & 1 deletion test/pt/pt_casual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test("Test - Single Expression", function () {
});

testSingleCase(chrono.pt, "O prazo é Amanhã", new Date(2012, 7, 10, 1), (result) => {
expect(result.start).toBeDate(new Date(2012, 7, 10, 12));
expect(result.start).toBeDate(new Date(2012, 7, 11, 1));
});

testSingleCase(chrono.pt, "O prazo foi ontem", new Date(2012, 7, 10, 12), (result) => {
Expand Down

0 comments on commit d898037

Please sign in to comment.