chrono-node 2.3.2 → 2.3.6
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/common/casualReferences.js +3 -1
- package/dist/common/parsers/AbstractParserWithWordBoundary.js +2 -1
- package/dist/common/refiners/ExtractTimezoneAbbrRefiner.js +5 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/dist/locales/de/constants.js +1 -1
- package/dist/locales/en/constants.js +1 -1
- package/dist/locales/en/parsers/ENRelativeDateFormatParser.js +3 -3
- package/dist/locales/en/parsers/ENTimeUnitAgoFormatParser.js +1 -1
- package/dist/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js +1 -1
- package/dist/locales/en/parsers/ENTimeUnitLaterFormatParser.js +1 -1
- package/dist/locales/en/parsers/ENTimeUnitWithinFormatParser.js +1 -1
- package/dist/locales/fr/constants.js +1 -1
- package/dist/locales/fr/parsers/FRTimeUnitAgoFormatParser.js +1 -1
- package/dist/locales/fr/parsers/FRTimeUnitRelativeFormatParser.js +1 -1
- package/dist/locales/fr/parsers/FRTimeUnitWithinFormatParser.js +1 -1
- package/dist/locales/nl/parsers/NLRelativeDateFormatParser.js +3 -3
- package/dist/locales/nl/parsers/NLTimeUnitAgoFormatParser.js +1 -1
- package/dist/locales/nl/parsers/NLTimeUnitCasualRelativeFormatParser.js +1 -1
- package/dist/locales/nl/parsers/NLTimeUnitLaterFormatParser.js +1 -1
- package/dist/locales/nl/parsers/NLTimeUnitWithinFormatParser.js +1 -1
- package/dist/locales/zh/hans/constants.d.ts +27 -0
- package/dist/locales/zh/hans/constants.js +51 -0
- package/dist/locales/zh/hans/index.d.ts +9 -0
- package/dist/locales/zh/hans/index.js +49 -0
- package/dist/locales/zh/hans/parsers/ZHHansCasualDateParser.d.ts +7 -0
- package/dist/locales/zh/hans/parsers/ZHHansCasualDateParser.js +138 -0
- package/dist/locales/zh/hans/parsers/ZHHansDateParser.d.ts +6 -0
- package/dist/locales/zh/hans/parsers/ZHHansDateParser.js +72 -0
- package/dist/locales/zh/hans/parsers/ZHHansDeadlineFormatParser.d.ts +6 -0
- package/dist/locales/zh/hans/parsers/ZHHansDeadlineFormatParser.js +78 -0
- package/dist/locales/zh/hans/parsers/ZHHansRelationWeekdayParser.d.ts +7 -0
- package/dist/locales/zh/hans/parsers/ZHHansRelationWeekdayParser.js +70 -0
- package/dist/locales/zh/hans/parsers/ZHHansTimeExpressionParser.d.ts +6 -0
- package/dist/locales/zh/hans/parsers/ZHHansTimeExpressionParser.js +438 -0
- package/dist/locales/zh/hans/parsers/ZHHansWeekdayParser.d.ts +7 -0
- package/dist/locales/zh/hans/parsers/ZHHansWeekdayParser.js +46 -0
- package/dist/locales/zh/hans/refiners/ZHHansMergeDateRangeRefiner.d.ts +4 -0
- package/dist/locales/zh/hans/refiners/ZHHansMergeDateRangeRefiner.js +12 -0
- package/dist/locales/zh/hans/refiners/ZHHansMergeDateTimeRefiner.d.ts +4 -0
- package/dist/locales/zh/hans/refiners/ZHHansMergeDateTimeRefiner.js +12 -0
- package/dist/locales/zh/hant/constants.d.ts +28 -0
- package/dist/locales/zh/hant/constants.js +52 -0
- package/dist/locales/zh/hant/index.d.ts +9 -0
- package/dist/locales/zh/hant/index.js +49 -0
- package/dist/locales/zh/hant/parsers/ZHHantCasualDateParser.d.ts +7 -0
- package/dist/locales/zh/hant/parsers/ZHHantCasualDateParser.js +138 -0
- package/dist/locales/zh/hant/parsers/ZHHantDateParser.d.ts +6 -0
- package/dist/locales/zh/hant/parsers/ZHHantDateParser.js +64 -0
- package/dist/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.d.ts +6 -0
- package/dist/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.js +78 -0
- package/dist/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.d.ts +7 -0
- package/dist/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.js +70 -0
- package/dist/locales/zh/hant/parsers/ZHHantTimeExpressionParser.d.ts +6 -0
- package/dist/locales/zh/hant/parsers/ZHHantTimeExpressionParser.js +438 -0
- package/dist/locales/zh/hant/parsers/ZHHantWeekdayParser.d.ts +7 -0
- package/dist/locales/zh/hant/parsers/ZHHantWeekdayParser.js +46 -0
- package/dist/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.d.ts +4 -0
- package/dist/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.js +12 -0
- package/dist/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.d.ts +4 -0
- package/dist/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.js +12 -0
- package/dist/locales/zh/index.d.ts +2 -0
- package/dist/locales/zh/index.js +16 -0
- package/dist/results.d.ts +2 -2
- package/dist/results.js +18 -12
- package/dist/timezone.d.ts +1 -1
- package/dist/timezone.js +3 -0
- package/dist/utils/dayjs.js +7 -0
- package/package.json +1 -1
- package/src/common/casualReferences.ts +3 -1
- package/src/common/parsers/AbstractParserWithWordBoundary.ts +1 -1
- package/src/common/refiners/ExtractTimezoneAbbrRefiner.ts +8 -0
- package/src/index.ts +2 -1
- package/src/locales/de/constants.ts +1 -1
- package/src/locales/en/constants.ts +1 -1
- package/src/locales/en/parsers/ENMonthNameLittleEndianParser.ts +0 -2
- package/src/locales/en/parsers/ENRelativeDateFormatParser.ts +3 -3
- package/src/locales/en/parsers/ENTimeUnitAgoFormatParser.ts +1 -1
- package/src/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.ts +1 -1
- package/src/locales/en/parsers/ENTimeUnitLaterFormatParser.ts +1 -1
- package/src/locales/en/parsers/ENTimeUnitWithinFormatParser.ts +1 -1
- package/src/locales/fr/constants.ts +1 -1
- package/src/locales/fr/parsers/FRTimeUnitAgoFormatParser.ts +1 -1
- package/src/locales/fr/parsers/FRTimeUnitRelativeFormatParser.ts +1 -1
- package/src/locales/fr/parsers/FRTimeUnitWithinFormatParser.ts +1 -1
- package/src/locales/nl/parsers/NLRelativeDateFormatParser.ts +3 -3
- package/src/locales/nl/parsers/NLTimeUnitAgoFormatParser.ts +1 -1
- package/src/locales/nl/parsers/NLTimeUnitCasualRelativeFormatParser.ts +1 -1
- package/src/locales/nl/parsers/NLTimeUnitLaterFormatParser.ts +1 -1
- package/src/locales/nl/parsers/NLTimeUnitWithinFormatParser.ts +1 -1
- package/src/locales/zh/hans/constants.ts +52 -0
- package/src/locales/zh/hans/index.ts +62 -0
- package/src/locales/zh/hans/parsers/ZHHansCasualDateParser.ts +128 -0
- package/src/locales/zh/hans/parsers/ZHHansDateParser.ts +75 -0
- package/src/locales/zh/hans/parsers/ZHHansDeadlineFormatParser.ts +81 -0
- package/src/locales/zh/hans/parsers/ZHHansRelationWeekdayParser.ts +69 -0
- package/src/locales/zh/hans/parsers/ZHHansTimeExpressionParser.ts +424 -0
- package/src/locales/zh/hans/parsers/ZHHansWeekdayParser.ts +46 -0
- package/src/locales/zh/hans/refiners/ZHHansMergeDateRangeRefiner.ts +7 -0
- package/src/locales/zh/hans/refiners/ZHHansMergeDateTimeRefiner.ts +7 -0
- package/src/locales/zh/hant/constants.ts +53 -0
- package/src/locales/zh/hant/index.ts +63 -0
- package/src/locales/zh/hant/parsers/ZHHantCasualDateParser.ts +128 -0
- package/src/locales/zh/hant/parsers/ZHHantDateParser.ts +68 -0
- package/src/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.ts +81 -0
- package/src/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.ts +69 -0
- package/src/locales/zh/hant/parsers/ZHHantTimeExpressionParser.ts +424 -0
- package/src/locales/zh/hant/parsers/ZHHantWeekdayParser.ts +46 -0
- package/src/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.ts +7 -0
- package/src/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.ts +7 -0
- package/src/locales/zh/index.ts +2 -0
- package/src/results.ts +22 -12
- package/src/timezone.ts +5 -1
- package/src/utils/dayjs.ts +6 -0
- package/test/en/en_casual.test.ts +11 -0
- package/test/en/en_month.test.ts +13 -0
- package/test/en/en_relative.test.ts +8 -0
- package/test/en/en_time_units_ago.test.ts +16 -0
- package/test/en/en_time_units_later.test.ts +6 -0
- package/test/en/en_timezone_exp.test.ts +85 -10
- package/test/en/negative_cases.test.ts +2 -0
- package/test/test_util.ts +5 -2
- package/test/zh/zh.test.ts +18 -0
- package/test/zh/zh_hans_casual.test.ts +228 -0
- package/test/zh/zh_hans_date.test.ts +102 -0
- package/test/zh/zh_hans_deadline.test.ts +136 -0
- package/test/zh/zh_hans_time_exp.test.ts +182 -0
- package/test/zh/zh_hans_weekday.test.ts +132 -0
- package/test/zh/zh_hant_casual.test.ts +246 -0
- package/test/zh/zh_hant_date.test.ts +102 -0
- package/test/zh/zh_hant_deadline.test.ts +136 -0
- package/test/zh/zh_hant_time_exp.test.ts +182 -0
- package/test/zh/zh_hant_weekday.test.ts +132 -0
package/dist/utils/dayjs.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.implySimilarTime = exports.assignSimilarTime = exports.assignSimilarDate = exports.assignTheNextDay = void 0;
|
|
4
|
+
const index_1 = require("../index");
|
|
4
5
|
function assignTheNextDay(component, targetDayJs) {
|
|
5
6
|
targetDayJs = targetDayJs.add(1, "day");
|
|
6
7
|
assignSimilarDate(component, targetDayJs);
|
|
@@ -18,6 +19,12 @@ function assignSimilarTime(component, targetDayJs) {
|
|
|
18
19
|
component.assign("minute", targetDayJs.minute());
|
|
19
20
|
component.assign("second", targetDayJs.second());
|
|
20
21
|
component.assign("millisecond", targetDayJs.millisecond());
|
|
22
|
+
if (component.get("hour") < 12) {
|
|
23
|
+
component.assign("meridiem", index_1.Meridiem.AM);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
component.assign("meridiem", index_1.Meridiem.PM);
|
|
27
|
+
}
|
|
21
28
|
}
|
|
22
29
|
exports.assignSimilarTime = assignSimilarTime;
|
|
23
30
|
function implySimilarTime(component, targetDayJs) {
|
package/package.json
CHANGED
|
@@ -8,7 +8,9 @@ export function now(reference: ReferenceWithTimezone): ParsingComponents {
|
|
|
8
8
|
const component = new ParsingComponents(reference, {});
|
|
9
9
|
assignSimilarDate(component, targetDate);
|
|
10
10
|
assignSimilarTime(component, targetDate);
|
|
11
|
-
|
|
11
|
+
if (reference.timezoneOffset !== null) {
|
|
12
|
+
component.assign("timezoneOffset", targetDate.utcOffset());
|
|
13
|
+
}
|
|
12
14
|
return component;
|
|
13
15
|
}
|
|
14
16
|
|
|
@@ -27,7 +27,7 @@ export abstract class AbstractParserWithWordBoundaryChecking implements Parser {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
extract(context: ParsingContext, match: RegExpMatchArray) {
|
|
30
|
-
const header = match[1];
|
|
30
|
+
const header = match[1] ?? "";
|
|
31
31
|
match.index = match.index + header.length;
|
|
32
32
|
match[0] = match[0].substring(header.length);
|
|
33
33
|
for (let i = 2; i < match.length; i++) {
|
|
@@ -240,6 +240,14 @@ export default class ExtractTimezoneAbbrRefiner implements Refiner {
|
|
|
240
240
|
}
|
|
241
241
|
}
|
|
242
242
|
|
|
243
|
+
if (result.start.isOnlyDate()) {
|
|
244
|
+
// If the time is not explicitly mentioned,
|
|
245
|
+
// Then, we also want to double check the abbr case (e.g. "GET" not "get")
|
|
246
|
+
if (timezoneAbbr != match[1]) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
243
251
|
result.text += match[0];
|
|
244
252
|
|
|
245
253
|
if (!result.start.isCertain("timezoneOffset")) {
|
package/src/index.ts
CHANGED
|
@@ -105,7 +105,8 @@ import * as fr from "./locales/fr";
|
|
|
105
105
|
import * as ja from "./locales/ja";
|
|
106
106
|
import * as pt from "./locales/pt";
|
|
107
107
|
import * as nl from "./locales/nl";
|
|
108
|
-
|
|
108
|
+
import * as zh from "./locales/zh";
|
|
109
|
+
export { de, fr, ja, pt, nl, zh };
|
|
109
110
|
|
|
110
111
|
/**
|
|
111
112
|
* A shortcut for {@link en | chrono.en.strict}
|
|
@@ -108,7 +108,7 @@ export const TIME_UNIT_DICTIONARY: { [word: string]: OpUnitType } = {
|
|
|
108
108
|
|
|
109
109
|
export const NUMBER_PATTERN = `(?:${matchAnyPattern(
|
|
110
110
|
INTEGER_WORD_DICTIONARY
|
|
111
|
-
)}|[0-9]+|[0-9]+\\.[0-9]+|half(?:\\s*an?)?|an
|
|
111
|
+
)}|[0-9]+|[0-9]+\\.[0-9]+|half(?:\\s*an?)?|an?\\b(?:\\s*few)?|few|several|a?\\s*couple\\s*(?:of)?)`;
|
|
112
112
|
|
|
113
113
|
export function parseNumberPattern(match: string): number {
|
|
114
114
|
const num = match.toLowerCase();
|
|
@@ -162,7 +162,7 @@ export const TIME_UNIT_DICTIONARY: { [word: string]: OpUnitType } = {
|
|
|
162
162
|
|
|
163
163
|
export const NUMBER_PATTERN = `(?:${matchAnyPattern(
|
|
164
164
|
INTEGER_WORD_DICTIONARY
|
|
165
|
-
)}|[0-9]+|[0-9]+\\.[0-9]+|half(?:\\s{0,2}an?)?|an
|
|
165
|
+
)}|[0-9]+|[0-9]+\\.[0-9]+|half(?:\\s{0,2}an?)?|an?\\b(?:\\s{0,2}few)?|few|several|a?\\s{0,2}couple\\s{0,2}(?:of)?)`;
|
|
166
166
|
|
|
167
167
|
export function parseNumberPattern(match: string): number {
|
|
168
168
|
const num = match.toLowerCase();
|
|
@@ -36,8 +36,6 @@ export default class ENMonthNameLittleEndianParser extends AbstractParserWithWor
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingResult {
|
|
39
|
-
//console.log(match)
|
|
40
|
-
|
|
41
39
|
const result = context.createParsingResult(match.index, match[0]);
|
|
42
40
|
|
|
43
41
|
const month = MONTH_DICTIONARY[match[MONTH_NAME_GROUP].toLowerCase()];
|
|
@@ -26,17 +26,17 @@ export default class ENRelativeDateFormatParser extends AbstractParserWithWordBo
|
|
|
26
26
|
if (modifier == "next") {
|
|
27
27
|
const timeUnits = {};
|
|
28
28
|
timeUnits[timeunit] = 1;
|
|
29
|
-
return ParsingComponents.
|
|
29
|
+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
if (modifier == "last" || modifier == "past") {
|
|
33
33
|
const timeUnits = {};
|
|
34
34
|
timeUnits[timeunit] = -1;
|
|
35
|
-
return ParsingComponents.
|
|
35
|
+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
const components = context.createParsingComponents();
|
|
39
|
-
let date = dayjs(context.
|
|
39
|
+
let date = dayjs(context.reference.instant);
|
|
40
40
|
|
|
41
41
|
// This week
|
|
42
42
|
if (unitWord.match(/week/i)) {
|
|
@@ -20,6 +20,6 @@ export default class ENTimeUnitAgoFormatParser extends AbstractParserWithWordBou
|
|
|
20
20
|
const timeUnits = parseTimeUnits(match[1]);
|
|
21
21
|
const outputTimeUnits = reverseTimeUnits(timeUnits);
|
|
22
22
|
|
|
23
|
-
return ParsingComponents.
|
|
23
|
+
return ParsingComponents.createRelativeFromReference(context.reference, outputTimeUnits);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
@@ -22,6 +22,6 @@ export default class ENTimeUnitCasualRelativeFormatParser extends AbstractParser
|
|
|
22
22
|
break;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
return ParsingComponents.
|
|
25
|
+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -22,6 +22,6 @@ export default class ENTimeUnitLaterFormatParser extends AbstractParserWithWordB
|
|
|
22
22
|
|
|
23
23
|
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
|
|
24
24
|
const fragments = parseTimeUnits(match[GROUP_NUM_TIMEUNITS]);
|
|
25
|
-
return ParsingComponents.
|
|
25
|
+
return ParsingComponents.createRelativeFromReference(context.reference, fragments);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -21,6 +21,6 @@ export default class ENTimeUnitWithinFormatParser extends AbstractParserWithWord
|
|
|
21
21
|
|
|
22
22
|
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents {
|
|
23
23
|
const timeUnits = parseTimeUnits(match[1]);
|
|
24
|
-
return ParsingComponents.
|
|
24
|
+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -105,7 +105,7 @@ export const TIME_UNIT_DICTIONARY: { [word: string]: OpUnitType | QUnitType } =
|
|
|
105
105
|
|
|
106
106
|
export const NUMBER_PATTERN = `(?:${matchAnyPattern(
|
|
107
107
|
INTEGER_WORD_DICTIONARY
|
|
108
|
-
)}|[0-9]+|[0-9]+\\.[0-9]+|une
|
|
108
|
+
)}|[0-9]+|[0-9]+\\.[0-9]+|une?\\b|quelques?|demi-?)`;
|
|
109
109
|
|
|
110
110
|
export function parseNumberPattern(match: string): number {
|
|
111
111
|
const num = match.toLowerCase();
|
|
@@ -17,6 +17,6 @@ export default class FRTimeUnitAgoFormatParser extends AbstractParserWithWordBou
|
|
|
17
17
|
const timeUnits = parseTimeUnits(match[1]);
|
|
18
18
|
const outputTimeUnits = reverseTimeUnits(timeUnits);
|
|
19
19
|
|
|
20
|
-
return ParsingComponents.
|
|
20
|
+
return ParsingComponents.createRelativeFromReference(context.reference, outputTimeUnits);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -38,6 +38,6 @@ export default class FRTimeUnitAgoFormatParser extends AbstractParserWithWordBou
|
|
|
38
38
|
timeUnits = reverseTimeUnits(timeUnits);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
return ParsingComponents.
|
|
41
|
+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
42
42
|
}
|
|
43
43
|
}
|
|
@@ -10,6 +10,6 @@ export default class FRTimeUnitWithinFormatParser extends AbstractParserWithWord
|
|
|
10
10
|
|
|
11
11
|
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents {
|
|
12
12
|
const timeUnits = parseTimeUnits(match[1]);
|
|
13
|
-
return ParsingComponents.
|
|
13
|
+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
14
14
|
}
|
|
15
15
|
}
|
|
@@ -27,17 +27,17 @@ export default class NLRelativeDateFormatParser extends AbstractParserWithWordBo
|
|
|
27
27
|
if (modifier == "volgend" || modifier == "volgende" || modifier == "komende") {
|
|
28
28
|
const timeUnits = {};
|
|
29
29
|
timeUnits[timeunit] = 1;
|
|
30
|
-
return ParsingComponents.
|
|
30
|
+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
if (modifier == "afgelopen" || modifier == "vorige") {
|
|
34
34
|
const timeUnits = {};
|
|
35
35
|
timeUnits[timeunit] = -1;
|
|
36
|
-
return ParsingComponents.
|
|
36
|
+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
const components = context.createParsingComponents();
|
|
40
|
-
let date = dayjs(context.
|
|
40
|
+
let date = dayjs(context.reference.instant);
|
|
41
41
|
|
|
42
42
|
// This week
|
|
43
43
|
if (unitWord.match(/week/i)) {
|
|
@@ -21,6 +21,6 @@ export default class NLTimeUnitAgoFormatParser extends AbstractParserWithWordBou
|
|
|
21
21
|
const timeUnits = parseTimeUnits(match[1]);
|
|
22
22
|
const outputTimeUnits = reverseTimeUnits(timeUnits);
|
|
23
23
|
|
|
24
|
-
return ParsingComponents.
|
|
24
|
+
return ParsingComponents.createRelativeFromReference(context.reference, outputTimeUnits);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -22,6 +22,6 @@ export default class NLTimeUnitCasualRelativeFormatParser extends AbstractParser
|
|
|
22
22
|
break;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
return ParsingComponents.
|
|
25
|
+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -22,6 +22,6 @@ export default class NLTimeUnitLaterFormatParser extends AbstractParserWithWordB
|
|
|
22
22
|
|
|
23
23
|
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
|
|
24
24
|
const fragments = parseTimeUnits(match[GROUP_NUM_TIMEUNITS]);
|
|
25
|
-
return ParsingComponents.
|
|
25
|
+
return ParsingComponents.createRelativeFromReference(context.reference, fragments);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -10,6 +10,6 @@ export default class NLTimeUnitWithinFormatParser extends AbstractParserWithWord
|
|
|
10
10
|
|
|
11
11
|
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents {
|
|
12
12
|
const timeUnits = parseTimeUnits(match[1]);
|
|
13
|
-
return ParsingComponents.
|
|
13
|
+
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
|
|
14
14
|
}
|
|
15
15
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export const NUMBER = {
|
|
2
|
+
"零": 0,
|
|
3
|
+
"〇": 0,
|
|
4
|
+
"一": 1,
|
|
5
|
+
"二": 2,
|
|
6
|
+
"两": 2,
|
|
7
|
+
"三": 3,
|
|
8
|
+
"四": 4,
|
|
9
|
+
"五": 5,
|
|
10
|
+
"六": 6,
|
|
11
|
+
"七": 7,
|
|
12
|
+
"八": 8,
|
|
13
|
+
"九": 9,
|
|
14
|
+
"十": 10,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const WEEKDAY_OFFSET = {
|
|
18
|
+
"天": 0,
|
|
19
|
+
"日": 0,
|
|
20
|
+
"一": 1,
|
|
21
|
+
"二": 2,
|
|
22
|
+
"三": 3,
|
|
23
|
+
"四": 4,
|
|
24
|
+
"五": 5,
|
|
25
|
+
"六": 6,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export function zhStringToNumber(text: string) {
|
|
29
|
+
let number = 0;
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i < text.length; i++) {
|
|
32
|
+
const char = text[i];
|
|
33
|
+
if (char === "十") {
|
|
34
|
+
number = number === 0 ? NUMBER[char] : number * NUMBER[char];
|
|
35
|
+
} else {
|
|
36
|
+
number += NUMBER[char];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function zhStringToYear(text: string) {
|
|
44
|
+
let string = "";
|
|
45
|
+
|
|
46
|
+
for (let i = 0; i < text.length; i++) {
|
|
47
|
+
const char = text[i];
|
|
48
|
+
string = string + NUMBER[char];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return parseInt(string);
|
|
52
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chrono components for zh support
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Chrono, Configuration } from "../../../chrono";
|
|
6
|
+
import ExtractTimezoneOffsetRefiner from "../../../common/refiners/ExtractTimezoneOffsetRefiner";
|
|
7
|
+
import { includeCommonConfiguration } from "../../../configurations";
|
|
8
|
+
import { ParsedResult, ParsingOption } from "../../../index";
|
|
9
|
+
import ZHHansCasualDateParser from "./parsers/ZHHansCasualDateParser";
|
|
10
|
+
import ZHHansDateParser from "./parsers/ZHHansDateParser";
|
|
11
|
+
import ZHHansDeadlineFormatParser from "./parsers/ZHHansDeadlineFormatParser";
|
|
12
|
+
import ZHHansRelationWeekdayParser from "./parsers/ZHHansRelationWeekdayParser";
|
|
13
|
+
import ZHHansTimeExpressionParser from "./parsers/ZHHansTimeExpressionParser";
|
|
14
|
+
import ZHHansWeekdayParser from "./parsers/ZHHansWeekdayParser";
|
|
15
|
+
import ZHHansMergeDateRangeRefiner from "./refiners/ZHHansMergeDateRangeRefiner";
|
|
16
|
+
import ZHHansMergeDateTimeRefiner from "./refiners/ZHHansMergeDateTimeRefiner";
|
|
17
|
+
|
|
18
|
+
// Shortcuts
|
|
19
|
+
export const hans = new Chrono(createCasualConfiguration());
|
|
20
|
+
|
|
21
|
+
export const casual = new Chrono(createCasualConfiguration());
|
|
22
|
+
export const strict = new Chrono(createConfiguration());
|
|
23
|
+
|
|
24
|
+
export function parse(text: string, ref?: Date, option?: ParsingOption): ParsedResult[] {
|
|
25
|
+
return casual.parse(text, ref, option);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function parseDate(text: string, ref?: Date, option?: ParsingOption): Date {
|
|
29
|
+
return casual.parseDate(text, ref, option);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @ignore (to be documented later)
|
|
34
|
+
*/
|
|
35
|
+
export function createCasualConfiguration(): Configuration {
|
|
36
|
+
const option = createConfiguration();
|
|
37
|
+
option.parsers.unshift(new ZHHansCasualDateParser());
|
|
38
|
+
return option;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @ignore (to be documented later)
|
|
43
|
+
*/
|
|
44
|
+
export function createConfiguration(): Configuration {
|
|
45
|
+
const configuration = includeCommonConfiguration({
|
|
46
|
+
parsers: [
|
|
47
|
+
new ZHHansDateParser(),
|
|
48
|
+
new ZHHansRelationWeekdayParser(),
|
|
49
|
+
new ZHHansWeekdayParser(),
|
|
50
|
+
new ZHHansTimeExpressionParser(),
|
|
51
|
+
new ZHHansDeadlineFormatParser(),
|
|
52
|
+
],
|
|
53
|
+
refiners: [new ZHHansMergeDateRangeRefiner(), new ZHHansMergeDateTimeRefiner()],
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// REMOVE ExtractTimezoneOffsetRefiner
|
|
57
|
+
configuration.refiners = configuration.refiners.filter(
|
|
58
|
+
(refiner) => !(refiner instanceof ExtractTimezoneOffsetRefiner)
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return configuration;
|
|
62
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import dayjs from "dayjs";
|
|
2
|
+
import { ParsingContext } from "../../../../chrono";
|
|
3
|
+
import { AbstractParserWithWordBoundaryChecking } from "../../../../common/parsers/AbstractParserWithWordBoundary";
|
|
4
|
+
import { ParsingComponents, ParsingResult } from "../../../../results";
|
|
5
|
+
|
|
6
|
+
const NOW_GROUP = 1;
|
|
7
|
+
const DAY_GROUP_1 = 2;
|
|
8
|
+
const TIME_GROUP_1 = 3;
|
|
9
|
+
const TIME_GROUP_2 = 4;
|
|
10
|
+
const DAY_GROUP_3 = 5;
|
|
11
|
+
const TIME_GROUP_3 = 6;
|
|
12
|
+
|
|
13
|
+
export default class ZHHansCasualDateParser extends AbstractParserWithWordBoundaryChecking {
|
|
14
|
+
innerPattern(context: ParsingContext): RegExp {
|
|
15
|
+
return new RegExp(
|
|
16
|
+
"(现在|立(?:刻|即)|即刻)|" +
|
|
17
|
+
"(今|明|前|大前|后|大后|昨)(早|晚)|" +
|
|
18
|
+
"(上(?:午)|早(?:上)|下(?:午)|晚(?:上)|夜(?:晚)?|中(?:午)|凌(?:晨))|" +
|
|
19
|
+
"(今|明|前|大前|后|大后|昨)(?:日|天)" +
|
|
20
|
+
"(?:[\\s|,|,]*)" +
|
|
21
|
+
"(?:(上(?:午)|早(?:上)|下(?:午)|晚(?:上)|夜(?:晚)?|中(?:午)|凌(?:晨)))?",
|
|
22
|
+
"i"
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents | ParsingResult {
|
|
27
|
+
const index = match.index;
|
|
28
|
+
const result = context.createParsingResult(index, match[0]);
|
|
29
|
+
|
|
30
|
+
const refMoment = dayjs(context.refDate);
|
|
31
|
+
let startMoment = refMoment;
|
|
32
|
+
|
|
33
|
+
if (match[NOW_GROUP]) {
|
|
34
|
+
result.start.imply("hour", refMoment.hour());
|
|
35
|
+
result.start.imply("minute", refMoment.minute());
|
|
36
|
+
result.start.imply("second", refMoment.second());
|
|
37
|
+
result.start.imply("millisecond", refMoment.millisecond());
|
|
38
|
+
} else if (match[DAY_GROUP_1]) {
|
|
39
|
+
const day1 = match[DAY_GROUP_1];
|
|
40
|
+
const time1 = match[TIME_GROUP_1];
|
|
41
|
+
|
|
42
|
+
if (day1 == "明") {
|
|
43
|
+
// Check not "Tomorrow" on late night
|
|
44
|
+
if (refMoment.hour() > 1) {
|
|
45
|
+
startMoment = startMoment.add(1, "day");
|
|
46
|
+
}
|
|
47
|
+
} else if (day1 == "昨") {
|
|
48
|
+
startMoment = startMoment.add(-1, "day");
|
|
49
|
+
} else if (day1 == "前") {
|
|
50
|
+
startMoment = startMoment.add(-2, "day");
|
|
51
|
+
} else if (day1 == "大前") {
|
|
52
|
+
startMoment = startMoment.add(-3, "day");
|
|
53
|
+
} else if (day1 == "后") {
|
|
54
|
+
startMoment = startMoment.add(2, "day");
|
|
55
|
+
} else if (day1 == "大后") {
|
|
56
|
+
startMoment = startMoment.add(3, "day");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (time1 == "早") {
|
|
60
|
+
result.start.imply("hour", 6);
|
|
61
|
+
} else if (time1 == "晚") {
|
|
62
|
+
result.start.imply("hour", 22);
|
|
63
|
+
result.start.imply("meridiem", 1);
|
|
64
|
+
}
|
|
65
|
+
} else if (match[TIME_GROUP_2]) {
|
|
66
|
+
const timeString2 = match[TIME_GROUP_2];
|
|
67
|
+
const time2 = timeString2[0];
|
|
68
|
+
if (time2 == "早" || time2 == "上") {
|
|
69
|
+
result.start.imply("hour", 6);
|
|
70
|
+
} else if (time2 == "下") {
|
|
71
|
+
result.start.imply("hour", 15);
|
|
72
|
+
result.start.imply("meridiem", 1);
|
|
73
|
+
} else if (time2 == "中") {
|
|
74
|
+
result.start.imply("hour", 12);
|
|
75
|
+
result.start.imply("meridiem", 1);
|
|
76
|
+
} else if (time2 == "夜" || time2 == "晚") {
|
|
77
|
+
result.start.imply("hour", 22);
|
|
78
|
+
result.start.imply("meridiem", 1);
|
|
79
|
+
} else if (time2 == "凌") {
|
|
80
|
+
result.start.imply("hour", 0);
|
|
81
|
+
}
|
|
82
|
+
} else if (match[DAY_GROUP_3]) {
|
|
83
|
+
const day3 = match[DAY_GROUP_3];
|
|
84
|
+
|
|
85
|
+
if (day3 == "明") {
|
|
86
|
+
// Check not "Tomorrow" on late night
|
|
87
|
+
if (refMoment.hour() > 1) {
|
|
88
|
+
startMoment = startMoment.add(1, "day");
|
|
89
|
+
}
|
|
90
|
+
} else if (day3 == "昨") {
|
|
91
|
+
startMoment = startMoment.add(-1, "day");
|
|
92
|
+
} else if (day3 == "前") {
|
|
93
|
+
startMoment = startMoment.add(-2, "day");
|
|
94
|
+
} else if (day3 == "大前") {
|
|
95
|
+
startMoment = startMoment.add(-3, "day");
|
|
96
|
+
} else if (day3 == "后") {
|
|
97
|
+
startMoment = startMoment.add(2, "day");
|
|
98
|
+
} else if (day3 == "大后") {
|
|
99
|
+
startMoment = startMoment.add(3, "day");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const timeString3 = match[TIME_GROUP_3];
|
|
103
|
+
if (timeString3) {
|
|
104
|
+
const time3 = timeString3[0];
|
|
105
|
+
if (time3 == "早" || time3 == "上") {
|
|
106
|
+
result.start.imply("hour", 6);
|
|
107
|
+
} else if (time3 == "下") {
|
|
108
|
+
result.start.imply("hour", 15);
|
|
109
|
+
result.start.imply("meridiem", 1);
|
|
110
|
+
} else if (time3 == "中") {
|
|
111
|
+
result.start.imply("hour", 12);
|
|
112
|
+
result.start.imply("meridiem", 1);
|
|
113
|
+
} else if (time3 == "夜" || time3 == "晚") {
|
|
114
|
+
result.start.imply("hour", 22);
|
|
115
|
+
result.start.imply("meridiem", 1);
|
|
116
|
+
} else if (time3 == "凌") {
|
|
117
|
+
result.start.imply("hour", 0);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
result.start.assign("day", startMoment.date());
|
|
123
|
+
result.start.assign("month", startMoment.month() + 1);
|
|
124
|
+
result.start.assign("year", startMoment.year());
|
|
125
|
+
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import dayjs from "dayjs";
|
|
2
|
+
import { ParsingContext } from "../../../../chrono";
|
|
3
|
+
import { AbstractParserWithWordBoundaryChecking } from "../../../../common/parsers/AbstractParserWithWordBoundary";
|
|
4
|
+
import { NUMBER, zhStringToNumber, zhStringToYear } from "../constants";
|
|
5
|
+
|
|
6
|
+
const YEAR_GROUP = 1;
|
|
7
|
+
const MONTH_GROUP = 2;
|
|
8
|
+
const DAY_GROUP = 3;
|
|
9
|
+
|
|
10
|
+
export default class ZHHansDateParser extends AbstractParserWithWordBoundaryChecking {
|
|
11
|
+
innerPattern() {
|
|
12
|
+
// prettier-ignore
|
|
13
|
+
return new RegExp(
|
|
14
|
+
"(" +
|
|
15
|
+
"\\d{2,4}|" +
|
|
16
|
+
"[" +
|
|
17
|
+
Object.keys(NUMBER).join("") +
|
|
18
|
+
"]{4}|" +
|
|
19
|
+
"[" +
|
|
20
|
+
Object.keys(NUMBER).join("") +
|
|
21
|
+
"]{2}" +
|
|
22
|
+
")?" +
|
|
23
|
+
"(?:\\s*)" +
|
|
24
|
+
"(?:年)?" +
|
|
25
|
+
"(?:[\\s|,|,]*)" +
|
|
26
|
+
"(" +
|
|
27
|
+
"\\d{1,2}|" +
|
|
28
|
+
"[" +
|
|
29
|
+
Object.keys(NUMBER).join("") +
|
|
30
|
+
"]{1,3}" +
|
|
31
|
+
")" +
|
|
32
|
+
"(?:\\s*)" +
|
|
33
|
+
"(?:月)" +
|
|
34
|
+
"(?:\\s*)" +
|
|
35
|
+
"(" +
|
|
36
|
+
"\\d{1,2}|" +
|
|
37
|
+
"[" +
|
|
38
|
+
Object.keys(NUMBER).join("") +
|
|
39
|
+
"]{1,3}" +
|
|
40
|
+
")?" +
|
|
41
|
+
"(?:\\s*)" +
|
|
42
|
+
"(?:日|号)?"
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
|
|
47
|
+
const startMoment = dayjs(context.refDate);
|
|
48
|
+
const result = context.createParsingResult(match.index, match[0]);
|
|
49
|
+
|
|
50
|
+
//Month
|
|
51
|
+
let month = parseInt(match[MONTH_GROUP]);
|
|
52
|
+
if (isNaN(month)) month = zhStringToNumber(match[MONTH_GROUP]);
|
|
53
|
+
result.start.assign("month", month);
|
|
54
|
+
|
|
55
|
+
//Day
|
|
56
|
+
if (match[DAY_GROUP]) {
|
|
57
|
+
let day = parseInt(match[DAY_GROUP]);
|
|
58
|
+
if (isNaN(day)) day = zhStringToNumber(match[DAY_GROUP]);
|
|
59
|
+
result.start.assign("day", day);
|
|
60
|
+
} else {
|
|
61
|
+
result.start.imply("day", startMoment.date());
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//Year
|
|
65
|
+
if (match[YEAR_GROUP]) {
|
|
66
|
+
let year = parseInt(match[YEAR_GROUP]);
|
|
67
|
+
if (isNaN(year)) year = zhStringToYear(match[YEAR_GROUP]);
|
|
68
|
+
result.start.assign("year", year);
|
|
69
|
+
} else {
|
|
70
|
+
result.start.imply("year", startMoment.year());
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
}
|