Skip to content

Commit

Permalink
Fix: support "the x after .." pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Mar 5, 2023
1 parent 175c94e commit 6760ca0
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/locales/en/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ export const TIME_UNIT_DICTIONARY: { [word: string]: OpUnitType | QUnitType } =

export const NUMBER_PATTERN = `(?:${matchAnyPattern(
INTEGER_WORD_DICTIONARY
)}|[0-9]+|[0-9]+\\.[0-9]+|half(?:\\s{0,2}an?)?|an?\\b(?:\\s{0,2}few)?|few|several|a?\\s{0,2}couple\\s{0,2}(?:of)?)`;
)}|[0-9]+|[0-9]+\\.[0-9]+|half(?:\\s{0,2}an?)?|an?\\b(?:\\s{0,2}few)?|few|several|the|a?\\s{0,2}couple\\s{0,2}(?:of)?)`;

export function parseNumberPattern(match: string): number {
const num = match.toLowerCase();
if (INTEGER_WORD_DICTIONARY[num] !== undefined) {
return INTEGER_WORD_DICTIONARY[num];
} else if (num === "a" || num === "an") {
} else if (num === "a" || num === "an" || num == "the") {
return 1;
} else if (num.match(/few/)) {
return 3;
Expand Down
11 changes: 10 additions & 1 deletion src/locales/en/parsers/ENCasualDateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/
import { assignSimilarDate } from "../../../utils/dayjs";
import * as references from "../../../common/casualReferences";

const PATTERN = /(now|today|tonight|tomorrow|tmr|tmrw|yesterday|last\s*night)(?=\W|$)/i;
const PATTERN =
/(now|today|tonight|tomorrow|tmr|tmrw|yesterday|last\s*night|(?<!\d\s*)day\s*after\s*tomorrow|(?<!\d\s*)day\s*before\s*yesterday)(?=\W|$)/i;

export default class ENCasualDateParser extends AbstractParserWithWordBoundaryChecking {
innerPattern(context: ParsingContext): RegExp {
Expand Down Expand Up @@ -45,6 +46,14 @@ export default class ENCasualDateParser extends AbstractParserWithWordBoundaryCh
component.imply("hour", 0);
}

if (lowerText.match(/after\s*tomorrow/)) {
return references.theDayAfter(context.reference, 2);
}

if (lowerText.match(/before\s*yesterday/)) {
return references.theDayBefore(context.reference, 2);
}

break;
}

Expand Down
36 changes: 36 additions & 0 deletions test/en/en_time_units_ago.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ test("Test - Single Expression", function () {

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

testSingleCase(chrono, "the min before", new Date(2012, 7, 10, 12, 14), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe("the min before");
expect(result.start.get("hour")).toBe(12);
expect(result.start.get("minute")).toBe(13);
expect(result.start.get("meridiem")).toBe(Meridiem.PM);

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

test("Test - Single Expression (Casual)", function () {
Expand Down Expand Up @@ -264,6 +274,32 @@ test("Test - Nested time ago", function () {
});
});

test("Test - Before with reference", () => {
testSingleCase(chrono, "2 day before today", new Date(2012, 7, 10), (result) => {
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(8);
});

testSingleCase(chrono, "the day before yesterday", new Date(2012, 7, 10), (result) => {
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(8);
});

testSingleCase(chrono, "2 day before yesterday", new Date(2012, 7, 10), (result) => {
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(7);
});

testSingleCase(chrono, "a week before yesterday", new Date(2012, 7, 10), (result) => {
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(2);
});
});

test("Test - Negative cases", function () {
testUnexpectedResult(chrono, "15 hours 29 min");
testUnexpectedResult(chrono, "a few hour");
Expand Down
2 changes: 2 additions & 0 deletions test/en/en_time_units_casual_relative.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,6 @@ test("Test - Minus '-' sign", () => {
test("Test - Negative cases", () => {
testUnexpectedResult(chrono.casual, "3y", new Date(2015, 7 - 1, 10, 12, 14));
testUnexpectedResult(chrono.casual, "1 m", new Date(2015, 7 - 1, 10, 12, 14));
testUnexpectedResult(chrono.casual, "the day", new Date(2015, 7 - 1, 10, 12, 14));
testUnexpectedResult(chrono.casual, "a day", new Date(2015, 7 - 1, 10, 12, 14));
});
38 changes: 38 additions & 0 deletions test/en/en_time_units_later.test.ts