chrono-node 2.3.4 → 2.3.5

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.
Files changed (47) hide show
  1. package/README.md +1 -1
  2. package/dist/common/parsers/AbstractParserWithWordBoundary.js +2 -1
  3. package/dist/index.d.ts +2 -1
  4. package/dist/index.js +3 -1
  5. package/dist/locales/zh/hant/constants.d.ts +28 -0
  6. package/dist/locales/zh/hant/constants.js +52 -0
  7. package/dist/locales/zh/hant/parsers/ZHHantCasualDateParser.d.ts +7 -0
  8. package/dist/locales/zh/hant/parsers/ZHHantCasualDateParser.js +138 -0
  9. package/dist/locales/zh/hant/parsers/ZHHantDateParser.d.ts +6 -0
  10. package/dist/locales/zh/hant/parsers/ZHHantDateParser.js +64 -0
  11. package/dist/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.d.ts +6 -0
  12. package/dist/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.js +78 -0
  13. package/dist/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.d.ts +7 -0
  14. package/dist/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.js +70 -0
  15. package/dist/locales/zh/hant/parsers/ZHHantTimeExpressionParser.d.ts +6 -0
  16. package/dist/locales/zh/hant/parsers/ZHHantTimeExpressionParser.js +438 -0
  17. package/dist/locales/zh/hant/parsers/ZHHantWeekdayParser.d.ts +7 -0
  18. package/dist/locales/zh/hant/parsers/ZHHantWeekdayParser.js +46 -0
  19. package/dist/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.d.ts +4 -0
  20. package/dist/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.js +12 -0
  21. package/dist/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.d.ts +4 -0
  22. package/dist/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.js +12 -0
  23. package/dist/locales/zh/index.d.ts +9 -0
  24. package/dist/locales/zh/index.js +49 -0
  25. package/dist/utils/dayjs.js +7 -0
  26. package/package.json +1 -1
  27. package/src/common/parsers/AbstractParserWithWordBoundary.ts +1 -1
  28. package/src/index.ts +2 -1
  29. package/src/locales/zh/hant/constants.ts +53 -0
  30. package/src/locales/zh/hant/parsers/ZHHantCasualDateParser.ts +128 -0
  31. package/src/locales/zh/hant/parsers/ZHHantDateParser.ts +68 -0
  32. package/src/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.ts +81 -0
  33. package/src/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.ts +69 -0
  34. package/src/locales/zh/hant/parsers/ZHHantTimeExpressionParser.ts +424 -0
  35. package/src/locales/zh/hant/parsers/ZHHantWeekdayParser.ts +46 -0
  36. package/src/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.ts +7 -0
  37. package/src/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.ts +7 -0
  38. package/src/locales/zh/index.ts +63 -0
  39. package/src/utils/dayjs.ts +6 -0
  40. package/test/en/en_time_units_ago.test.ts +16 -0
  41. package/test/en/en_time_units_later.test.ts +6 -0
  42. package/test/zh/zh.test.ts +18 -0
  43. package/test/zh/zh_hant_casual.test.ts +246 -0
  44. package/test/zh/zh_hant_date.test.ts +103 -0
  45. package/test/zh/zh_hant_deadline.test.ts +136 -0
  46. package/test/zh/zh_hant_time_exp.test.ts +182 -0
  47. package/test/zh/zh_hant_weekday.test.ts +132 -0
package/README.md CHANGED
@@ -37,7 +37,7 @@ const chrono = require('chrono-node');
37
37
  ### What's changed in the v2
38
38
  For Users
39
39
  * Chrono’s default now handles only international English. While in the previous version, it tried to parse with all known languages.
40
- * The current fully supported languages are `en`, `ja`, and `fr` (`de` and `pt` are partially supported). Other languages from v1 (`nl` and `zh`) are under development.
40
+ * The current fully supported languages are `en`, `ja`, `fr`, and `nl` (`de`, `pt`, and `zh.hant` are partially supported).
41
41
 
42
42
  For contributors and advanced users
43
43
  * The project is rewritten in TypeScript
@@ -16,7 +16,8 @@ class AbstractParserWithWordBoundaryChecking {
16
16
  return this.cachedPattern;
17
17
  }
