chrono-node 2.7.5 → 2.7.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cjs/common/parsers/AbstractTimeExpressionParser.js +11 -2
- package/dist/cjs/common/parsers/AbstractTimeExpressionParser.js.map +1 -1
- package/dist/cjs/common/parsers/ISOFormatParser.d.ts +1 -12
- package/dist/cjs/common/parsers/ISOFormatParser.js +30 -28
- package/dist/cjs/common/parsers/ISOFormatParser.js.map +1 -1
- package/dist/cjs/common/parsers/SlashDateFormatParser.js +13 -6
- package/dist/cjs/common/parsers/SlashDateFormatParser.js.map +1 -1
- package/dist/cjs/common/refiners/ForwardDateRefiner.js +8 -5
- package/dist/cjs/common/refiners/ForwardDateRefiner.js.map +1 -1
- package/dist/cjs/locales/en/configuration.js +4 -2
- package/dist/cjs/locales/en/configuration.js.map +1 -1
- package/dist/cjs/locales/en/constants.d.ts +1 -1
- package/dist/cjs/locales/en/constants.js +6 -0
- package/dist/cjs/locales/en/constants.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENMonthNameLittleEndianParser.js +1 -1
- package/dist/cjs/locales/en/parsers/ENMonthNameLittleEndianParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENTimeUnitAgoFormatParser.js +3 -0
- package/dist/cjs/locales/en/parsers/ENTimeUnitAgoFormatParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js +3 -0
- package/dist/cjs/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENTimeUnitLaterFormatParser.js +5 -2
- package/dist/cjs/locales/en/parsers/ENTimeUnitLaterFormatParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENTimeUnitWithinFormatParser.js +3 -0
- package/dist/cjs/locales/en/parsers/ENTimeUnitWithinFormatParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/{ENCasualYearMonthDayParser.d.ts → ENYearMonthDayParser.d.ts} +3 -1
- package/dist/cjs/locales/en/parsers/{ENCasualYearMonthDayParser.js → ENYearMonthDayParser.js} +20 -8
- package/dist/cjs/locales/en/parsers/ENYearMonthDayParser.js.map +1 -0
- package/dist/cjs/locales/en/refiners/ENExtractYearSuffixRefiner.d.ts +5 -0
- package/dist/cjs/locales/en/refiners/ENExtractYearSuffixRefiner.js +31 -0
- package/dist/cjs/locales/en/refiners/ENExtractYearSuffixRefiner.js.map +1 -0
- package/dist/cjs/locales/en/refiners/ENMergeDateTimeRefiner.js +1 -1
- package/dist/cjs/locales/en/refiners/ENMergeDateTimeRefiner.js.map +1 -1
- package/dist/cjs/locales/zh/index.d.ts +12 -1
- package/dist/cjs/locales/zh/index.js +65 -5
- package/dist/cjs/locales/zh/index.js.map +1 -1
- package/dist/cjs/results.js +7 -4
- package/dist/cjs/results.js.map +1 -1
- package/dist/esm/common/parsers/AbstractTimeExpressionParser.js +11 -2
- package/dist/esm/common/parsers/AbstractTimeExpressionParser.js.map +1 -1
- package/dist/esm/common/parsers/ISOFormatParser.d.ts +1 -12
- package/dist/esm/common/parsers/ISOFormatParser.js +30 -28
- package/dist/esm/common/parsers/ISOFormatParser.js.map +1 -1
- package/dist/esm/common/parsers/SlashDateFormatParser.js +13 -6
- package/dist/esm/common/parsers/SlashDateFormatParser.js.map +1 -1
- package/dist/esm/common/refiners/ForwardDateRefiner.js +8 -5
- package/dist/esm/common/refiners/ForwardDateRefiner.js.map +1 -1
- package/dist/esm/locales/en/configuration.js +4 -2
- package/dist/esm/locales/en/configuration.js.map +1 -1
- package/dist/esm/locales/en/constants.d.ts +1 -1
- package/dist/esm/locales/en/constants.js +6 -0
- package/dist/esm/locales/en/constants.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENMonthNameLittleEndianParser.js +1 -1
- package/dist/esm/locales/en/parsers/ENMonthNameLittleEndianParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENTimeUnitAgoFormatParser.js +3 -0
- package/dist/esm/locales/en/parsers/ENTimeUnitAgoFormatParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js +3 -0
- package/dist/esm/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENTimeUnitLaterFormatParser.js +5 -2
- package/dist/esm/locales/en/parsers/ENTimeUnitLaterFormatParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENTimeUnitWithinFormatParser.js +3 -0
- package/dist/esm/locales/en/parsers/ENTimeUnitWithinFormatParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/{ENCasualYearMonthDayParser.d.ts → ENYearMonthDayParser.d.ts} +3 -1
- package/dist/esm/locales/en/parsers/{ENCasualYearMonthDayParser.js → ENYearMonthDayParser.js} +19 -7
- package/dist/esm/locales/en/parsers/ENYearMonthDayParser.js.map +1 -0
- package/dist/esm/locales/en/refiners/ENExtractYearSuffixRefiner.d.ts +5 -0
- package/dist/esm/locales/en/refiners/ENExtractYearSuffixRefiner.js +28 -0
- package/dist/esm/locales/en/refiners/ENExtractYearSuffixRefiner.js.map +1 -0
- package/dist/esm/locales/en/refiners/ENMergeDateTimeRefiner.js +1 -1
- package/dist/esm/locales/en/refiners/ENMergeDateTimeRefiner.js.map +1 -1
- package/dist/esm/locales/zh/index.d.ts +12 -1
- package/dist/esm/locales/zh/index.js +53 -1
- package/dist/esm/locales/zh/index.js.map +1 -1
- package/dist/esm/results.js +7 -4
- package/dist/esm/results.js.map +1 -1
- package/package.json +1 -1
- package/src/common/parsers/AbstractTimeExpressionParser.ts +16 -2
- package/src/common/parsers/ISOFormatParser.ts +30 -32
- package/src/common/parsers/SlashDateFormatParser.ts +13 -10
- package/src/common/refiners/ForwardDateRefiner.ts +8 -5
- package/src/locales/en/configuration.ts +8 -2
- package/src/locales/en/constants.ts +7 -1
- package/src/locales/en/parsers/ENMonthNameLittleEndianParser.ts +1 -1
- package/src/locales/en/parsers/ENTimeUnitAgoFormatParser.ts +3 -1
- package/src/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.ts +4 -2
- package/src/locales/en/parsers/ENTimeUnitLaterFormatParser.ts +5 -2
- package/src/locales/en/parsers/ENTimeUnitWithinFormatParser.ts +4 -2
- package/src/locales/en/parsers/{ENCasualYearMonthDayParser.ts → ENYearMonthDayParser.ts} +18 -7
- package/src/locales/en/refiners/ENExtractYearSuffixRefiner.ts +34 -0
- package/src/locales/en/refiners/ENMergeDateTimeRefiner.ts +1 -1
- package/src/locales/zh/index.ts +73 -1
- package/src/results.ts +8 -5
- package/test/en/en_casual.test.ts +6 -0
- package/test/en/en_inter_std.test.ts +30 -28
- package/test/en/en_merging_relative_dates.test.ts +3 -4
- package/test/en/en_slash.test.ts +62 -42
- package/test/en/en_time_exp.test.ts +88 -0
- package/test/en/en_time_units_ago.test.ts +34 -0
- package/test/en/en_time_units_casual_relative.test.ts +3 -0
- package/test/en/en_time_units_later.test.ts +4 -0
- package/test/en/en_time_units_within.test.ts +5 -0
- package/test/en/en_year.test.ts +26 -1
- package/test/en/{en_casual_year_month_day.test.ts → en_year_month_day.test.ts} +25 -4
- package/test/system.test.ts +2 -0
- package/test/system_timezone.test.ts +11 -0
- package/test/zh/{zh_hans_casual.test.ts → hans/zh_hans_casual.test.ts} +2 -2
- package/test/zh/{zh_hans_date.test.ts → hans/zh_hans_date.test.ts} +2 -2
- package/test/zh/{zh_hans_deadline.test.ts → hans/zh_hans_deadline.test.ts} +2 -2
- package/test/zh/{zh_hans_time_exp.test.ts → hans/zh_hans_time_exp.test.ts} +16 -2
- package/test/zh/{zh_hans_weekday.test.ts → hans/zh_hans_weekday.test.ts} +2 -2
- package/test/zh/{zh_hant_casual.test.ts → hant/zh_hant_casual.test.ts} +2 -2
- package/test/zh/{zh_hant_date.test.ts → hant/zh_hant_date.test.ts} +2 -2
- package/test/zh/{zh_hant_deadline.test.ts → hant/zh_hant_deadline.test.ts} +2 -2
- package/test/zh/{zh_hant_time_exp.test.ts → hant/zh_hant_time_exp.test.ts} +2 -2
- package/test/zh/{zh_hant_weekday.test.ts → hant/zh_hant_weekday.test.ts} +2 -2
- package/test/zh/zh.test.ts +18 -0
- package/dist/cjs/locales/en/parsers/ENCasualYearMonthDayParser.js.map +0 -1
- package/dist/esm/locales/en/parsers/ENCasualYearMonthDayParser.js.map +0 -1
|
@@ -41,18 +41,22 @@ export default class SlashDateFormatParser implements Parser {
|
|
|
41
41
|
extract(context: ParsingContext, match: RegExpMatchArray): ParsingResult {
|
|
42
42
|
// Because of how pattern is executed on remaining text in `chrono.ts`, the character before the match could
|
|
43
43
|
// 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.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
const index = match.index + match[OPENING_GROUP].length;
|
|
45
|
+
const indexEnd = match.index + match[0].length - match[ENDING_GROUP].length;
|
|
46
|
+
if (index > 0) {
|
|
47
|
+
const textBefore = context.text.substring(0, index);
|
|
48
|
+
if (textBefore.match("\\d/?$")) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (indexEnd < context.text.length) {
|
|
53
|
+
const textAfter = context.text.substring(indexEnd);
|
|
54
|
+
if (textAfter.match("^/?\\d")) {
|
|
47
55
|
return;
|
|
48
56
|
}
|
|
49
57
|
}
|
|
50
58
|
|
|
51
|
-
const
|
|
52
|
-
const text = match[0].substr(
|
|
53
|
-
match[OPENING_GROUP].length,
|
|
54
|
-
match[0].length - match[OPENING_GROUP].length - match[ENDING_GROUP].length
|
|
55
|
-
);
|
|
59
|
+
const text = context.text.substring(index, indexEnd);
|
|
56
60
|
|
|
57
61
|
// '1.12', '1.12.12' is more like a version numbers
|
|
58
62
|
if (text.match(/^\d\.\d$/) || text.match(/^\d\.\d{1,2}\.\d{1,2}\s*$/)) {
|
|
@@ -61,14 +65,13 @@ export default class SlashDateFormatParser implements Parser {
|
|
|
61
65
|
|
|
62
66
|
// MM/dd -> OK
|
|
63
67
|
// MM.dd -> NG
|
|
64
|
-
if (!match[YEAR_GROUP] &&
|
|
68
|
+
if (!match[YEAR_GROUP] && text.indexOf("/") < 0) {
|
|
65
69
|
return;
|
|
66
70
|
}
|
|
67
71
|
|
|
68
72
|
const result = context.createParsingResult(index, text);
|
|
69
73
|
let month = parseInt(match[this.groupNumberMonth]);
|
|
70
74
|
let day = parseInt(match[this.groupNumberDay]);
|
|
71
|
-
|
|
72
75
|
if (month < 1 || month > 12) {
|
|
73
76
|
if (month > 12) {
|
|
74
77
|
if (day >= 1 && day <= 12 && month <= 31) {
|
|
@@ -15,7 +15,7 @@ export default class ForwardDateRefiner implements Refiner {
|
|
|
15
15
|
return results;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
results.forEach(
|
|
18
|
+
results.forEach((result) => {
|
|
19
19
|
let refMoment = dayjs(context.refDate);
|
|
20
20
|
|
|
21
21
|
if (result.start.isOnlyTime() && refMoment.isAfter(result.start.dayjs())) {
|
|
@@ -28,6 +28,9 @@ export default class ForwardDateRefiner implements Refiner {
|
|
|
28
28
|
implySimilarDate(result.end, refMoment);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
+
context.debug(() => {
|
|
32
|
+
console.log(`${this.constructor.name} adjusted ${result} time result (${result.start})`);
|
|
33
|
+
});
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
if (result.start.isOnlyWeekdayComponent() && refMoment.isAfter(result.start.dayjs())) {
|
|
@@ -41,7 +44,7 @@ export default class ForwardDateRefiner implements Refiner {
|
|
|
41
44
|
result.start.imply("month", refMoment.month() + 1);
|
|
42
45
|
result.start.imply("year", refMoment.year());
|
|
43
46
|
context.debug(() => {
|
|
44
|
-
console.log(
|
|
47
|
+
console.log(`${this.constructor.name} adjusted ${result} weekday (${result.start})`);
|
|
45
48
|
});
|
|
46
49
|
|
|
47
50
|
if (result.end && result.end.isOnlyWeekdayComponent()) {
|
|
@@ -56,7 +59,7 @@ export default class ForwardDateRefiner implements Refiner {
|
|
|
56
59
|
result.end.imply("month", refMoment.month() + 1);
|
|
57
60
|
result.end.imply("year", refMoment.year());
|
|
58
61
|
context.debug(() => {
|
|
59
|
-
console.log(
|
|
62
|
+
console.log(`${this.constructor.name} adjusted ${result} weekday (${result.end})`);
|
|
60
63
|
});
|
|
61
64
|
}
|
|
62
65
|
}
|
|
@@ -67,13 +70,13 @@ export default class ForwardDateRefiner implements Refiner {
|
|
|
67
70
|
for (let i = 0; i < 3 && refMoment.isAfter(result.start.dayjs()); i++) {
|
|
68
71
|
result.start.imply("year", result.start.get("year") + 1);
|
|
69
72
|
context.debug(() => {
|
|
70
|
-
console.log(
|
|
73
|
+
console.log(`${this.constructor.name} adjusted ${result} year (${result.start})`);
|
|
71
74
|
});
|
|
72
75
|
|
|
73
76
|
if (result.end && !result.end.isCertain("year")) {
|
|
74
77
|
result.end.imply("year", result.end.get("year") + 1);
|
|
75
78
|
context.debug(() => {
|
|
76
|
-
console.log(
|
|
79
|
+
console.log(`${this.constructor.name} adjusted ${result} month (${result.start})`);
|
|
77
80
|
});
|
|
78
81
|
}
|
|
79
82
|
}
|
|
@@ -4,7 +4,7 @@ import ENTimeUnitWithinFormatParser from "./parsers/ENTimeUnitWithinFormatParser
|
|
|
4
4
|
import ENMonthNameLittleEndianParser from "./parsers/ENMonthNameLittleEndianParser";
|
|
5
5
|
import ENMonthNameMiddleEndianParser from "./parsers/ENMonthNameMiddleEndianParser";
|
|
6
6
|
import ENMonthNameParser from "./parsers/ENMonthNameParser";
|
|
7
|
-
import
|
|
7
|
+
import ENYearMonthDayParser from "./parsers/ENYearMonthDayParser";
|
|
8
8
|
import ENSlashMonthFormatParser from "./parsers/ENSlashMonthFormatParser";
|
|
9
9
|
import ENTimeExpressionParser from "./parsers/ENTimeExpressionParser";
|
|
10
10
|
import ENTimeUnitAgoFormatParser from "./parsers/ENTimeUnitAgoFormatParser";
|
|
@@ -23,6 +23,7 @@ import ENTimeUnitCasualRelativeFormatParser from "./parsers/ENTimeUnitCasualRela
|
|
|
23
23
|
import ENMergeRelativeAfterDateRefiner from "./refiners/ENMergeRelativeAfterDateRefiner";
|
|
24
24
|
import ENMergeRelativeFollowByDateRefiner from "./refiners/ENMergeRelativeFollowByDateRefiner";
|
|
25
25
|
import OverlapRemovalRefiner from "../../common/refiners/OverlapRemovalRefiner";
|
|
26
|
+
import ENExtractYearSuffixRefiner from "./refiners/ENExtractYearSuffixRefiner";
|
|
26
27
|
|
|
27
28
|
export default class ENDefaultConfiguration {
|
|
28
29
|
/**
|
|
@@ -54,7 +55,6 @@ export default class ENDefaultConfiguration {
|
|
|
54
55
|
new ENMonthNameLittleEndianParser(),
|
|
55
56
|
new ENMonthNameMiddleEndianParser(/*shouldSkipYearLikeDate=*/ littleEndian),
|
|
56
57
|
new ENWeekdayParser(),
|
|
57
|
-
new ENCasualYearMonthDayParser(),
|
|
58
58
|
new ENSlashMonthFormatParser(),
|
|
59
59
|
new ENTimeExpressionParser(strictMode),
|
|
60
60
|
new ENTimeUnitAgoFormatParser(strictMode),
|
|
@@ -64,6 +64,8 @@ export default class ENDefaultConfiguration {
|
|
|
64
64
|
},
|
|
65
65
|
strictMode
|
|
66
66
|
);
|
|
67
|
+
options.parsers.unshift(new ENYearMonthDayParser(/*strictMonthDateOrder=*/ strictMode));
|
|
68
|
+
|
|
67
69
|
// These relative-dates consideration should be done before other common refiners.
|
|
68
70
|
options.refiners.unshift(new ENMergeRelativeFollowByDateRefiner());
|
|
69
71
|
options.refiners.unshift(new ENMergeRelativeAfterDateRefiner());
|
|
@@ -71,6 +73,10 @@ export default class ENDefaultConfiguration {
|
|
|
71
73
|
|
|
72
74
|
// Re-apply the date time refiner again after the timezone refinement and exclusion in common refiners.
|
|
73
75
|
options.refiners.push(new ENMergeDateTimeRefiner());
|
|
76
|
+
|
|
77
|
+
// Extract year after merging date and time
|
|
78
|
+
options.refiners.push(new ENExtractYearSuffixRefiner());
|
|
79
|
+
|
|
74
80
|
// Keep the date range refiner at the end (after all other refinements).
|
|
75
81
|
options.refiners.push(new ENMergeDateRangeRefiner());
|
|
76
82
|
return options;
|
|
@@ -277,7 +277,7 @@ export const TIME_UNITS_NO_ABBR_PATTERN = repeatedTimeunitPattern(
|
|
|
277
277
|
TIME_UNIT_CONNECTOR_PATTERN
|
|
278
278
|
);
|
|
279
279
|
|
|
280
|
-
export function parseTimeUnits(timeunitText): TimeUnits {
|
|
280
|
+
export function parseTimeUnits(timeunitText): null | TimeUnits {
|
|
281
281
|
const fragments = {};
|
|
282
282
|
let remainingText = timeunitText;
|
|
283
283
|
let match = SINGLE_TIME_UNIT_REGEX.exec(remainingText);
|
|
@@ -286,10 +286,16 @@ export function parseTimeUnits(timeunitText): TimeUnits {
|
|
|
286
286
|
remainingText = remainingText.substring(match[0].length).trim();
|
|
287
287
|
match = SINGLE_TIME_UNIT_REGEX.exec(remainingText);
|
|
288
288
|
}
|
|
289
|
+
if (Object.keys(fragments).length == 0) {
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
289
292
|
return fragments;
|
|
290
293
|
}
|
|
291
294
|
|
|
292
295
|
function collectDateTimeFragment(fragments, match) {
|
|
296
|
+
if (match[0].match(/^[a-zA-Z]+$/)) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
293
299
|
const num = parseNumberPattern(match[1]);
|
|
294
300
|
const unit = TIME_UNIT_DICTIONARY[match[2].toLowerCase()];
|
|
295
301
|
fragments[unit] = num;
|
|
@@ -18,8 +18,10 @@ export default class ENTimeUnitAgoFormatParser extends AbstractParserWithWordBou
|
|
|
18
18
|
|
|
19
19
|
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
|
|
20
20
|
const timeUnits = parseTimeUnits(match[1]);
|
|
21
|
+
if (!timeUnits) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
21
24
|
const outputTimeUnits = reverseTimeUnits(timeUnits);
|
|
22
|
-
|
|
23
25
|
return ParsingComponents.createRelativeFromReference(context.reference, outputTimeUnits);
|
|
24
26
|
}
|
|
25
27
|
}
|
|
@@ -19,9 +19,12 @@ export default class ENTimeUnitCasualRelativeFormatParser extends AbstractParser
|
|
|
19
19
|
return this.allowAbbreviations ? PATTERN : PATTERN_NO_ABBR;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
innerExtract(context: ParsingContext, match: RegExpMatchArray)
|
|
22
|
+
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
|
|
23
23
|
const prefix = match[1].toLowerCase();
|
|
24
24
|
let timeUnits = parseTimeUnits(match[2]);
|
|
25
|
+
if (!timeUnits) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
25
28
|
switch (prefix) {
|
|
26
29
|
case "last":
|
|
27
30
|
case "past":
|
|
@@ -29,7 +32,6 @@ export default class ENTimeUnitCasualRelativeFormatParser extends AbstractParser
|
|
|
29
32
|
timeUnits = reverseTimeUnits(timeUnits);
|
|
30
33
|
break;
|
|
31
34
|
}
|
|
32
|
-
|
|
33
35
|
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
34
36
|
}
|
|
35
37
|
}
|
|
@@ -21,7 +21,10 @@ export default class ENTimeUnitLaterFormatParser extends AbstractParserWithWordB
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
|
|
24
|
-
const
|
|
25
|
-
|
|
24
|
+
const timeUnits = parseTimeUnits(match[GROUP_NUM_TIMEUNITS]);
|
|
25
|
+
if (!timeUnits) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
26
29
|
}
|
|
27
30
|
}
|
|
@@ -33,13 +33,15 @@ export default class ENTimeUnitWithinFormatParser extends AbstractParserWithWord
|
|
|
33
33
|
return context.option.forwardDate ? PATTERN_WITH_OPTIONAL_PREFIX : PATTERN_WITH_PREFIX;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
innerExtract(context: ParsingContext, match: RegExpMatchArray)
|
|
36
|
+
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
|
|
37
37
|
// Exclude "for the unit" phases, e.g. "for the year"
|
|
38
38
|
if (match[0].match(/^for\s*the\s*\w+/)) {
|
|
39
39
|
return null;
|
|
40
40
|
}
|
|
41
|
-
|
|
42
41
|
const timeUnits = parseTimeUnits(match[1]);
|
|
42
|
+
if (!timeUnits) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
43
45
|
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
44
46
|
}
|
|
45
47
|
}
|
|
@@ -11,8 +11,8 @@ import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/
|
|
|
11
11
|
- YYYY.MM.DD
|
|
12
12
|
*/
|
|
13
13
|
const PATTERN = new RegExp(
|
|
14
|
-
`([0-9]{4})[
|
|
15
|
-
`(?:(${matchAnyPattern(MONTH_DICTIONARY)})|([0-9]{1,2}))[
|
|
14
|
+
`([0-9]{4})[-\\.\\/\\s]` +
|
|
15
|
+
`(?:(${matchAnyPattern(MONTH_DICTIONARY)})|([0-9]{1,2}))[-\\.\\/\\s]` +
|
|
16
16
|
`([0-9]{1,2})` +
|
|
17
17
|
"(?=\\W|$)",
|
|
18
18
|
"i"
|
|
@@ -23,23 +23,34 @@ const MONTH_NAME_GROUP = 2;
|
|
|
23
23
|
const MONTH_NUMBER_GROUP = 3;
|
|
24
24
|
const DATE_NUMBER_GROUP = 4;
|
|
25
25
|
|
|
26
|
-
export default class
|
|
26
|
+
export default class ENYearMonthDayParser extends AbstractParserWithWordBoundaryChecking {
|
|
27
|
+
constructor(private strictMonthDateOrder: boolean) {
|
|
28
|
+
super();
|
|
29
|
+
}
|
|
30
|
+
|
|
27
31
|
innerPattern(): RegExp {
|
|
28
32
|
return PATTERN;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
|
|
32
|
-
const
|
|
36
|
+
const year = parseInt(match[YEAR_NUMBER_GROUP]);
|
|
37
|
+
let day = parseInt(match[DATE_NUMBER_GROUP]);
|
|
38
|
+
let month = match[MONTH_NUMBER_GROUP]
|
|
33
39
|
? parseInt(match[MONTH_NUMBER_GROUP])
|
|
34
40
|
: MONTH_DICTIONARY[match[MONTH_NAME_GROUP].toLowerCase()];
|
|
35
41
|
|
|
36
42
|
if (month < 1 || month > 12) {
|
|
43
|
+
if (this.strictMonthDateOrder) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
if (day >= 1 && day <= 12) {
|
|
47
|
+
[month, day] = [day, month];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (day < 1 || day > 31) {
|
|
37
51
|
return null;
|
|
38
52
|
}
|
|
39
53
|
|
|
40
|
-
const year = parseInt(match[YEAR_NUMBER_GROUP]);
|
|
41
|
-
const day = parseInt(match[DATE_NUMBER_GROUP]);
|
|
42
|
-
|
|
43
54
|
return {
|
|
44
55
|
day: day,
|
|
45
56
|
month: month,
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ParsingContext, Refiner } from "../../../chrono";
|
|
2
|
+
import { ParsingResult } from "../../../results";
|
|
3
|
+
import { YEAR_PATTERN, parseYear } from "../constants";
|
|
4
|
+
|
|
5
|
+
const YEAR_SUFFIX_PATTERN = new RegExp(`^\\s*(${YEAR_PATTERN})`, "i");
|
|
6
|
+
const YEAR_GROUP = 1;
|
|
7
|
+
export default class ENExtractYearSuffixRefiner implements Refiner {
|
|
8
|
+
refine(context: ParsingContext, results: ParsingResult[]): ParsingResult[] {
|
|
9
|
+
results.forEach(function (result) {
|
|
10
|
+
if (!result.start.isDateWithUnknownYear()) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const suffix = context.text.substring(result.index + result.text.length);
|
|
15
|
+
const match = YEAR_SUFFIX_PATTERN.exec(suffix);
|
|
16
|
+
if (!match) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
context.debug(() => {
|
|
21
|
+
console.log(`Extracting year: '${match[0]}' into : ${result}`);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const year = parseYear(match[YEAR_GROUP]);
|
|
25
|
+
if (result.end != null) {
|
|
26
|
+
result.end.assign("year", year);
|
|
27
|
+
}
|
|
28
|
+
result.start.assign("year", year);
|
|
29
|
+
result.text += match[0];
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
return results;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -8,6 +8,6 @@ import AbstractMergeDateTimeRefiner from "../../../common/refiners/AbstractMerge
|
|
|
8
8
|
*/
|
|
9
9
|
export default class ENMergeDateTimeRefiner extends AbstractMergeDateTimeRefiner {
|
|
10
10
|
patternBetween(): RegExp {
|
|
11
|
-
return new RegExp("^\\s*(T|at|after|before|on|of
|
|
11
|
+
return new RegExp("^\\s*(T|at|after|before|on|of|,|-|\\.|∙|:)?\\s*$");
|
|
12
12
|
}
|
|
13
13
|
}
|
package/src/locales/zh/index.ts
CHANGED
|
@@ -1,2 +1,74 @@
|
|
|
1
|
-
|
|
1
|
+
import { includeCommonConfiguration } from "../../configurations";
|
|
2
|
+
import { Chrono, Configuration, Parser, Refiner } from "../../chrono";
|
|
3
|
+
import { ParsingResult, ParsingComponents, ReferenceWithTimezone } from "../../results";
|
|
4
|
+
import { Component, ParsedResult, ParsingOption, ParsingReference, Meridiem, Weekday } from "../../types";
|
|
5
|
+
import ExtractTimezoneOffsetRefiner from "../../common/refiners/ExtractTimezoneOffsetRefiner";
|
|
6
|
+
|
|
7
|
+
import ZHHansCasualDateParser from "./hans/parsers/ZHHansCasualDateParser";
|
|
8
|
+
import ZHHansDateParser from "./hans/parsers/ZHHansDateParser";
|
|
9
|
+
import ZHHansDeadlineFormatParser from "./hans/parsers/ZHHansDeadlineFormatParser";
|
|
10
|
+
import ZHHansRelationWeekdayParser from "./hans/parsers/ZHHansRelationWeekdayParser";
|
|
11
|
+
import ZHHansTimeExpressionParser from "./hans/parsers/ZHHansTimeExpressionParser";
|
|
12
|
+
import ZHHansWeekdayParser from "./hans/parsers/ZHHansWeekdayParser";
|
|
13
|
+
|
|
14
|
+
import ZHHantCasualDateParser from "./hant/parsers/ZHHantCasualDateParser";
|
|
15
|
+
import ZHHantDateParser from "./hant/parsers/ZHHantDateParser";
|
|
16
|
+
import ZHHantDeadlineFormatParser from "./hant/parsers/ZHHantDeadlineFormatParser";
|
|
17
|
+
import ZHHantRelationWeekdayParser from "./hant/parsers/ZHHantRelationWeekdayParser";
|
|
18
|
+
import ZHHantTimeExpressionParser from "./hant/parsers/ZHHantTimeExpressionParser";
|
|
19
|
+
import ZHHantWeekdayParser from "./hant/parsers/ZHHantWeekdayParser";
|
|
20
|
+
import ZHHantMergeDateRangeRefiner from "./hant/refiners/ZHHantMergeDateRangeRefiner";
|
|
21
|
+
import ZHHantMergeDateTimeRefiner from "./hant/refiners/ZHHantMergeDateTimeRefiner";
|
|
22
|
+
|
|
23
|
+
export * as hant from "./hant";
|
|
2
24
|
export * as hans from "./hans";
|
|
25
|
+
export { Chrono, Parser, Refiner, ParsingResult, ParsingComponents, ReferenceWithTimezone };
|
|
26
|
+
export { Component, ParsedResult, ParsingOption, ParsingReference, Meridiem, Weekday };
|
|
27
|
+
|
|
28
|
+
export const casual = new Chrono(createCasualConfiguration());
|
|
29
|
+
export const strict = new Chrono(createConfiguration());
|
|
30
|
+
|
|
31
|
+
export function parse(text: string, ref?: ParsingReference | Date, option?: ParsingOption): ParsedResult[] {
|
|
32
|
+
return casual.parse(text, ref, option);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function parseDate(text: string, ref?: ParsingReference | Date, option?: ParsingOption): Date {
|
|
36
|
+
return casual.parseDate(text, ref, option);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @ignore (to be documented later)
|
|
41
|
+
*/
|
|
42
|
+
export function createCasualConfiguration(): Configuration {
|
|
43
|
+
const option = createConfiguration();
|
|
44
|
+
option.parsers.unshift(new ZHHantCasualDateParser());
|
|
45
|
+
return option;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @ignore (to be documented later)
|
|
50
|
+
*/
|
|
51
|
+
export function createConfiguration(): Configuration {
|
|
52
|
+
const configuration = includeCommonConfiguration({
|
|
53
|
+
parsers: [
|
|
54
|
+
new ZHHantDateParser(),
|
|
55
|
+
new ZHHansDateParser(),
|
|
56
|
+
new ZHHantRelationWeekdayParser(),
|
|
57
|
+
new ZHHansRelationWeekdayParser(),
|
|
58
|
+
new ZHHantWeekdayParser(),
|
|
59
|
+
new ZHHansWeekdayParser(),
|
|
60
|
+
new ZHHantTimeExpressionParser(),
|
|
61
|
+
new ZHHansTimeExpressionParser(),
|
|
62
|
+
new ZHHantDeadlineFormatParser(),
|
|
63
|
+
new ZHHansDeadlineFormatParser(),
|
|
64
|
+
],
|
|
65
|
+
refiners: [new ZHHantMergeDateRangeRefiner(), new ZHHantMergeDateTimeRefiner()],
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// REMOVE ExtractTimezoneOffsetRefiner
|
|
69
|
+
configuration.refiners = configuration.refiners.filter(
|
|
70
|
+
(refiner) => !(refiner instanceof ExtractTimezoneOffsetRefiner)
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
return configuration;
|
|
74
|
+
}
|
package/src/results.ts
CHANGED
|
@@ -132,7 +132,9 @@ export class ParsingComponents implements ParsedComponents {
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
isOnlyTime(): boolean {
|
|
135
|
-
return
|
|
135
|
+
return (
|
|
136
|
+
!this.isCertain("weekday") && !this.isCertain("day") && !this.isCertain("month") && !this.isCertain("year")
|
|
137
|
+
);
|
|
136
138
|
}
|
|
137
139
|
|
|
138
140
|
isOnlyWeekdayComponent(): boolean {
|
|
@@ -230,11 +232,12 @@ export class ParsingComponents implements ParsedComponents {
|
|
|
230
232
|
components.assign("day", date.date());
|
|
231
233
|
components.assign("month", date.month() + 1);
|
|
232
234
|
components.assign("year", date.year());
|
|
235
|
+
} else if (fragments["week"]) {
|
|
236
|
+
components.assign("day", date.date());
|
|
237
|
+
components.assign("month", date.month() + 1);
|
|
238
|
+
components.assign("year", date.year());
|
|
239
|
+
components.imply("weekday", date.day());
|
|
233
240
|
} else {
|
|
234
|
-
if (fragments["week"]) {
|
|
235
|
-
components.imply("weekday", date.day());
|
|
236
|
-
}
|
|
237
|
-
|
|
238
241
|
components.imply("day", date.date());
|
|
239
242
|
if (fragments["month"]) {
|
|
240
243
|
components.assign("month", date.month() + 1);
|
|
@@ -409,4 +409,10 @@ test("Test - Random negative text", () => {
|
|
|
409
409
|
testUnexpectedResult(chrono, "mar");
|
|
410
410
|
|
|
411
411
|
testUnexpectedResult(chrono, "jan");
|
|
412
|
+
|
|
413
|
+
testUnexpectedResult(chrono, "do I have the money");
|
|
414
|
+
|
|
415
|
+
testUnexpectedResult(chrono.casual, "do I have the money");
|
|
416
|
+
|
|
417
|
+
testUnexpectedResult(chrono.en.GB, "do I have the money");
|
|
412
418
|
});
|
|
@@ -27,34 +27,6 @@ test("Test - Single Expression", function () {
|
|
|
27
27
|
expect(result.start).toBeDate(new Date(784043130000));
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
testSingleCase(chrono, "1994-11-05T13:15:30", new Date(2012, 7, 8), (result) => {
|
|
31
|
-
expect(result.start).not.toBeNull();
|
|
32
|
-
expect(result.start.get("year")).toBe(1994);
|
|
33
|
-
expect(result.start.get("month")).toBe(11);
|
|
34
|
-
expect(result.start.get("day")).toBe(5);
|
|
35
|
-
expect(result.start.get("hour")).toBe(13);
|
|
36
|
-
expect(result.start.get("minute")).toBe(15);
|
|
37
|
-
expect(result.start.get("second")).toBe(30);
|
|
38
|
-
expect(result.start.get("timezoneOffset")).toBe(0);
|
|
39
|
-
expect(result.text).toBe("1994-11-05T13:15:30");
|
|
40
|
-
|
|
41
|
-
expect(result.start).toBeDate(new Date(784041330000));
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
testSingleCase(chrono, "2015-07-31T12:00:00", new Date(2012, 7, 8), (result) => {
|
|
45
|
-
expect(result.start).not.toBeNull();
|
|
46
|
-
expect(result.start.get("year")).toBe(2015);
|
|
47
|
-
expect(result.start.get("month")).toBe(7);
|
|
48
|
-
expect(result.start.get("day")).toBe(31);
|
|
49
|
-
expect(result.start.get("hour")).toBe(12);
|
|
50
|
-
expect(result.start.get("minute")).toBe(0);
|
|
51
|
-
expect(result.start.get("second")).toBe(0);
|
|
52
|
-
expect(result.start.get("timezoneOffset")).toBe(0);
|
|
53
|
-
expect(result.text).toBe("2015-07-31T12:00:00");
|
|
54
|
-
|
|
55
|
-
expect(result.start).toBeDate(new Date(1438344000000));
|
|
56
|
-
});
|
|
57
|
-
|
|
58
30
|
testSingleCase(chrono, "1994-11-05T13:15:30Z", new Date(2012, 7, 8), (result) => {
|
|
59
31
|
expect(result.start).not.toBeNull();
|
|
60
32
|
expect(result.start.get("year")).toBe(1994);
|
|
@@ -114,3 +86,33 @@ test("Test - Single Expression", function () {
|
|
|
114
86
|
expect(result.start).toBeDate(new Date(1462661100487));
|
|
115
87
|
});
|
|
116
88
|
});
|
|
89
|
+
|
|
90
|
+
test("Test - Single Expression with local timezeon", function () {
|
|
91
|
+
testSingleCase(chrono, "1994-11-05T13:15:30", new Date(2012, 7, 8), (result) => {
|
|
92
|
+
expect(result.text).toBe("1994-11-05T13:15:30");
|
|
93
|
+
expect(result.start.get("year")).toBe(1994);
|
|
94
|
+
expect(result.start.get("month")).toBe(11);
|
|
95
|
+
expect(result.start.get("day")).toBe(5);
|
|
96
|
+
expect(result.start.get("hour")).toBe(13);
|
|
97
|
+
expect(result.start.get("minute")).toBe(15);
|
|
98
|
+
expect(result.start.get("second")).toBe(30);
|
|
99
|
+
expect(result.start.get("millisecond")).toBe(0);
|
|
100
|
+
|
|
101
|
+
expect(result.start.isCertain("timezoneOffset")).toBe(false);
|
|
102
|
+
expect(result.start).toBeDate(new Date("1994-11-05T13:15:30"));
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
testSingleCase(chrono, "2015-07-31T12:00:00", new Date(2012, 7, 8), (result) => {
|
|
106
|
+
expect(result.text).toBe("2015-07-31T12:00:00");
|
|
107
|
+
expect(result.start.get("year")).toBe(2015);
|
|
108
|
+
expect(result.start.get("month")).toBe(7);
|
|
109
|
+
expect(result.start.get("day")).toBe(31);
|
|
110
|
+
expect(result.start.get("hour")).toBe(12);
|
|
111
|
+
expect(result.start.get("minute")).toBe(0);
|
|
112
|
+
expect(result.start.get("second")).toBe(0);
|
|
113
|
+
expect(result.start.get("millisecond")).toBe(0);
|
|
114
|
+
|
|
115
|
+
expect(result.start.isCertain("timezoneOffset")).toBe(false);
|
|
116
|
+
expect(result.start).toBeDate(new Date("2015-07-31T12:00:00"));
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -11,10 +11,9 @@ test("Test - Single Expression", function () {
|
|
|
11
11
|
expect(result.start.get("day")).toBe(15);
|
|
12
12
|
expect(result.start.get("weekday")).toBe(2);
|
|
13
13
|
|
|
14
|
-
expect(result.start.isCertain("day")).toBe(
|
|
15
|
-
expect(result.start.isCertain("month")).toBe(
|
|
16
|
-
expect(result.start.isCertain("year")).toBe(
|
|
17
|
-
expect(result.start.isCertain("weekday")).toBe(false);
|
|
14
|
+
expect(result.start.isCertain("day")).toBe(true);
|
|
15
|
+
expect(result.start.isCertain("month")).toBe(true);
|
|
16
|
+
expect(result.start.isCertain("year")).toBe(true);
|
|
18
17
|
|
|
19
18
|
expect(result.start).toBeDate(new Date(2022, 2 - 1, 15, 0));
|
|
20
19
|
});
|