-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: casual date parsing also imply the similar time to the reference
- Loading branch information
Showing
2 changed files
with
69 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters