Skip to content

Commit

Permalink
Merge pull request #534 from wanasit/debugging-tags
Browse files Browse the repository at this point in the history
New: Introduce Parsing Tags
  • Loading branch information
wanasit authored Sep 10, 2023
2 parents aafee81 + f4154d1 commit a89099d
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 46 deletions.
2 changes: 2 additions & 0 deletions src/calculation/mergingCalculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@ export function mergeDateTimeComponent(
}
}

dateTimeComponent.addTags(dateComponent.tags());
dateTimeComponent.addTags(timeComponent.tags());
return dateTimeComponent;
}
16 changes: 13 additions & 3 deletions src/common/casualReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function now(reference: ReferenceWithTimezone): ParsingComponents {
if (reference.timezoneOffset !== null) {
component.assign("timezoneOffset", targetDate.utcOffset());
}
component.addTag("casualReference/now");
return component;
}

Expand All @@ -25,14 +26,15 @@ export function today(reference: ReferenceWithTimezone): ParsingComponents {
const component = new ParsingComponents(reference, {});
assignSimilarDate(component, targetDate);
implySimilarTime(component, targetDate);
component.addTag("casualReference/today");
return component;
}

/**
* The previous day. Imply the same time.
*/
export function yesterday(reference: ReferenceWithTimezone): ParsingComponents {
return theDayBefore(reference, 1);
return theDayBefore(reference, 1).addTag("casualReference/yesterday");
}

export function theDayBefore(reference: ReferenceWithTimezone, numDay: number): ParsingComponents {
Expand All @@ -43,7 +45,7 @@ export function theDayBefore(reference: ReferenceWithTimezone, numDay: number):
* The following day with dayjs.assignTheNextDay()
*/
export function tomorrow(reference: ReferenceWithTimezone): ParsingComponents {
return theDayAfter(reference, 1);
return theDayAfter(reference, 1).addTag("casualReference/tomorrow");
}

export function theDayAfter(reference: ReferenceWithTimezone, nDays: number): ParsingComponents {
Expand All @@ -58,9 +60,10 @@ export function theDayAfter(reference: ReferenceWithTimezone, nDays: number): Pa
export function tonight(reference: ReferenceWithTimezone, implyHour = 22): ParsingComponents {
const targetDate = dayjs(reference.instant);
const component = new ParsingComponents(reference, {});
assignSimilarDate(component, targetDate);
component.imply("hour", implyHour);
component.imply("meridiem", Meridiem.PM);
assignSimilarDate(component, targetDate);
component.addTag("casualReference/tonight");
return component;
}

Expand All @@ -79,6 +82,7 @@ export function evening(reference: ReferenceWithTimezone, implyHour = 20): Parsi
const component = new ParsingComponents(reference, {});
component.imply("meridiem", Meridiem.PM);
component.imply("hour", implyHour);
component.addTag("casualReference/evening");
return component;
}

Expand All @@ -89,6 +93,8 @@ export function yesterdayEvening(reference: ReferenceWithTimezone, implyHour = 2
assignSimilarDate(component, targetDate);
component.imply("hour", implyHour);
component.imply("meridiem", Meridiem.PM);
component.addTag("casualReference/yesterday");
component.addTag("casualReference/evening");
return component;
}

Expand All @@ -104,6 +110,7 @@ export function midnight(reference: ReferenceWithTimezone): ParsingComponents {
component.imply("minute", 0);
component.imply("second", 0);
component.imply("millisecond", 0);
component.addTag("casualReference/midnight");
return component;
}

Expand All @@ -114,6 +121,7 @@ export function morning(reference: ReferenceWithTimezone, implyHour = 6): Parsin
component.imply("minute", 0);
component.imply("second", 0);
component.imply("millisecond", 0);
component.addTag("casualReference/morning");
return component;
}

Expand All @@ -124,6 +132,7 @@ export function afternoon(reference: ReferenceWithTimezone, implyHour = 15): Par
component.imply("minute", 0);
component.imply("second", 0);
component.imply("millisecond", 0);
component.addTag("casualReference/afternoon");
return component;
}

Expand All @@ -134,5 +143,6 @@ export function noon(reference: ReferenceWithTimezone): ParsingComponents {
component.imply("minute", 0);
component.imply("second", 0);
component.imply("millisecond", 0);
component.addTag("casualReference/noon");
return component;
}
19 changes: 12 additions & 7 deletions src/locales/en/parsers/ENCasualDateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,30 @@ export default class ENCasualDateParser extends AbstractParserWithWordBoundaryCh
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents | ParsingResult {
let targetDate = dayjs(context.refDate);
const lowerText = match[0].toLowerCase();
const component = context.createParsingComponents();
let component = context.createParsingComponents();

switch (lowerText) {
case "now":
return references.now(context.reference);
component = references.now(context.reference);
break;

case "today":
return references.today(context.reference);
component = references.today(context.reference);
break;

case "yesterday":
return references.yesterday(context.reference);
component = references.yesterday(context.reference);
break;

case "tomorrow":
case "tmr":
case "tmrw":
return references.tomorrow(context.reference);
component = references.tomorrow(context.reference);
break;

case "tonight":
return references.tonight(context.reference);
component = references.tonight(context.reference);
break;

default:
if (lowerText.match(/last\s*night/)) {
Expand All @@ -46,7 +51,7 @@ export default class ENCasualDateParser extends AbstractParserWithWordBoundaryCh
}
break;
}

component.addTag("parser/ENCasualDateParser");
return component;
}
}
21 changes: 15 additions & 6 deletions src/locales/en/parsers/ENCasualTimeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@ export default class ENCasualTimeParser extends AbstractParserWithWordBoundaryCh
return PATTERN;
}
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
let component = null;
switch (match[1].toLowerCase()) {
case "afternoon":
return casualReferences.afternoon(context.reference);
component = casualReferences.afternoon(context.reference);
break;
case "evening":
case "night":
return casualReferences.evening(context.reference);
component = casualReferences.evening(context.reference);
break;
case "midnight":
return casualReferences.midnight(context.reference);
component = casualReferences.midnight(context.reference);
break;
case "morning":
return casualReferences.morning(context.reference);
component = casualReferences.morning(context.reference);
break;
case "noon":
case "midday":
return casualReferences.noon(context.reference);
component = casualReferences.noon(context.reference);
break;
}
return null;
if (component) {
component.addTag("parser/ENCasualTimeParser");
}
return component;
}
}
46 changes: 24 additions & 22 deletions src/locales/en/parsers/ENTimeExpressionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,36 @@ export default class ENTimeExpressionParser extends AbstractTimeExpressionParser

