Skip to content

Commit

Permalink
Fix: slash date format accept invalid dates
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Nov 19, 2022
1 parent 996a998 commit 49e0c63
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
13 changes: 7 additions & 6 deletions src/common/parsers/SlashDateFormatParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ export default class SlashDateFormatParser implements Parser {
}

extract(context: ParsingContext, match: RegExpMatchArray): ParsingResult {
if (match[OPENING_GROUP] == "/" || match[ENDING_GROUP] == "/") {
// Long skip, if there is some overlapping like:
// XX[/YY/ZZ]
// [XX/YY/]ZZ
match.index += match[0].length;
return;
// Because of how pattern is executed on remaining text in `chrono.ts`, the character before the match could
// still be a number (e.g. X[X/YY/ZZ] or XX[/YY/ZZ] or [XX/YY/]ZZ). We want to check and skip them.
if (match[OPENING_GROUP].length == 0 && match.index > 0 && match.index < context.text.length) {
const previousChar = context.text[match.index - 1];
if (previousChar >= "0" && previousChar <= "9") {
return;
}
}

const index = match.index + match[OPENING_GROUP].length;
Expand Down
23 changes: 12 additions & 11 deletions test/en/en_slash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,24 @@ test("Test - Splitter variances patterns", function () {
testWithExpectedDate(chrono, "05-25-2015", expectDate);
testWithExpectedDate(chrono, "05/25/2015", expectDate);
testWithExpectedDate(chrono, "05.25.2015", expectDate);
testWithExpectedDate(chrono, "/05/25/2015", expectDate);

// Also, guessing ambiguous date
testWithExpectedDate(chrono, "25/05/2015", expectDate);
});

test("Test - Impossible Dates and Unexpected Results", function () {
testUnexpectedResult(chrono, "8/32/2014", new Date(2012, 7, 10));

testUnexpectedResult(chrono, "8/32", new Date(2012, 7, 10));

testUnexpectedResult(chrono, "2/29/2014", new Date(2012, 7, 10));

testUnexpectedResult(chrono, "2014/22/29", new Date(2012, 7, 10));

testUnexpectedResult(chrono, "2014/13/22", new Date(2012, 7, 10));

testUnexpectedResult(chrono, "80-32-89-89", new Date(2012, 7, 10));
testUnexpectedResult(chrono, "8/32/2014");
testUnexpectedResult(chrono, "8/32");
testUnexpectedResult(chrono, "2/29/2014");
testUnexpectedResult(chrono, "2014/22/29");
testUnexpectedResult(chrono, "2014/13/22");
testUnexpectedResult(chrono, "80-32-89-89");
testUnexpectedResult(chrono, "02/29/2022");
testUnexpectedResult(chrono, "06/31/2022");
testUnexpectedResult(chrono, "06/-31/2022");
testUnexpectedResult(chrono, "18/13/2022");
testUnexpectedResult(chrono, "15/28/2022");
});

test("Test - forward dates only option", function () {
Expand Down
11 changes: 11 additions & 0 deletions test/en/negative_cases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ test("Test - Skip hyphenated numbers pattern", () => {
testUnexpectedResult(chrono, "2200-25");
});

test("Test - Skip impossible dates/times", () => {
testUnexpectedResult(chrono, "February 29, 2022");
testUnexpectedResult(chrono, "02/29/2022");

testUnexpectedResult(chrono, "June 31, 2022");
testUnexpectedResult(chrono, "06/31/2022");

testUnexpectedResult(chrono, "14PM");
testUnexpectedResult(chrono, "25:12");
});

test("Test - Skip version number pattern", () => {
testUnexpectedResult(chrono, "Version: 1.1.3");

Expand Down

0 comments on commit 49e0c63

Please sign in to comment.