Skip to content

Commit

Permalink
New: casual date parsing also imply the similar time to the reference
Browse files Browse the repository at this point in the history
  • Loading branch information
wanasit committed Jul 18, 2020
1 parent e1a3c04 commit 17bb16f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 29 deletions.
90 changes: 65 additions & 25 deletions src/locales/en/parsers/ENCasualDateParser.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,85 @@
import { Parser, ParsingContext } from "../../../chrono";
import { ParsingContext } from "../../../chrono";
import { ParsingComponents, ParsingResult } from "../../../results";
import dayjs from "dayjs";
import { Meridiem } from "../../../index";
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";

export default class ENCasualDateParser extends AbstractParserWithWordBoundaryChecking {
innerPattern(context: ParsingContext): RegExp {
return /(now|today|tonight|last\s*night|tomorrow|tmr|yesterday)(?=\W|$)/i;
return /(now|today|tonight|tomorrow|tmr|yesterday|last\s*night)(?=\W|$)/i;
}

innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents | ParsingResult {
let targetDate = dayjs(context.refDate);
const lowerText = match[0].toLowerCase();
const component = context.createParsingComponents();

if (lowerText == "tonight") {
// Normally means this coming midnight
component.imply("hour", 22);
component.imply("meridiem", Meridiem.PM);
} else if (/^tomorrow|^tmr/.test(lowerText)) {
// Check not "Tomorrow" on late night
if (targetDate.hour() > 1) {
targetDate = targetDate.add(1, "day");
}
} else if (/^yesterday/.test(lowerText)) {
targetDate = targetDate.add(-1, "day");
} else if (lowerText.match(/last\s*night/)) {
component.imply("hour", 0);
if (targetDate.hour() > 6) {
switch (lowerText) {
case "now":
assignDate(component, targetDate);
component.assign("hour", targetDate.hour());
component.assign("minute", targetDate.minute());
component.assign("second", targetDate.second());
component.assign("millisecond", targetDate.millisecond());
break;

case "today":
assignDate(component, targetDate);
implySimilarTime(component, targetDate);
break;

case "tonight":
component.imply("hour", 22);
component.imply("meridiem", Meridiem.PM);
assignDate(component, targetDate);
break;

case "tomorrow":
case "tmr":
// Check not "Tomorrow" on late night
if (targetDate.hour() > 1) {
targetDate = targetDate.add(1, "day");
assignDate(component, targetDate);
implySimilarTime(component, targetDate);
} else {
assignDate(component, targetDate);
component.imply("hour", 12);
component.imply("minute", 0);
component.imply("second", 0);
}
break;

case "yesterday":
targetDate = targetDate.add(-1, "day");
}
} else if (lowerText.match("now")) {
component.assign("hour", targetDate.hour());
component.assign("minute", targetDate.minute());
component.assign("second", targetDate.second());
component.assign("millisecond", targetDate.millisecond());
assignDate(component, targetDate);
implySimilarTime(component, targetDate);
break;

default:
if (lowerText.match(/last\s*night/)) {
if (targetDate.hour() > 6) {
targetDate = targetDate.add(-1, "day");
}

assignDate(component, targetDate);
component.imply("hour", 0);
}

break;
}

component.assign("day", targetDate.date());
component.assign("month", targetDate.month() + 1);
component.assign("year", targetDate.year());
return component;
}
}

function assignDate(component: ParsingComponents, targetDayJs: dayjs.Dayjs) {
component.assign("day", targetDayJs.date());
component.assign("month", targetDayJs.month() + 1);
component.assign("year", targetDayJs.year());
}

function implySimilarTime(component: ParsingComponents, targetDayJs: dayjs.Dayjs) {
component.imply("hour", targetDayJs.hour());
component.imply("minute", targetDayJs.minute());
component.imply("second", targetDayJs.second());
}
8 changes: 4 additions & 4 deletions test/en/en_casual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test("Test - Single Expression", () => {
expect(result.start).toBeDate(new Date(2012, 7, 10, 8, 9, 10, 11));
});

testSingleCase(chrono.casual, "The Deadline is today", new Date(2012, 7, 10, 12), (result) => {
testSingleCase(chrono.casual, "The Deadline is today", new Date(2012, 7, 10, 14, 12), (result) => {
expect(result.index).toBe(16);
expect(result.text).toBe("today");

Expand All @@ -28,10 +28,10 @@ test("Test - Single Expression", () => {
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(10);

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

testSingleCase(chrono.casual, "The Deadline is Tomorrow", new Date(2012, 7, 10, 12), (result) => {
testSingleCase(chrono.casual, "The Deadline is Tomorrow", new Date(2012, 7, 10, 17, 10), (result) => {
expect(result.index).toBe(16);
expect(result.text).toBe("Tomorrow");

Expand All @@ -40,7 +40,7 @@ test("Test - Single Expression", () => {
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(11);

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

// Say.."Tomorrow" in the late night (1 AM)
Expand Down

0 comments on commit 17bb16f

Please sign in to comment.