extractPrimaryTimeComponents(context: ParsingContext, match: RegExpMatchArray): null | ParsingComponents {
const components = super.extractPrimaryTimeComponents(context, match);
if (components) {
if (match[0].endsWith("night")) {
const hour = components.get("hour");
if (hour >= 6 && hour < 12) {
components.assign("hour", components.get("hour") + 12);
components.assign("meridiem", Meridiem.PM);
} else if (hour < 6) {
components.assign("meridiem", Meridiem.AM);
}
}
if (!components) {
return components;
}

if (match[0].endsWith("afternoon")) {
if (match[0].endsWith("night")) {
const hour = components.get("hour");
if (hour >= 6 && hour < 12) {
components.assign("hour", components.get("hour") + 12);
components.assign("meridiem", Meridiem.PM);
const hour = components.get("hour");
if (hour >= 0 && hour <= 6) {
components.assign("hour", components.get("hour") + 12);
}
} else if (hour < 6) {
components.assign("meridiem", Meridiem.AM);
}
}

if (match[0].endsWith("afternoon")) {
components.assign("meridiem", Meridiem.PM);
const hour = components.get("hour");
if (hour >= 0 && hour <= 6) {
components.assign("hour", components.get("hour") + 12);
}
}

if (match[0].endsWith("morning")) {
components.assign("meridiem", Meridiem.AM);
const hour = components.get("hour");
if (hour < 12) {
components.assign("hour", components.get("hour"));
}
if (match[0].endsWith("morning")) {
components.assign("meridiem", Meridiem.AM);
const hour = components.get("hour");
if (hour < 12) {
components.assign("hour", components.get("hour"));
}
}

return components;
return components.addTag("parser/ENTimeExpressionParser");
}
}
38 changes: 34 additions & 4 deletions src/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class ParsingComponents implements ParsedComponents {
private knownValues: { [c in Component]?: number };
private impliedValues: { [c in Component]?: number };
private reference: ReferenceWithTimezone;
private _tags = new Set<string>();

constructor(reference: ReferenceWithTimezone, knownComponents?: { [c in Component]?: number }) {
this.reference = reference;
Expand Down Expand Up @@ -155,9 +156,11 @@ export class ParsingComponents implements ParsedComponents {
}

toString() {
return `[ParsingComponents {knownValues: ${JSON.stringify(this.knownValues)}, impliedValues: ${JSON.stringify(
this.impliedValues
)}}, reference: ${JSON.stringify(this.reference)}]`;
return `[ParsingComponents {
tags: ${JSON.stringify(Array.from(this._tags).sort())},
knownValues: ${JSON.stringify(this.knownValues)},
impliedValues: ${JSON.stringify(this.impliedValues)}},
reference: ${JSON.stringify(this.reference)}]`;
}

dayjs() {
Expand All @@ -170,6 +173,22 @@ export class ParsingComponents implements ParsedComponents {
return new Date(date.getTime() + timezoneAdjustment * 60000);
}

addTag(tag: string): ParsingComponents {
this._tags.add(tag);
return this;
}

addTags(tags: string[] | Set<string>): ParsingComponents {
for (const tag of tags) {
this._tags.add(tag);
}
return this;
}

tags(): Set<string> {
return new Set(this._tags);
}

private dateWithoutTimezoneAdjustment() {
const date = new Date(
this.get("year"),
Expand Down Expand Up @@ -271,7 +290,18 @@ export class ParsingResult implements ParsedResult {
return this.start.date();
}

tags(): Set<string> {
const combinedTags: Set<string> = new Set(this.start.tags());
if (this.end) {
for (const tag of this.end.tags()) {
combinedTags.add(tag);
}
}
return combinedTags;
}

toString() {
return `[ParsingResult {index: ${this.index}, text: '${this.text}', ...}]`;
const tags = Array.from(this.tags()).sort();
return `[ParsingResult {index: ${this.index}, text: '${this.text}', tags: ${JSON.stringify(tags)} ...}]`;
}
}
12 changes: 11 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ export interface ParsedResult {
readonly end?: ParsedComponents;

/**
* Create a javascript date object (from the result.start).
* @return a javascript date object created from the `result.start`.
*/
date(): Date;

/**
* @return debugging tags combined of the `result.start` and `result.end`.
*/
tags(): Set<string>;
}

/**
Expand All @@ -105,6 +110,11 @@ export interface ParsedComponents {
* @return a javascript date object.
*/
date(): Date;

/**
* @return debugging tags of the parsed component.
*/
tags(): Set<string>;
}

export type Component =
Expand Down
Loading

0 comments on commit a89099d

Please sign in to comment.