chrono-node 2.3.1 → 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 (97) hide show
  1. package/README.md +1 -1
  2. package/dist/common/casualReferences.js +3 -1
  3. package/dist/common/parsers/AbstractParserWithWordBoundary.js +2 -1
  4. package/dist/common/refiners/ForwardDateRefiner.js +1 -1
  5. package/dist/index.d.ts +2 -1
  6. package/dist/index.js +3 -1
  7. package/dist/locales/de/constants.js +1 -1
  8. package/dist/locales/en/constants.js +1 -1
  9. package/dist/locales/en/parsers/ENRelativeDateFormatParser.js +3 -3
  10. package/dist/locales/en/parsers/ENTimeUnitAgoFormatParser.js +1 -1
  11. package/dist/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js +1 -1
  12. package/dist/locales/en/parsers/ENTimeUnitLaterFormatParser.js +1 -1
  13. package/dist/locales/en/parsers/ENTimeUnitWithinFormatParser.js +1 -1
  14. package/dist/locales/fr/constants.js +1 -1
  15. package/dist/locales/fr/parsers/FRTimeUnitAgoFormatParser.js +1 -1
  16. package/dist/locales/fr/parsers/FRTimeUnitRelativeFormatParser.js +1 -1
  17. package/dist/locales/fr/parsers/FRTimeUnitWithinFormatParser.js +1 -1
  18. package/dist/locales/nl/parsers/NLRelativeDateFormatParser.js +3 -3
  19. package/dist/locales/nl/parsers/NLTimeUnitAgoFormatParser.js +1 -1
  20. package/dist/locales/nl/parsers/NLTimeUnitCasualRelativeFormatParser.js +1 -1
  21. package/dist/locales/nl/parsers/NLTimeUnitLaterFormatParser.js +1 -1
  22. package/dist/locales/nl/parsers/NLTimeUnitWithinFormatParser.js +1 -1
  23. package/dist/locales/zh/hant/constants.d.ts +28 -0
  24. package/dist/locales/zh/hant/constants.js +52 -0
  25. package/dist/locales/zh/hant/parsers/ZHHantCasualDateParser.d.ts +7 -0
  26. package/dist/locales/zh/hant/parsers/ZHHantCasualDateParser.js +138 -0
  27. package/dist/locales/zh/hant/parsers/ZHHantDateParser.d.ts +6 -0
  28. package/dist/locales/zh/hant/parsers/ZHHantDateParser.js +64 -0
  29. package/dist/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.d.ts +6 -0
  30. package/dist/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.js +78 -0
  31. package/dist/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.d.ts +7 -0
  32. package/dist/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.js +70 -0
  33. package/dist/locales/zh/hant/parsers/ZHHantTimeExpressionParser.d.ts +6 -0
  34. package/dist/locales/zh/hant/parsers/ZHHantTimeExpressionParser.js +438 -0
  35. package/dist/locales/zh/hant/parsers/ZHHantWeekdayParser.d.ts +7 -0
  36. package/dist/locales/zh/hant/parsers/ZHHantWeekdayParser.js +46 -0
  37. package/dist/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.d.ts +4 -0
  38. package/dist/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.js +12 -0
  39. package/dist/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.d.ts +4 -0
  40. package/dist/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.js +12 -0
  41. package/dist/locales/zh/index.d.ts +9 -0
  42. package/dist/locales/zh/index.js +49 -0
  43. package/dist/results.d.ts +2 -2
  44. package/dist/results.js +18 -12
  45. package/dist/timezone.d.ts +1 -1
  46. package/dist/timezone.js +3 -0
  47. package/dist/utils/dayjs.js +7 -0
  48. package/package.json +1 -1
  49. package/src/common/casualReferences.ts +3 -1
  50. package/src/common/parsers/AbstractParserWithWordBoundary.ts +1 -1
  51. package/src/common/refiners/ForwardDateRefiner.ts +1 -1
  52. package/src/index.ts +2 -1
  53. package/src/locales/de/constants.ts +1 -1
  54. package/src/locales/en/constants.ts +1 -1
  55. package/src/locales/en/parsers/ENRelativeDateFormatParser.ts +3 -3
  56. package/src/locales/en/parsers/ENTimeUnitAgoFormatParser.ts +1 -1
  57. package/src/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.ts +1 -1
  58. package/src/locales/en/parsers/ENTimeUnitLaterFormatParser.ts +1 -1
  59. package/src/locales/en/parsers/ENTimeUnitWithinFormatParser.ts +1 -1
  60. package/src/locales/fr/constants.ts +1 -1
  61. package/src/locales/fr/parsers/FRTimeUnitAgoFormatParser.ts +1 -1
  62. package/src/locales/fr/parsers/FRTimeUnitRelativeFormatParser.ts +1 -1
  63. package/src/locales/fr/parsers/FRTimeUnitWithinFormatParser.ts +1 -1
  64. package/src/locales/nl/parsers/NLRelativeDateFormatParser.ts +3 -3
  65. package/src/locales/nl/parsers/NLTimeUnitAgoFormatParser.ts +1 -1
  66. package/src/locales/nl/parsers/NLTimeUnitCasualRelativeFormatParser.ts +1 -1
  67. package/src/locales/nl/parsers/NLTimeUnitLaterFormatParser.ts +1 -1
  68. package/src/locales/nl/parsers/NLTimeUnitWithinFormatParser.ts +1 -1
  69. package/src/locales/zh/hant/constants.ts +53 -0
  70. package/src/locales/zh/hant/parsers/ZHHantCasualDateParser.ts +128 -0
  71. package/src/locales/zh/hant/parsers/ZHHantDateParser.ts +68 -0
  72. package/src/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.ts +81 -0
  73. package/src/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.ts +69 -0
  74. package/src/locales/zh/hant/parsers/ZHHantTimeExpressionParser.ts +424 -0
  75. package/src/locales/zh/hant/parsers/ZHHantWeekdayParser.ts +46 -0
  76. package/src/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.ts +7 -0
  77. package/src/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.ts +7 -0
  78. package/src/locales/zh/index.ts +63 -0
  79. package/src/results.ts +22 -12
  80. package/src/timezone.ts +5 -1
  81. package/src/utils/dayjs.ts +6 -0
  82. package/test/en/en_casual.test.ts +11 -0
  83. package/test/en/en_month_name_little_endian.test.ts +10 -0
  84. package/test/en/en_relative.test.ts +8 -0
  85. package/test/en/en_slash.test.ts +11 -0
  86. package/test/en/en_time_units_ago.test.ts +16 -0
  87. package/test/en/en_time_units_later.test.ts +6 -0
  88. package/test/en/en_timezone_exp.test.ts +85 -10
  89. package/test/en/en_weekday.test.ts +69 -25
  90. package/test/en/negative_cases.test.ts +2 -0
  91. package/test/test_util.ts +5 -2
  92. package/test/zh/zh.test.ts +18 -0
  93. package/test/zh/zh_hant_casual.test.ts +246 -0
  94. package/test/zh/zh_hant_date.test.ts +103 -0
  95. package/test/zh/zh_hant_deadline.test.ts +136 -0
  96. package/test/zh/zh_hant_time_exp.test.ts +182 -0
  97. 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