18
18
  extract(context, match) {
19
- const header = match[1];
19
+ var _a;
20
+ const header = (_a = match[1]) !== null && _a !== void 0 ? _a : "";
20
21
  match.index = match.index + header.length;
21
22
  match[0] = match[0].substring(header.length);
22
23
  for (let i = 2; i < match.length; i++) {
package/dist/index.d.ts CHANGED
@@ -34,7 +34,8 @@ import * as fr from "./locales/fr";
34
34
  import * as ja from "./locales/ja";
35
35
  import * as pt from "./locales/pt";
36
36
  import * as nl from "./locales/nl";
37
- export { de, fr, ja, pt, nl };
37
+ import * as zh from "./locales/zh";
38
+ export { de, fr, ja, pt, nl, zh };
38
39
  export declare const strict: Chrono;
39
40
  export declare const casual: Chrono;
40
41
  export declare function parse(text: string, ref?: ParsingReference | Date, option?: ParsingOption): ParsedResult[];
package/dist/index.js CHANGED
@@ -19,7 +19,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
19
  return result;
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.parseDate = exports.parse = exports.casual = exports.strict = exports.nl = exports.pt = exports.ja = exports.fr = exports.de = exports.Meridiem = exports.Chrono = exports.en = void 0;
22
+ exports.parseDate = exports.parse = exports.casual = exports.strict = exports.zh = exports.nl = exports.pt = exports.ja = exports.fr = exports.de = exports.Meridiem = exports.Chrono = exports.en = void 0;
23
23
  const en = __importStar(require("./locales/en"));
24
24
  exports.en = en;
25
25
  const chrono_1 = require("./chrono");
@@ -39,6 +39,8 @@ const pt = __importStar(require("./locales/pt"));
39
39
  exports.pt = pt;
40
40
  const nl = __importStar(require("./locales/nl"));
41
41
  exports.nl = nl;
42
+ const zh = __importStar(require("./locales/zh"));
43
+ exports.zh = zh;
42
44
  exports.strict = en.strict;
43
45
  exports.casual = en.casual;
44
46
  function parse(text, ref, option) {
@@ -0,0 +1,28 @@
1
+ export declare const NUMBER: {
2
+ 零: number;
3
+ 一: number;
4
+ 二: number;
5
+ 兩: number;
6
+ 三: number;
7
+ 四: number;
8
+ 五: number;
9
+ 六: number;
10
+ 七: number;
11
+ 八: number;
12
+ 九: number;
13
+ 十: number;
14
+ 廿: number;
15
+ 卅: number;
16
+ };
17
+ export declare const WEEKDAY_OFFSET: {
18
+ 天: number;
19
+ 日: number;
20
+ 一: number;
21
+ 二: number;
22
+ 三: number;
23
+ 四: number;
24
+ 五: number;
25
+ 六: number;
26
+ };
27
+ export declare function zhStringToNumber(text: string): number;
28
+ export declare function zhStringToYear(text: string): number;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.zhStringToYear = exports.zhStringToNumber = exports.WEEKDAY_OFFSET = exports.NUMBER = void 0;
4
+ exports.NUMBER = {
5
+ "零": 0,
6
+ "一": 1,
7
+ "二": 2,
8
+ "兩": 2,
9
+ "三": 3,
10
+ "四": 4,
11
+ "五": 5,
12
+ "六": 6,
13
+ "七": 7,
14
+ "八": 8,
15
+ "九": 9,
16
+ "十": 10,
17
+ "廿": 20,
18
+ "卅": 30,
19
+ };
20
+ exports.WEEKDAY_OFFSET = {
21
+ "天": 0,
22
+ "日": 0,
23
+ "一": 1,
24
+ "二": 2,
25
+ "三": 3,
26
+ "四": 4,
27
+ "五": 5,
28
+ "六": 6,
29
+ };
30
+ function zhStringToNumber(text) {
31
+ let number = 0;
32
+ for (let i = 0; i < text.length; i++) {
33
+ const char = text[i];
34
+ if (char === "十") {
35
+ number = number === 0 ? exports.NUMBER[char] : number * exports.NUMBER[char];
36
+ }
37
+ else {
38
+ number += exports.NUMBER[char];
39
+ }
40
+ }
41
+ return number;
42
+ }
43
+ exports.zhStringToNumber = zhStringToNumber;
44
+ function zhStringToYear(text) {
45
+ let string = "";
46
+ for (let i = 0; i < text.length; i++) {
47
+ const char = text[i];
48
+ string = string + exports.NUMBER[char];
49
+ }
50
+ return parseInt(string);
51
+ }
52
+ exports.zhStringToYear = zhStringToYear;
@@ -0,0 +1,7 @@
1
+ import { ParsingContext } from "../../../../chrono";
2
+ import { AbstractParserWithWordBoundaryChecking } from "../../../../common/parsers/AbstractParserWithWordBoundary";
3
+ import { ParsingComponents, ParsingResult } from "../../../../results";
4
+ export default class ZHHantCasualDateParser extends AbstractParserWithWordBoundaryChecking {
5
+ innerPattern(context: ParsingContext): RegExp;
6
+ innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents | ParsingResult;
7
+ }
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const dayjs_1 = __importDefault(require("dayjs"));
7
+ const AbstractParserWithWordBoundary_1 = require("../../../../common/parsers/AbstractParserWithWordBoundary");
8
+ const NOW_GROUP = 1;
9
+ const DAY_GROUP_1 = 2;
10
+ const TIME_GROUP_1 = 3;
11
+ const TIME_GROUP_2 = 4;
12
+ const DAY_GROUP_3 = 5;
13
+ const TIME_GROUP_3 = 6;
14
+ class ZHHantCasualDateParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking {
15
+ innerPattern(context) {
16
+ return new RegExp("(而家|立(?:刻|即)|即刻)|" +
17
+ "(今|明|前|大前|後|大後|聽|昨|尋|琴)(早|朝|晚)|" +
18
+ "(上(?:午|晝)|朝(?:早)|早(?:上)|下(?:午|晝)|晏(?:晝)|晚(?:上)|夜(?:晚)?|中(?:午)|凌(?:晨))|" +
19
+ "(今|明|前|大前|後|大後|聽|昨|尋|琴)(?:日|天)" +
20
+ "(?:[\\s|,|,]*)" +
21
+ "(?:(上(?:午|晝)|朝(?:早)|早(?:上)|下(?:午|晝)|晏(?:晝)|晚(?:上)|夜(?:晚)?|中(?:午)|凌(?:晨)))?", "i");
22
+ }
23
+ innerExtract(context, match) {
24
+ const index = match.index;
25
+ const result = context.createParsingResult(index, match[0]);
26
+ const refMoment = dayjs_1.default(context.refDate);
27
+ let startMoment = refMoment;
28
+ if (match[NOW_GROUP]) {
29
+ result.start.imply("hour", refMoment.hour());
30
+ result.start.imply("minute", refMoment.minute());
31
+ result.start.imply("second", refMoment.second());
32
+ result.start.imply("millisecond", refMoment.millisecond());
33
+ }
34
+ else if (match[DAY_GROUP_1]) {
35
+ const day1 = match[DAY_GROUP_1];
36
+ const time1 = match[TIME_GROUP_1];
37
+ if (day1 == "明" || day1 == "聽") {
38
+ if (refMoment.hour() > 1) {
39
+ startMoment = startMoment.add(1, "day");
40
+ }
41
+ }
42
+ else if (day1 == "昨" || day1 == "尋" || day1 == "琴") {
43
+ startMoment = startMoment.add(-1, "day");
44
+ }
45
+ else if (day1 == "前") {
46
+ startMoment = startMoment.add(-2, "day");
47
+ }
48
+ else if (day1 == "大前") {
49
+ startMoment = startMoment.add(-3, "day");
50
+ }
51
+ else if (day1 == "後") {
52
+ startMoment = startMoment.add(2, "day");
53
+ }
54
+ else if (day1 == "大後") {
55
+ startMoment = startMoment.add(3, "day");
56
+ }
57
+ if (time1 == "早" || time1 == "朝") {
58
+ result.start.imply("hour", 6);
59
+ }
60
+ else if (time1 == "晚") {
61
+ result.start.imply("hour", 22);
62
+ result.start.imply("meridiem", 1);
63
+ }
64
+ }
65
+ else if (match[TIME_GROUP_2]) {
66
+ const timeString2 = match[TIME_GROUP_2];
67
+ const time2 = timeString2[0];
68
+ if (time2 == "早" || time2 == "朝" || time2 == "上") {
69
+ result.start.imply("hour", 6);
70
+ }
71
+ else if (time2 == "下" || time2 == "晏") {
72
+ result.start.imply("hour", 15);
73
+ result.start.imply("meridiem", 1);
74
+ }
75
+ else if (time2 == "中") {
76
+ result.start.imply("hour", 12);
77
+ result.start.imply("meridiem", 1);
78
+ }
79
+ else if (time2 == "夜" || time2 == "晚") {
80
+ result.start.imply("hour", 22);
81
+ result.start.imply("meridiem", 1);
82
+ }
83
+ else if (time2 == "凌") {
84
+ result.start.imply("hour", 0);
85
+ }
86
+ }
87
+ else if (match[DAY_GROUP_3]) {
88
+ const day3 = match[DAY_GROUP_3];
89
+ if (day3 == "明" || day3 == "聽") {
90
+ if (refMoment.hour() > 1) {
91
+ startMoment = startMoment.add(1, "day");
92
+ }
93
+ }
94
+ else if (day3 == "昨" || day3 == "尋" || day3 == "琴") {
95
+ startMoment = startMoment.add(-1, "day");
96
+ }
97
+ else if (day3 == "前") {
98
+ startMoment = startMoment.add(-2, "day");
99
+ }
100
+ else if (day3 == "大前") {
101
+ startMoment = startMoment.add(-3, "day");
102
+ }
103
+ else if (day3 == "後") {
104
+ startMoment = startMoment.add(2, "day");
105
+ }
106
+ else if (day3 == "大後") {
107
+ startMoment = startMoment.add(3, "day");
108
+ }
109
+ const timeString3 = match[TIME_GROUP_3];
110
+ if (timeString3) {
111
+ const time3 = timeString3[0];
112
+ if (time3 == "早" || time3 == "朝" || time3 == "上") {
113
+ result.start.imply("hour", 6);
114
+ }
115
+ else if (time3 == "下" || time3 == "晏") {
116
+ result.start.imply("hour", 15);
117
+ result.start.imply("meridiem", 1);
118
+ }
119
+ else if (time3 == "中") {
120
+ result.start.imply("hour", 12);
121
+ result.start.imply("meridiem", 1);
122
+ }
123
+ else if (time3 == "夜" || time3 == "晚") {
124
+ result.start.imply("hour", 22);
125
+ result.start.imply("meridiem", 1);
126
+ }
127
+ else if (time3 == "凌") {
128
+ result.start.imply("hour", 0);
129
+ }
130
+ }
131
+ }
132
+ result.start.assign("day", startMoment.date());
133
+ result.start.assign("month", startMoment.month() + 1);
134
+ result.start.assign("year", startMoment.year());
135
+ return result;
136
+ }
137
+ }
138
+ exports.default = ZHHantCasualDateParser;
@@ -0,0 +1,6 @@
1
+ import { ParsingContext } from "../../../../chrono";
2
+ import { AbstractParserWithWordBoundaryChecking } from "../../../../common/parsers/AbstractParserWithWordBoundary";
3
+ export default class ZHHantDateParser extends AbstractParserWithWordBoundaryChecking {
4
+ innerPattern(): RegExp;
5
+ innerExtract(context: ParsingContext, match: RegExpMatchArray): import("../../../../results").ParsingResult;
6
+ }
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const dayjs_1 = __importDefault(require("dayjs"));
7
+ const AbstractParserWithWordBoundary_1 = require("../../../../common/parsers/AbstractParserWithWordBoundary");
8
+ const constants_1 = require("../constants");
9
+ const YEAR_GROUP = 1;
10
+ const MONTH_GROUP = 2;
11
+ const DAY_GROUP = 3;
12
+ class ZHHantDateParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking {
13
+ innerPattern() {
14
+ return new RegExp("(" +
15
+ "\\d{2,4}|" +
16
+ "[" + Object.keys(constants_1.NUMBER).join("") + "]{4}|" +
17
+ "[" + Object.keys(constants_1.NUMBER).join("") + "]{2}" +
18
+ ")?" +
19
+ "(?:\\s*)" +
20
+ "(?:年)?" +
21
+ "(?:[\\s|,|,]*)" +
22
+ "(" +
23
+ "\\d{1,2}|" +
24
+ "[" + Object.keys(constants_1.NUMBER).join("") + "]{1,2}" +
25
+ ")" +
26
+ "(?:\\s*)" +
27
+ "(?:月)" +
28
+ "(?:\\s*)" +
29
+ "(" +
30
+ "\\d{1,2}|" +
31
+ "[" + Object.keys(constants_1.NUMBER).join("") + "]{1,2}" +
32
+ ")?" +
33
+ "(?:\\s*)" +
34
+ "(?:日|號)?");
35
+ }
36
+ innerExtract(context, match) {
37
+ const startMoment = dayjs_1.default(context.refDate);
38
+ const result = context.createParsingResult(match.index, match[0]);
39
+ let month = parseInt(match[MONTH_GROUP]);
40
+ if (isNaN(month))
41
+ month = constants_1.zhStringToNumber(match[MONTH_GROUP]);
42
+ result.start.assign("month", month);
43
+ if (match[DAY_GROUP]) {
44
+ let day = parseInt(match[DAY_GROUP]);
45
+ if (isNaN(day))
46
+ day = constants_1.zhStringToNumber(match[DAY_GROUP]);
47
+ result.start.assign("day", day);
48
+ }
49
+ else {
50
+ result.start.imply("day", startMoment.date());
51
+ }
52
+ if (match[YEAR_GROUP]) {
53
+ let year = parseInt(match[YEAR_GROUP]);
54
+ if (isNaN(year))
55
+ year = constants_1.zhStringToYear(match[YEAR_GROUP]);
56
+ result.start.assign("year", year);
57
+ }
58
+ else {
59
+ result.start.imply("year", startMoment.year());
60
+ }
61
+ return result;
62
+ }
63
+ }
64
+ exports.default = ZHHantDateParser;
@@ -0,0 +1,6 @@
1
+ import { ParsingContext } from "../../../../chrono";
2
+ import { AbstractParserWithWordBoundaryChecking } from "../../../../common/parsers/AbstractParserWithWordBoundary";
3
+ export default class ZHHantDeadlineFormatParser extends AbstractParserWithWordBoundaryChecking {
4
+ innerPattern(): RegExp;
5
+ innerExtract(context: ParsingContext, match: RegExpMatchArray): import("../../../../results").ParsingResult;
6
+ }
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const dayjs_1 = __importDefault(require("dayjs"));
7
+ const AbstractParserWithWordBoundary_1 = require("../../../../common/parsers/AbstractParserWithWordBoundary");
8
+ const constants_1 = require("../constants");
9
+ const PATTERN = new RegExp("(\\d+|[" +
10
+ Object.keys(constants_1.NUMBER).join("") +
11
+ "]+|半|幾)(?:\\s*)" +
12
+ "(?:個)?" +
13
+ "(秒(?:鐘)?|分鐘|小時|鐘|日|天|星期|禮拜|月|年)" +
14
+ "(?:(?:之|過)?後|(?:之)?內)", "i");
15
+ const NUMBER_GROUP = 1;
16
+ const UNIT_GROUP = 2;
17
+ class ZHHantDeadlineFormatParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking {
18
+ innerPattern() {
19
+ return PATTERN;
20
+ }
21
+ innerExtract(context, match) {
22
+ const result = context.createParsingResult(match.index, match[0]);
23
+ let number = parseInt(match[NUMBER_GROUP]);
24
+ if (isNaN(number)) {
25
+ number = constants_1.zhStringToNumber(match[NUMBER_GROUP]);
26
+ }
27
+ if (isNaN(number)) {
28
+ const string = match[NUMBER_GROUP];
29
+ if (string === "幾") {
30
+ number = 3;
31
+ }
32
+ else if (string === "半") {
33
+ number = 0.5;
34
+ }
35
+ else {
36
+ return null;
37
+ }
38
+ }
39
+ let date = dayjs_1.default(context.refDate);
40
+ const unit = match[UNIT_GROUP];
41
+ const unitAbbr = unit[0];
42
+ if (unitAbbr.match(/[日天星禮月年]/)) {
43
+ if (unitAbbr == "日" || unitAbbr == "天") {
44
+ date = date.add(number, "d");
45
+ }
46
+ else if (unitAbbr == "星" || unitAbbr == "禮") {
47
+ date = date.add(number * 7, "d");
48
+ }
49
+ else if (unitAbbr == "月") {
50
+ date = date.add(number, "month");
51
+ }
52
+ else if (unitAbbr == "年") {
53
+ date = date.add(number, "year");
54
+ }
55
+ result.start.assign("year", date.year());
56
+ result.start.assign("month", date.month() + 1);
57
+ result.start.assign("day", date.date());
58
+ return result;
59
+ }
60
+ if (unitAbbr == "秒") {
61
+ date = date.add(number, "second");
62
+ }
63
+ else if (unitAbbr == "分") {
64
+ date = date.add(number, "minute");
65
+ }
66
+ else if (unitAbbr == "小" || unitAbbr == "鐘") {
67
+ date = date.add(number, "hour");
68
+ }
69
+ result.start.imply("year", date.year());
70
+ result.start.imply("month", date.month() + 1);
71
+ result.start.imply("day", date.date());
72
+ result.start.assign("hour", date.hour());
73
+ result.start.assign("minute", date.minute());
74
+ result.start.assign("second", date.second());
75
+ return result;
76
+ }
77
+ }
78
+ exports.default = ZHHantDeadlineFormatParser;
@@ -0,0 +1,7 @@
1
+ import { ParsingContext } from "../../../../chrono";
2
+ import { AbstractParserWithWordBoundaryChecking } from "../../../../common/parsers/AbstractParserWithWordBoundary";
3
+ import { ParsingResult } from "../../../../results";
4
+ export default class ZHHantRelationWeekdayParser extends AbstractParserWithWordBoundaryChecking {
5
+ innerPattern(): RegExp;
6
+ innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingResult;
7
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const dayjs_1 = __importDefault(require("dayjs"));
7
+ const AbstractParserWithWordBoundary_1 = require("../../../../common/parsers/AbstractParserWithWordBoundary");
8
+ const constants_1 = require("../constants");
9
+ const PATTERN = new RegExp("(?<prefix>上|今|下|這|呢)(?:個)?(?:星期|禮拜|週)(?<weekday>" + Object.keys(constants_1.WEEKDAY_OFFSET).join("|") + ")");
10
+ class ZHHantRelationWeekdayParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking {
11
+ innerPattern() {
12
+ return PATTERN;
13
+ }
14
+ innerExtract(context, match) {
15
+ const result = context.createParsingResult(match.index, match[0]);
16
+ const dayOfWeek = match.groups.weekday;
17
+ const offset = constants_1.WEEKDAY_OFFSET[dayOfWeek];
18
+ if (offset === undefined)
19
+ return null;
20
+ let modifier = null;
21
+ const prefix = match.groups.prefix;
22
+ if (prefix == "上") {
23
+ modifier = "last";
24
+ }
25
+ else if (prefix == "下") {
26
+ modifier = "next";
27
+ }
28
+ else if (prefix == "今" || prefix == "這" || prefix == "呢") {
29
+ modifier = "this";
30
+ }
31
+ let startMoment = dayjs_1.default(context.refDate);
32
+ let startMomentFixed = false;
33
+ const refOffset = startMoment.day();
34
+ if (modifier == "last" || modifier == "past") {
35
+ startMoment = startMoment.day(offset - 7);
36
+ startMomentFixed = true;
37
+ }
38
+ else if (modifier == "next") {
39
+ startMoment = startMoment.day(offset + 7);
40
+ startMomentFixed = true;
41
+ }
42
+ else if (modifier == "this") {
43
+ startMoment = startMoment.day(offset);
44
+ }
45
+ else {
46
+ if (Math.abs(offset - 7 - refOffset) < Math.abs(offset - refOffset)) {
47
+ startMoment = startMoment.day(offset - 7);
48
+ }
49
+ else if (Math.abs(offset + 7 - refOffset) < Math.abs(offset - refOffset)) {
50
+ startMoment = startMoment.day(offset + 7);
51
+ }
52
+ else {
53
+ startMoment = startMoment.day(offset);
54
+ }
55
+ }
56
+ result.start.assign("weekday", offset);
57
+ if (startMomentFixed) {
58
+ result.start.assign("day", startMoment.date());
59
+ result.start.assign("month", startMoment.month() + 1);
60
+ result.start.assign("year", startMoment.year());
61
+ }
62
+ else {
63
+ result.start.imply("day", startMoment.date());
64
+ result.start.imply("month", startMoment.month() + 1);
65
+ result.start.imply("year", startMoment.year());
66
+ }
67
+ return result;
68
+ }
69
+ }
70
+ exports.default = ZHHantRelationWeekdayParser;
@@ -0,0 +1,6 @@
1
+ import { ParsingContext } from "../../../../chrono";
2
+ import { AbstractParserWithWordBoundaryChecking } from "../../../../common/parsers/AbstractParserWithWordBoundary";
3
+ export default class ZHHantTimeExpressionParser extends AbstractParserWithWordBoundaryChecking {
4
+ innerPattern(): RegExp;
5
+ innerExtract(context: ParsingContext, match: RegExpMatchArray): import("../../../../results").ParsingResult;
6
+ }