Skip to content

Commit

Permalink
Fix: little-endian month and year case
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Dec 30, 2023
1 parent 3f2e9ad commit c201115
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/locales/en/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export default class ENDefaultConfiguration {
*/
createCasualConfiguration(littleEndian = false): Configuration {
const option = this.createConfiguration(false, littleEndian);
option.parsers.unshift(new ENCasualDateParser());
option.parsers.unshift(new ENCasualTimeParser());
option.parsers.unshift(new ENMonthNameParser());
option.parsers.unshift(new ENRelativeDateFormatParser());
option.parsers.unshift(new ENTimeUnitCasualRelativeFormatParser());
option.parsers.push(new ENCasualDateParser());
option.parsers.push(new ENCasualTimeParser());
option.parsers.push(new ENMonthNameParser());
option.parsers.push(new ENRelativeDateFormatParser());
option.parsers.push(new ENTimeUnitCasualRelativeFormatParser());
return option;
}

Expand All @@ -52,7 +52,7 @@ export default class ENDefaultConfiguration {
new SlashDateFormatParser(littleEndian),
new ENTimeUnitWithinFormatParser(strictMode),
new ENMonthNameLittleEndianParser(),
new ENMonthNameMiddleEndianParser(),
new ENMonthNameMiddleEndianParser(/*shouldSkipYearLikeDate=*/ littleEndian),
new ENWeekdayParser(),
new ENCasualYearMonthDayParser(),
new ENSlashMonthFormatParser(),
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export function parseOrdinalNumberPattern(match: string): number {

//-----------------------------

export const YEAR_PATTERN = `(?:[1-9][0-9]{0,3}\\s{0,2}(?:BE|AD|BC|BCE|CE)|[1-2][0-9]{3}|[5-9][0-9])`;
export const YEAR_PATTERN = `(?:[1-9][0-9]{0,3}\\s{0,2}(?:BE|AD|BC|BCE|CE)|[1-2][0-9]{3}|[5-9][0-9]|2[0-5])`;
export function parseYear(match: string): number {
if (/BE/i.test(match)) {
// Buddhist Era
Expand Down
25 changes: 20 additions & 5 deletions src/locales/en/parsers/ENMonthNameMiddleEndianParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ const YEAR_GROUP = 4;
* - January 12:00
* - January 12.44
* - January 1222344
* - January 21 (when shouldSkipYearLikeDate=true)
*/
export default class ENMonthNameMiddleEndianParser extends AbstractParserWithWordBoundaryChecking {
shouldSkipYearLikeDate: boolean;

constructor(shouldSkipYearLikeDate: boolean) {
super();
this.shouldSkipYearLikeDate = shouldSkipYearLikeDate;
}

innerPattern(): RegExp {
return PATTERN;
}
Expand All @@ -49,10 +57,18 @@ export default class ENMonthNameMiddleEndianParser extends AbstractParserWithWor
return null;
}

const components = context.createParsingComponents({
day: day,
month: month,
});
// Skip the case where the day looks like a year (ex: January 21)
if (this.shouldSkipYearLikeDate) {
if (!match[DATE_TO_GROUP] && !match[YEAR_GROUP] && match[DATE_GROUP].match(/^2[0-5]$/)) {
return null;
}
}
const components = context
.createParsingComponents({
day: day,
month: month,
})
.addTag("parser/ENMonthNameMiddleEndianParser");

if (match[YEAR_GROUP]) {
const year = parseYear(match[YEAR_GROUP]);
Expand All @@ -61,7 +77,6 @@ export default class ENMonthNameMiddleEndianParser extends AbstractParserWithWor
const year = findYearClosestToRef(context.refDate, day, month);
components.imply("year", year);
}

if (!match[DATE_TO_GROUP]) {
return components;
}
Expand Down
1 change: 1 addition & 0 deletions src/locales/en/parsers/ENMonthNameParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class ENMonthNameParser extends AbstractParserWithWordBoundaryChe
match.index + match[0].length
);
result.start.imply("day", 1);
result.start.addTag("parser/ENMonthNameParser");

const month = MONTH_DICTIONARY[monthName];
result.start.assign("month", month);
Expand Down
16 changes: 16 additions & 0 deletions test/en/en_month_name_middle_endian.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as chrono from "../../src";
import { testSingleCase, testWithExpectedDate, testUnexpectedResult } from "../test_util";
import { configuration } from "../../src/locales/en";

test("Test - Single Expression", () => {
testSingleCase(chrono, "She is getting married soon (July 2017).", (result) => {
Expand Down Expand Up @@ -357,6 +358,21 @@ test("Test - year 90's parsing", () => {
});
});

test("Test - Skip year-like on little-endian configuration", () => {
const chronoMiddleEndian = new chrono.Chrono(chrono.en.configuration.createCasualConfiguration(false));
testSingleCase(chronoMiddleEndian, "Dec. 21", new Date(2023, 12, 10), (result) => {
expect(result.start.get("year")).toBe(2023);
expect(result.start.get("month")).toBe(12);
expect(result.start.get("day")).toBe(21);
});

const chronoLittleEndian = new chrono.Chrono(chrono.en.configuration.createCasualConfiguration(true));
testSingleCase(chronoLittleEndian, "Dec. 21", new Date(2023, 12, 10), (result) => {
expect(result.start.get("year")).toBe(2021);
expect(result.start.get("month")).toBe(12);
});
});

test("Test - Impossible Dates (Strict Mode)", () => {
testUnexpectedResult(chrono.strict, "August 32, 2014");

Expand Down

0 comments on commit c201115

Please sign in to comment.