@@ -13,7 +13,9 @@ function now(reference) {
13
13
  const component = new results_1.ParsingComponents(reference, {});
14
14
  dayjs_2.assignSimilarDate(component, targetDate);
15
15
  dayjs_2.assignSimilarTime(component, targetDate);
16
- component.assign("timezoneOffset", targetDate.utcOffset());
16
+ if (reference.timezoneOffset !== null) {
17
+ component.assign("timezoneOffset", targetDate.utcOffset());
18
+ }
17
19
  return component;
18
20
  }
19
21
  exports.now = now;
@@ -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++) {
@@ -26,7 +26,7 @@ class ForwardDateRefiner {
26
26
  }
27
27
  }
28
28
  if (result.start.isOnlyWeekdayComponent() && refMoment.isAfter(result.start.dayjs())) {
29
- if (refMoment.day() > result.start.get("weekday")) {
29
+ if (refMoment.day() >= result.start.get("weekday")) {
30
30
  refMoment = refMoment.day(result.start.get("weekday") + 7);
31
31
  }
32
32
  else {
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) {
@@ -100,7 +100,7 @@ exports.TIME_UNIT_DICTIONARY = {
100
100
  year: "year",
101
101
  years: "year",
102
102
  };
103
- exports.NUMBER_PATTERN = `(?:${pattern_1.matchAnyPattern(exports.INTEGER_WORD_DICTIONARY)}|[0-9]+|[0-9]+\\.[0-9]+|half(?:\\s*an?)?|an?(?:\\s*few)?|few|several|a?\\s*couple\\s*(?:of)?)`;
103
+ exports.NUMBER_PATTERN = `(?:${pattern_1.matchAnyPattern(exports.INTEGER_WORD_DICTIONARY)}|[0-9]+|[0-9]+\\.[0-9]+|half(?:\\s*an?)?|an?\\b(?:\\s*few)?|few|several|a?\\s*couple\\s*(?:of)?)`;
104
104
  function parseNumberPattern(match) {
105
105
  const num = match.toLowerCase();
106
106
  if (exports.INTEGER_WORD_DICTIONARY[num] !== undefined) {
@@ -126,7 +126,7 @@ exports.TIME_UNIT_DICTIONARY = {
126
126
  year: "year",
127
127
  years: "year",
128
128
  };
129
- exports.NUMBER_PATTERN = `(?:${pattern_1.matchAnyPattern(exports.INTEGER_WORD_DICTIONARY)}|[0-9]+|[0-9]+\\.[0-9]+|half(?:\\s{0,2}an?)?|an?(?:\\s{0,2}few)?|few|several|a?\\s{0,2}couple\\s{0,2}(?:of)?)`;
129
+ exports.NUMBER_PATTERN = `(?:${pattern_1.matchAnyPattern(exports.INTEGER_WORD_DICTIONARY)}|[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)?)`;
130
130
  function parseNumberPattern(match) {
131
131
  const num = match.toLowerCase();
132
132
  if (exports.INTEGER_WORD_DICTIONARY[num] !== undefined) {
@@ -22,15 +22,15 @@ class ENRelativeDateFormatParser extends AbstractParserWithWordBoundary_1.Abstra
22
22
  if (modifier == "next") {
23
23
  const timeUnits = {};
24
24
  timeUnits[timeunit] = 1;
25
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, timeUnits);
25
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
26
26
  }
27
27
  if (modifier == "last" || modifier == "past") {
28
28
  const timeUnits = {};
29
29
  timeUnits[timeunit] = -1;
30
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, timeUnits);
30
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
31
31
  }
32
32
  const components = context.createParsingComponents();
33
- let date = dayjs_1.default(context.refDate);
33
+ let date = dayjs_1.default(context.reference.instant);
34
34
  if (unitWord.match(/week/i)) {
35
35
  date = date.add(-date.get("d"), "d");
36
36
  components.imply("day", date.date());
@@ -17,7 +17,7 @@ class ENTimeUnitAgoFormatParser extends AbstractParserWithWordBoundary_1.Abstrac
17
17
  innerExtract(context, match) {
18
18
  const timeUnits = constants_1.parseTimeUnits(match[1]);
19
19
  const outputTimeUnits = timeunits_1.reverseTimeUnits(timeUnits);
20
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, outputTimeUnits);
20
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, outputTimeUnits);
21
21
  }
22
22
  }
23
23
  exports.default = ENTimeUnitAgoFormatParser;
@@ -19,7 +19,7 @@ class ENTimeUnitCasualRelativeFormatParser extends AbstractParserWithWordBoundar
19
19
  timeUnits = timeunits_1.reverseTimeUnits(timeUnits);
20
20
  break;
21
21
  }
22
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, timeUnits);
22
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
23
23
  }
