Skip to content

Commit 08e4dc4

Browse files
author
Wanasit Tanakitrungruang
committedSep 16, 2024·
Fix: (en) incorrect timeunit words extraction
1 parent 31bcae3 commit 08e4dc4

9 files changed

+38
-8
lines changed
 

‎src/locales/en/constants.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export const TIME_UNITS_NO_ABBR_PATTERN = repeatedTimeunitPattern(
277277
TIME_UNIT_CONNECTOR_PATTERN
278278
);
279279

280-
export function parseTimeUnits(timeunitText): TimeUnits {
280+
export function parseTimeUnits(timeunitText): null | TimeUnits {
281281
const fragments = {};
282282
let remainingText = timeunitText;
283283
let match = SINGLE_TIME_UNIT_REGEX.exec(remainingText);
@@ -286,10 +286,16 @@ export function parseTimeUnits(timeunitText): TimeUnits {
286286
remainingText = remainingText.substring(match[0].length).trim();
287287
match = SINGLE_TIME_UNIT_REGEX.exec(remainingText);
288288
}
289+
if (Object.keys(fragments).length == 0) {
290+
return null;
291+
}
289292
return fragments;
290293
}
291294

292295
function collectDateTimeFragment(fragments, match) {
296+
if (match[0].match(/^[a-zA-Z]+$/)) {
297+
return;
298+
}
293299
const num = parseNumberPattern(match[1]);
294300
const unit = TIME_UNIT_DICTIONARY[match[2].toLowerCase()];
295301
fragments[unit] = num;

‎src/locales/en/parsers/ENTimeUnitAgoFormatParser.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ export default class ENTimeUnitAgoFormatParser extends AbstractParserWithWordBou
1818

1919
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
2020
const timeUnits = parseTimeUnits(match[1]);
21+
if (!timeUnits) {
22+
return null;
23+
}
2124
const outputTimeUnits = reverseTimeUnits(timeUnits);
22-
2325
return ParsingComponents.createRelativeFromReference(context.reference, outputTimeUnits);
2426
}
2527
}

‎src/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ export default class ENTimeUnitCasualRelativeFormatParser extends AbstractParser
1919
return this.allowAbbreviations ? PATTERN : PATTERN_NO_ABBR;
2020
}
2121

22-
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents {
22+
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
2323
const prefix = match[1].toLowerCase();
2424
let timeUnits = parseTimeUnits(match[2]);
25+
if (!timeUnits) {
26+
return null;
27+
}
2528
switch (prefix) {
2629
case "last":
2730
case "past":
2831
case "-":
2932
timeUnits = reverseTimeUnits(timeUnits);
3033
break;
3134
}
32-
3335
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
3436
}
3537
}

‎src/locales/en/parsers/ENTimeUnitLaterFormatParser.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export default class ENTimeUnitLaterFormatParser extends AbstractParserWithWordB
2121
}
2222

2323
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
24-
const fragments = parseTimeUnits(match[GROUP_NUM_TIMEUNITS]);
25-
return ParsingComponents.createRelativeFromReference(context.reference, fragments);
24+
const timeUnits = parseTimeUnits(match[GROUP_NUM_TIMEUNITS]);
25+
if (!timeUnits) {
26+
return null;
27+
}
28+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
2629
}
2730
}

‎src/locales/en/parsers/ENTimeUnitWithinFormatParser.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ export default class ENTimeUnitWithinFormatParser extends AbstractParserWithWord
3333
return context.option.forwardDate ? PATTERN_WITH_OPTIONAL_PREFIX : PATTERN_WITH_PREFIX;
3434
}
3535

36-
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents {
36+
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
3737
// Exclude "for the unit" phases, e.g. "for the year"
3838
if (match[0].match(/^for\s*the\s*\w+/)) {
3939
return null;
4040
}
41-
4241
const timeUnits = parseTimeUnits(match[1]);
42+
if (!timeUnits) {
43+
return null;
44+
}
4345
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
4446
}
4547
}

‎test/en/en_time_units_ago.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,7 @@ test("Test - Negative cases", function () {
347347
testUnexpectedResult(chrono, "15 hours 29 min");
348348
testUnexpectedResult(chrono, "a few hour");
349349
testUnexpectedResult(chrono, "5 days");
350+
351+
testUnexpectedResult(chrono, "am ago");
352+
testUnexpectedResult(chrono, "them ago");
350353
});

‎test/en/en_time_units_casual_relative.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,7 @@ test("Test - Negative cases", () => {
164164
testUnexpectedResult(chrono.casual, "1 m", new Date(2015, 7 - 1, 10, 12, 14));
165165
testUnexpectedResult(chrono.casual, "the day", new Date(2015, 7 - 1, 10, 12, 14));
166166
testUnexpectedResult(chrono.casual, "a day", new Date(2015, 7 - 1, 10, 12, 14));
167+
168+
testUnexpectedResult(chrono, "+am");
169+
testUnexpectedResult(chrono, "+them");
167170
});

‎test/en/en_time_units_later.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,7 @@ test("Test - Plus after reference", () => {
354354
expect(result.start.get("minute")).toBe(10);
355355
});
356356
});
357+
358+
test("Test - Negative cases", () => {
359+
testUnexpectedResult(chrono, "tell them later");
360+
});

‎test/en/en_time_units_within.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,8 @@ test("Test - Forward date option", () => {
408408
expect(result.start.get("day")).toBe(1);
409409
});
410410
});
411+
412+
test("Test - Negative cases", () => {
413+
testUnexpectedResult(chrono, "in am");
414+
testUnexpectedResult(chrono, "in them");
415+
});

0 commit comments

Comments
 (0)
Please sign in to comment.