Skip to content

Commit

Permalink
Fix: adjust tag names and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Sep 10, 2023
1 parent 94684b6 commit f4154d1
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 48 deletions.
14 changes: 11 additions & 3 deletions src/common/casualReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function today(reference: ReferenceWithTimezone): ParsingComponents {
* 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 @@ -45,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 @@ -60,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 @@ -81,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 @@ -91,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 @@ -106,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 @@ -116,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 @@ -126,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 @@ -136,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;
}
8 changes: 1 addition & 7 deletions src/locales/en/parsers/ENCasualDateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,24 @@ export default class ENCasualDateParser extends AbstractParserWithWordBoundaryCh
switch (lowerText) {
case "now":
component = references.now(context.reference);
component.addTag("ENCasualDateParser/extract/now");
break;

case "today":
component = references.today(context.reference);
component.addTag("ENCasualDateParser/extract/today");
break;

case "yesterday":
component = references.yesterday(context.reference);
component.addTag("ENCasualDateParser/extract/yesterday");
break;

case "tomorrow":
case "tmr":
case "tmrw":
component = references.tomorrow(context.reference);
component.addTag("ENCasualDateParser/extract/tomorrow");
break;

case "tonight":
component = references.tonight(context.reference);
component.addTag("ENCasualDateParser/extract/tonight");
break;

default:
Expand All @@ -53,11 +48,10 @@ export default class ENCasualDateParser extends AbstractParserWithWordBoundaryCh

assignSimilarDate(component, targetDate);
component.imply("hour", 0);
component.addTag("ENCasualDateParser/extract/last_night");
}
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");
}
}
3 changes: 2 additions & 1 deletion src/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ export class ParsingResult implements ParsedResult {
}

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)} ...}]`;
}
}
39 changes: 36 additions & 3 deletions test/result.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ParsingComponents, ReferenceWithTimezone } from "../src/results";
import { ParsingComponents, ParsingResult, ReferenceWithTimezone } from "../src/results";

test("Test - Create & manipulate date results", () => {
test("Test - Create & manipulate parsing components", () => {
const reference = new ReferenceWithTimezone(new Date());
const components = new ParsingComponents(reference, { year: 2014, month: 11, day: 24 });

expect(components.get("year")).toBe(2014);
expect(components.get("month")).toBe(11);
expect(components.get("day")).toBe(24);
expect(components.date()).toBeDefined();
expect(components.tags().size).toBe(0);

// null
expect(components.get("weekday")).toBeNull();
Expand All @@ -23,13 +24,45 @@ test("Test - Create & manipulate date results", () => {
expect(components.get("weekday")).toBe(2);
expect(components.isCertain("weekday")).toBe(true);

// "imply" doesn't overrides "assign"
// "imply" doesn't override "assign"
components.imply("year", 2013);
expect(components.get("year")).toBe(2014);

// "assign" overrides "assign"
components.assign("year", 2013);
expect(components.get("year")).toBe(2013);

components.addTag("custom/testing_component_tag");
expect(components.tags().size).toBe(1);
expect(components.tags()).toContain("custom/testing_component_tag");
expect(components.toString()).toContain("custom/testing_component_tag");
});

test("Test - Create & manipulate parsing results", () => {
const reference = new ReferenceWithTimezone(new Date());
const text = "1 - 2 hour later";

const startComponents = ParsingComponents.createRelativeFromReference(reference, { "hour": 1 }).addTag(
"custom/testing_start_component_tag"
);

const endComponents = ParsingComponents.createRelativeFromReference(reference, { "hour": 2 }).addTag(
"custom/testing_end_component_tag"
);

const result = new ParsingResult(reference, 0, text, startComponents, endComponents);

// The result's date() should be the same as the start components' date()
expect(result.date()).toStrictEqual(startComponents.date());

// The result's tags should include both the start and end components' tags
expect(result.tags()).toContain("custom/testing_start_component_tag");
expect(result.tags()).toContain("custom/testing_end_component_tag");

// The result's toString() should include the text and tags
expect(result.toString()).toContain(text);
expect(result.toString()).toContain("custom/testing_start_component_tag");
expect(result.toString()).toContain("custom/testing_end_component_tag");
});

test("Test - Calendar checking with implied components", () => {
Expand Down
12 changes: 6 additions & 6 deletions test/system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,25 @@ test("Test - Add custom parser with tags example", () => {
day: 25,
month: 12,
})
.addTag("CustomParser/chirstmas");
.addTag("parser/ChristmasDayParser");
},
});

testSingleCase(custom, "Doing something tomorrow", (result) => {
expect(result.text).toBe("tomorrow");
expect(result.tags()).toContain("ENCasualDateParser/extract/tomorrow");
expect(result.tags()).toContain("parser/ENCasualDateParser");
});

testSingleCase(custom, "I'll arrive at 2.30AM on Christmas", (result) => {
expect(result.text).toBe("at 2.30AM on Christmas");
expect(result.tags()).toContain("CustomParser/chirstmas");
// TODO: Check for time (expression) parsing tags
expect(result.tags()).toContain("parser/ChristmasDayParser");
expect(result.tags()).toContain("parser/ENTimeExpressionParser");
});

testSingleCase(custom, "I'll arrive at Christmas night", (result) => {
expect(result.text).toBe("Christmas night");
expect(result.tags()).toContain("CustomParser/chirstmas");
// TODO: Check for time (casual) parsing tags
expect(result.tags()).toContain("parser/ChristmasDayParser");
expect(result.tags()).toContain("parser/ENCasualTimeParser");
});

// TODO: Check if the merge date range combine tags correctly
Expand Down

0 comments on commit f4154d1

Please sign in to comment.