24
24
  }
25
25
  exports.default = ENTimeUnitCasualRelativeFormatParser;
@@ -16,7 +16,7 @@ class ENTimeUnitLaterFormatParser extends AbstractParserWithWordBoundary_1.Abstr
16
16
  }
17
17
  innerExtract(context, match) {
18
18
  const fragments = constants_1.parseTimeUnits(match[GROUP_NUM_TIMEUNITS]);
19
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, fragments);
19
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, fragments);
20
20
  }
21
21
  }
22
22
  exports.default = ENTimeUnitLaterFormatParser;
@@ -12,7 +12,7 @@ class ENTimeUnitWithinFormatParser extends AbstractParserWithWordBoundary_1.Abst
12
12
  }
13
13
  innerExtract(context, match) {
14
14
  const timeUnits = constants_1.parseTimeUnits(match[1]);
15
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, timeUnits);
15
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
16
16
  }
17
17
  }
18
18
  exports.default = ENTimeUnitWithinFormatParser;
@@ -98,7 +98,7 @@ exports.TIME_UNIT_DICTIONARY = {
98
98
  "année": "year",
99
99
  "années": "year",
100
100
  };
101
- exports.NUMBER_PATTERN = `(?:${pattern_1.matchAnyPattern(exports.INTEGER_WORD_DICTIONARY)}|[0-9]+|[0-9]+\\.[0-9]+|une?|quelques?|demi-?)`;
101
+ exports.NUMBER_PATTERN = `(?:${pattern_1.matchAnyPattern(exports.INTEGER_WORD_DICTIONARY)}|[0-9]+|[0-9]+\\.[0-9]+|une?\\b|quelques?|demi-?)`;
102
102
  function parseNumberPattern(match) {
103
103
  const num = match.toLowerCase();
104
104
  if (exports.INTEGER_WORD_DICTIONARY[num] !== undefined) {
@@ -14,7 +14,7 @@ class FRTimeUnitAgoFormatParser extends AbstractParserWithWordBoundary_1.Abstrac
14
14
  innerExtract(context, match) {
15
15
  const timeUnits = constants_1.parseTimeUnits(match[1]);
16
16
  const outputTimeUnits = timeunits_1.reverseTimeUnits(timeUnits);
17
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, outputTimeUnits);
17
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, outputTimeUnits);
18
18
  }
19
19
  }
20
20
  exports.default = FRTimeUnitAgoFormatParser;
@@ -29,7 +29,7 @@ class FRTimeUnitAgoFormatParser extends AbstractParserWithWordBoundary_1.Abstrac
29
29
  if (/derni[eè]re?s?/.test(modifier) || /pass[ée]e?s?/.test(modifier) || /pr[ée]c[ée]dents?/.test(modifier)) {
30
30
  timeUnits = timeunits_1.reverseTimeUnits(timeUnits);
31
31
  }
32
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, timeUnits);
32
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
33
33
  }
34
34
  }
35
35
  exports.default = FRTimeUnitAgoFormatParser;
@@ -9,7 +9,7 @@ class FRTimeUnitWithinFormatParser extends AbstractParserWithWordBoundary_1.Abst
9
9
  }
10
10
  innerExtract(context, match) {
11
11
  const timeUnits = constants_1.parseTimeUnits(match[1]);
12
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, timeUnits);
12
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
13
13
  }
14
14
  }
15
15
  exports.default = FRTimeUnitWithinFormatParser;
@@ -23,15 +23,15 @@ class NLRelativeDateFormatParser extends AbstractParserWithWordBoundary_1.Abstra
23
23
  if (modifier == "volgend" || modifier == "volgende" || modifier == "komende") {
24
24
  const timeUnits = {};
25
25
  timeUnits[timeunit] = 1;
26
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, timeUnits);
26
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
27
27
  }
28
28
  if (modifier == "afgelopen" || modifier == "vorige") {
29
29
  const timeUnits = {};
30
30
  timeUnits[timeunit] = -1;
31
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, timeUnits);
31
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
32
32
  }
33
33
  const components = context.createParsingComponents();
34
- let date = dayjs_1.default(context.refDate);
34
+ let date = dayjs_1.default(context.reference.instant);
35
35
  if (unitWord.match(/week/i)) {
36
36
  date = date.add(-date.get("d"), "d");
37
37
  components.imply("day", date.date());
@@ -17,7 +17,7 @@ class NLTimeUnitAgoFormatParser extends AbstractParserWithWordBoundary_1.Abstrac
17
17
  innerExtract(context, match) {
18
18
  const timeUnits = constants_1.parseTimeUnits(match[1]);
19
19
  const outputTimeUnits = timeunits_1.reverseTimeUnits(timeUnits);
20
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, outputTimeUnits);
20
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, outputTimeUnits);
21
21
  }
22
22
  }
23
23
  exports.default = NLTimeUnitAgoFormatParser;
@@ -19,7 +19,7 @@ class NLTimeUnitCasualRelativeFormatParser extends AbstractParserWithWordBoundar
19
19
  timeUnits = timeunits_1.reverseTimeUnits(timeUnits);
20
20
  break;
21
21
  }
22
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, timeUnits);
22
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
23
23
  }
24
24
  }
25
25
  exports.default = NLTimeUnitCasualRelativeFormatParser;
@@ -16,7 +16,7 @@ class NLTimeUnitLaterFormatParser extends AbstractParserWithWordBoundary_1.Abstr
16
16
  }
17
17
  innerExtract(context, match) {
18
18
  const fragments = constants_1.parseTimeUnits(match[GROUP_NUM_TIMEUNITS]);
19
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, fragments);
19
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, fragments);
20
20
  }
21
21
  }
22
22
  exports.default = NLTimeUnitLaterFormatParser;
@@ -9,7 +9,7 @@ class NLTimeUnitWithinFormatParser extends AbstractParserWithWordBoundary_1.Abst
9
9
  }
10
10
  innerExtract(context, match) {
11
11
  const timeUnits = constants_1.parseTimeUnits(match[1]);
12
- return results_1.ParsingComponents.createRelativeFromRefInstant(context.refDate, timeUnits);
12
+ return results_1.ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
13
13
  }
14
14
  }
15
15
  exports.default = NLTimeUnitWithinFormatParser;
@@ -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;