chrono-node 2.3.3 → 2.3.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.
Files changed (109) hide show
  1. package/README.md +2 -2
  2. package/dist/common/casualReferences.js +3 -1
  3. package/dist/common/parsers/AbstractParserWithWordBoundary.js +2 -1
  4. package/dist/common/refiners/ExtractTimezoneAbbrRefiner.js +5 -0
  5. package/dist/index.d.ts +2 -1
  6. package/dist/index.js +3 -1
  7. package/dist/locales/en/index.js +2 -1
  8. package/dist/locales/en/parsers/ENRelativeDateFormatParser.js +2 -2
  9. package/dist/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js +1 -1
  10. package/dist/locales/en/refiners/ENMergeRelativeDateRefiner.d.ts +7 -0
  11. package/dist/locales/en/refiners/ENMergeRelativeDateRefiner.js +35 -0
  12. package/dist/locales/zh/hans/constants.d.ts +27 -0
  13. package/dist/locales/zh/hans/constants.js +51 -0
  14. package/dist/locales/zh/hans/index.d.ts +9 -0
  15. package/dist/locales/zh/hans/index.js +49 -0
  16. package/dist/locales/zh/hans/parsers/ZHHansCasualDateParser.d.ts +7 -0
  17. package/dist/locales/zh/hans/parsers/ZHHansCasualDateParser.js +138 -0
  18. package/dist/locales/zh/hans/parsers/ZHHansDateParser.d.ts +6 -0
  19. package/dist/locales/zh/hans/parsers/ZHHansDateParser.js +72 -0
  20. package/dist/locales/zh/hans/parsers/ZHHansDeadlineFormatParser.d.ts +6 -0
  21. package/dist/locales/zh/hans/parsers/ZHHansDeadlineFormatParser.js +78 -0
  22. package/dist/locales/zh/hans/parsers/ZHHansRelationWeekdayParser.d.ts +7 -0
  23. package/dist/locales/zh/hans/parsers/ZHHansRelationWeekdayParser.js +70 -0
  24. package/dist/locales/zh/hans/parsers/ZHHansTimeExpressionParser.d.ts +6 -0
  25. package/dist/locales/zh/hans/parsers/ZHHansTimeExpressionParser.js +438 -0
  26. package/dist/locales/zh/hans/parsers/ZHHansWeekdayParser.d.ts +7 -0
  27. package/dist/locales/zh/hans/parsers/ZHHansWeekdayParser.js +46 -0
  28. package/dist/locales/zh/hans/refiners/ZHHansMergeDateRangeRefiner.d.ts +4 -0
  29. package/dist/locales/zh/hans/refiners/ZHHansMergeDateRangeRefiner.js +12 -0
  30. package/dist/locales/zh/hans/refiners/ZHHansMergeDateTimeRefiner.d.ts +4 -0
  31. package/dist/locales/zh/hans/refiners/ZHHansMergeDateTimeRefiner.js +12 -0
  32. package/dist/locales/zh/hant/constants.d.ts +28 -0
  33. package/dist/locales/zh/hant/constants.js +52 -0
  34. package/dist/locales/zh/hant/index.d.ts +9 -0
  35. package/dist/locales/zh/hant/index.js +49 -0
  36. package/dist/locales/zh/hant/parsers/ZHHantCasualDateParser.d.ts +7 -0
  37. package/dist/locales/zh/hant/parsers/ZHHantCasualDateParser.js +138 -0
  38. package/dist/locales/zh/hant/parsers/ZHHantDateParser.d.ts +6 -0
  39. package/dist/locales/zh/hant/parsers/ZHHantDateParser.js +64 -0
  40. package/dist/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.d.ts +6 -0
  41. package/dist/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.js +78 -0
  42. package/dist/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.d.ts +7 -0
  43. package/dist/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.js +70 -0
  44. package/dist/locales/zh/hant/parsers/ZHHantTimeExpressionParser.d.ts +6 -0
  45. package/dist/locales/zh/hant/parsers/ZHHantTimeExpressionParser.js +438 -0
  46. package/dist/locales/zh/hant/parsers/ZHHantWeekdayParser.d.ts +7 -0
  47. package/dist/locales/zh/hant/parsers/ZHHantWeekdayParser.js +46 -0
  48. package/dist/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.d.ts +4 -0
  49. package/dist/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.js +12 -0
  50. package/dist/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.d.ts +4 -0
  51. package/dist/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.js +12 -0
  52. package/dist/locales/zh/index.d.ts +2 -0
  53. package/dist/locales/zh/index.js +16 -0
  54. package/dist/results.js +8 -5
  55. package/dist/timezone.js +1 -1
  56. package/dist/utils/dayjs.js +7 -0
  57. package/package.json +1 -1
  58. package/src/common/casualReferences.ts +3 -1
  59. package/src/common/parsers/AbstractParserWithWordBoundary.ts +1 -1
  60. package/src/common/refiners/ExtractTimezoneAbbrRefiner.ts +8 -0
  61. package/src/index.ts +2 -1
  62. package/src/locales/en/index.ts +2 -1
  63. package/src/locales/en/parsers/ENMonthNameLittleEndianParser.ts +0 -2
  64. package/src/locales/en/parsers/ENRelativeDateFormatParser.ts +2 -2
  65. package/src/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.ts +1 -1
  66. package/src/locales/en/refiners/ENMergeRelativeDateRefiner.ts +58 -0
  67. package/src/locales/zh/hans/constants.ts +52 -0
  68. package/src/locales/zh/hans/index.ts +62 -0
  69. package/src/locales/zh/hans/parsers/ZHHansCasualDateParser.ts +128 -0
  70. package/src/locales/zh/hans/parsers/ZHHansDateParser.ts +75 -0
  71. package/src/locales/zh/hans/parsers/ZHHansDeadlineFormatParser.ts +81 -0
  72. package/src/locales/zh/hans/parsers/ZHHansRelationWeekdayParser.ts +69 -0
  73. package/src/locales/zh/hans/parsers/ZHHansTimeExpressionParser.ts +424 -0
  74. package/src/locales/zh/hans/parsers/ZHHansWeekdayParser.ts +46 -0
  75. package/src/locales/zh/hans/refiners/ZHHansMergeDateRangeRefiner.ts +7 -0
  76. package/src/locales/zh/hans/refiners/ZHHansMergeDateTimeRefiner.ts +7 -0
  77. package/src/locales/zh/hant/constants.ts +53 -0
  78. package/src/locales/zh/hant/index.ts +63 -0
  79. package/src/locales/zh/hant/parsers/ZHHantCasualDateParser.ts +128 -0
  80. package/src/locales/zh/hant/parsers/ZHHantDateParser.ts +68 -0
  81. package/src/locales/zh/hant/parsers/ZHHantDeadlineFormatParser.ts +81 -0
  82. package/src/locales/zh/hant/parsers/ZHHantRelationWeekdayParser.ts +69 -0
  83. package/src/locales/zh/hant/parsers/ZHHantTimeExpressionParser.ts +424 -0
  84. package/src/locales/zh/hant/parsers/ZHHantWeekdayParser.ts +46 -0
  85. package/src/locales/zh/hant/refiners/ZHHantMergeDateRangeRefiner.ts +7 -0
  86. package/src/locales/zh/hant/refiners/ZHHantMergeDateTimeRefiner.ts +7 -0
  87. package/src/locales/zh/index.ts +2 -0
  88. package/src/results.ts +11 -4
  89. package/src/timezone.ts +1 -1
  90. package/src/utils/dayjs.ts +6 -0
  91. package/test/en/en_casual.test.ts +11 -0
  92. package/test/en/en_merging_relative_dates.test.ts +51 -0
  93. package/test/en/en_month.test.ts +13 -0
  94. package/test/en/en_relative.test.ts +20 -0
  95. package/test/en/en_time_units_ago.test.ts +16 -0
  96. package/test/en/en_time_units_casual_relative.test.ts +16 -0
  97. package/test/en/en_time_units_later.test.ts +6 -0
  98. package/test/en/en_timezone_exp.test.ts +85 -10
  99. package/test/zh/zh.test.ts +18 -0
  100. package/test/zh/zh_hans_casual.test.ts +228 -0
  101. package/test/zh/zh_hans_date.test.ts +102 -0
  102. package/test/zh/zh_hans_deadline.test.ts +136 -0
  103. package/test/zh/zh_hans_time_exp.test.ts +182 -0
  104. package/test/zh/zh_hans_weekday.test.ts +132 -0
  105. package/test/zh/zh_hant_casual.test.ts +246 -0
  106. package/test/zh/zh_hant_date.test.ts +102 -0
  107. package/test/zh/zh_hant_deadline.test.ts +136 -0
  108. package/test/zh/zh_hant_time_exp.test.ts +182 -0
  109. 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
@@ -133,7 +133,7 @@ For example:
133
133
  ```js
134
134
  const results = chrono.parse('I have an appointment tomorrow from 10 to 11 AM');
135
135
 
136
- results[0].index; // 15
136
+ results[0].index; // 22
137
137
  results[0].text; // 'tomorrow from 10 to 11 AM'
138
138
  results[0].refDate; // Sat Dec 13 2014 21:50:14 GMT-0600 (CST)
139
139
 
@@ -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++) {
@@ -225,6 +225,11 @@ class ExtractTimezoneAbbrRefiner {
225
225
  return;
226
226
  }
227
227
  }
228
+ if (result.start.isOnlyDate()) {
229
+ if (timezoneAbbr != match[1]) {
230
+ return;
231
+ }
232
+ }
228
233
  result.text += match[0];
229
234
  if (!result.start.isCertain("timezoneOffset")) {
230
235
  result.start.assign("timezoneOffset", extractedTimezoneOffset);
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) {
@@ -23,6 +23,7 @@ const ENRelativeDateFormatParser_1 = __importDefault(require("./parsers/ENRelati
23
23
  const chrono_1 = require("../../chrono");
24
24
  const SlashDateFormatParser_1 = __importDefault(require("../../common/parsers/SlashDateFormatParser"));
25
25
  const ENTimeUnitCasualRelativeFormatParser_1 = __importDefault(require("./parsers/ENTimeUnitCasualRelativeFormatParser"));
26
+ const ENMergeRelativeDateRefiner_1 = __importDefault(require("./refiners/ENMergeRelativeDateRefiner"));
26
27
  exports.casual = new chrono_1.Chrono(createCasualConfiguration(false));
27
28
  exports.strict = new chrono_1.Chrono(createConfiguration(true, false));
28
29
  exports.GB = new chrono_1.Chrono(createConfiguration(false, true));
@@ -58,7 +59,7 @@ function createConfiguration(strictMode = true, littleEndian = false) {
58
59
  new ENTimeUnitAgoFormatParser_1.default(strictMode),
59
60
  new ENTimeUnitLaterFormatParser_1.default(strictMode),
60
61
  ],
61
- refiners: [new ENMergeDateTimeRefiner_1.default(), new ENMergeDateRangeRefiner_1.default()],
62
+ refiners: [new ENMergeRelativeDateRefiner_1.default(), new ENMergeDateTimeRefiner_1.default(), new ENMergeDateRangeRefiner_1.default()],
62
63
  }, strictMode);
63
64
  }
64
65
  exports.createConfiguration = createConfiguration;
@@ -8,7 +8,7 @@ const results_1 = require("../../../results");
8
8
  const dayjs_1 = __importDefault(require("dayjs"));
9
9
  const AbstractParserWithWordBoundary_1 = require("../../../common/parsers/AbstractParserWithWordBoundary");
10
10
  const pattern_1 = require("../../../utils/pattern");
11
- const PATTERN = new RegExp(`(this|next|last|past)\\s*(${pattern_1.matchAnyPattern(constants_1.TIME_UNIT_DICTIONARY)})(?=\\s*)` + "(?=\\W|$)", "i");
11
+ const PATTERN = new RegExp(`(this|last|past|next|after\\s*this)\\s*(${pattern_1.matchAnyPattern(constants_1.TIME_UNIT_DICTIONARY)})(?=\\s*)` + "(?=\\W|$)", "i");
12
12
  const MODIFIER_WORD_GROUP = 1;
13
13
  const RELATIVE_WORD_GROUP = 2;
14
14
  class ENRelativeDateFormatParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking {
@@ -19,7 +19,7 @@ class ENRelativeDateFormatParser extends AbstractParserWithWordBoundary_1.Abstra
19
19
  const modifier = match[MODIFIER_WORD_GROUP].toLowerCase();
20
20
  const unitWord = match[RELATIVE_WORD_GROUP].toLowerCase();
21
21
  const timeunit = constants_1.TIME_UNIT_DICTIONARY[unitWord];
22
- if (modifier == "next") {
22
+ if (modifier == "next" || modifier.startsWith("after")) {
23
23
  const timeUnits = {};
24
24
  timeUnits[timeunit] = 1;
25
25
  return results_1.ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
@@ -4,7 +4,7 @@ const constants_1 = require("../constants");
4
4
  const results_1 = require("../../../results");
5
5
  const AbstractParserWithWordBoundary_1 = require("../../../common/parsers/AbstractParserWithWordBoundary");
6
6
  const timeunits_1 = require("../../../utils/timeunits");
7
- const PATTERN = new RegExp(`(this|last|past|next|\\+|-)\\s*(${constants_1.TIME_UNITS_PATTERN})(?=\\W|$)`, "i");
7
+ const PATTERN = new RegExp(`(this|last|past|next|after|\\+|-)\\s*(${constants_1.TIME_UNITS_PATTERN})(?=\\W|$)`, "i");
8
8
  class ENTimeUnitCasualRelativeFormatParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking {
9
9
  innerPattern() {
10
10
  return PATTERN;
@@ -0,0 +1,7 @@
1
+ import { MergingRefiner } from "../../../common/abstractRefiners";
2
+ import { ParsingResult } from "../../../results";
3
+ export default class ENMergeRelativeDateRefiner extends MergingRefiner {
4
+ patternBetween(): RegExp;
5
+ shouldMergeResults(textBetween: string, currentResult: ParsingResult, nextResult: ParsingResult): boolean;
6
+ mergeResults(textBetween: string, currentResult: ParsingResult, nextResult: ParsingResult): ParsingResult;
7
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const abstractRefiners_1 = require("../../../common/abstractRefiners");
4
+ const results_1 = require("../../../results");
5
+ const constants_1 = require("../constants");
6
+ const timeunits_1 = require("../../../utils/timeunits");
7
+ function hasImpliedEarlierReferenceDate(result) {
8
+ return result.text.match(/\s+(before|from)$/i) != null;
9
+ }
10
+ function hasImpliedLaterReferenceDate(result) {
11
+ return result.text.match(/\s+(after|since)$/i) != null;
12
+ }
13
+ class ENMergeRelativeDateRefiner extends abstractRefiners_1.MergingRefiner {
14
+ patternBetween() {
15
+ return /^\s*$/i;
16
+ }
17
+ shouldMergeResults(textBetween, currentResult, nextResult) {
18
+ if (!textBetween.match(this.patternBetween())) {
19
+ return false;
20
+ }
21
+ if (!hasImpliedEarlierReferenceDate(currentResult) && !hasImpliedLaterReferenceDate(currentResult)) {
22
+ return false;
23
+ }
24
+ return !!nextResult.start.get("day") && !!nextResult.start.get("month") && !!nextResult.start.get("year");
25
+ }
26
+ mergeResults(textBetween, currentResult, nextResult) {
27
+ let timeUnits = constants_1.parseTimeUnits(currentResult.text);
28
+ if (hasImpliedEarlierReferenceDate(currentResult)) {
29
+ timeUnits = timeunits_1.reverseTimeUnits(timeUnits);
30
+ }
31
+ const components = results_1.ParsingComponents.createRelativeFromReference(new results_1.ReferenceWithTimezone(nextResult.start.date()), timeUnits);
32
+ return new results_1.ParsingResult(nextResult.reference, currentResult.index, `${currentResult.text}${textBetween}${nextResult.text}`, components);
33
+ }
34
+ }
35
+ exports.default = ENMergeRelativeDateRefiner;
@@ -0,0 +1,27 @@
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
+ };
16
+ export declare const WEEKDAY_OFFSET: {
17
+ 天: number;
18
+ 日: number;
19
+ 一: number;
20
+ 二: number;
21
+ 三: number;
22
+ 四: number;
23
+ 五: number;
24
+ 六: number;
25
+ };
26
+ export declare function zhStringToNumber(text: string): number;
27
+ export declare function zhStringToYear(text: string): number;
@@ -0,0 +1,51 @@
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
+ "〇": 0,
7
+ "一": 1,
8
+ "二": 2,
9
+ "两": 2,
10
+ "三": 3,
11
+ "四": 4,
12
+ "五": 5,
13
+ "六": 6,
14
+ "七": 7,
15
+ "八": 8,
16
+ "九": 9,
17
+ "十": 10,
18
+ };
19
+ exports.WEEKDAY_OFFSET = {
20
+ "天": 0,
21
+ "日": 0,
22
+ "一": 1,
23
+ "二": 2,
24
+ "三": 3,
25
+ "四": 4,
26
+ "五": 5,
27
+ "六": 6,
28
+ };
29
+ function zhStringToNumber(text) {
30
+ let number = 0;
31
+ for (let i = 0; i < text.length; i++) {
32
+ const char = text[i];
33
+ if (char === "十") {
34
+ number = number === 0 ? exports.NUMBER[char] : number * exports.NUMBER[char];
35
+ }
36
+ else {
37
+ number += exports.NUMBER[char];
38
+ }
39
+ }
40
+ return number;
41
+ }
42
+ exports.zhStringToNumber = zhStringToNumber;
43
+ function zhStringToYear(text) {
44
+ let string = "";
45
+ for (let i = 0; i < text.length; i++) {
46
+ const char = text[i];
47
+ string = string + exports.NUMBER[char];
48
+ }
49
+ return parseInt(string);
50
+ }
51
+ exports.zhStringToYear = zhStringToYear;
@@ -0,0 +1,9 @@
1
+ import { Chrono, Configuration } from "../../../chrono";
2
+ import { ParsedResult, ParsingOption } from "../../../index";
3
+ export declare const hans: Chrono;
4
+ export declare const casual: Chrono;
5
+ export declare const strict: Chrono;
6
+ export declare function parse(text: string, ref?: Date, option?: ParsingOption): ParsedResult[];
7
+ export declare function parseDate(text: string, ref?: Date, option?: ParsingOption): Date;
8
+ export declare function createCasualConfiguration(): Configuration;
9
+ export declare function createConfiguration(): Configuration;
@@ -0,0 +1,49 @@
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
+ exports.createConfiguration = exports.createCasualConfiguration = exports.parseDate = exports.parse = exports.strict = exports.casual = exports.hans = void 0;
7
+ const chrono_1 = require("../../../chrono");
8
+ const ExtractTimezoneOffsetRefiner_1 = __importDefault(require("../../../common/refiners/ExtractTimezoneOffsetRefiner"));
9
+ const configurations_1 = require("../../../configurations");
10
+ const ZHHansCasualDateParser_1 = __importDefault(require("./parsers/ZHHansCasualDateParser"));
11
+ const ZHHansDateParser_1 = __importDefault(require("./parsers/ZHHansDateParser"));
12
+ const ZHHansDeadlineFormatParser_1 = __importDefault(require("./parsers/ZHHansDeadlineFormatParser"));
13
+ const ZHHansRelationWeekdayParser_1 = __importDefault(require("./parsers/ZHHansRelationWeekdayParser"));
14
+ const ZHHansTimeExpressionParser_1 = __importDefault(require("./parsers/ZHHansTimeExpressionParser"));
15
+ const ZHHansWeekdayParser_1 = __importDefault(require("./parsers/ZHHansWeekdayParser"));
16
+ const ZHHansMergeDateRangeRefiner_1 = __importDefault(require("./refiners/ZHHansMergeDateRangeRefiner"));
17
+ const ZHHansMergeDateTimeRefiner_1 = __importDefault(require("./refiners/ZHHansMergeDateTimeRefiner"));
18
+ exports.hans = new chrono_1.Chrono(createCasualConfiguration());
19
+ exports.casual = new chrono_1.Chrono(createCasualConfiguration());
20
+ exports.strict = new chrono_1.Chrono(createConfiguration());
21
+ function parse(text, ref, option) {
22
+ return exports.casual.parse(text, ref, option);
23
+ }
24
+ exports.parse = parse;
25
+ function parseDate(text, ref, option) {
26
+ return exports.casual.parseDate(text, ref, option);
27
+ }
28
+ exports.parseDate = parseDate;
29
+ function createCasualConfiguration() {
30
+ const option = createConfiguration();
31
+ option.parsers.unshift(new ZHHansCasualDateParser_1.default());
32
+ return option;
33
+ }
34
+ exports.createCasualConfiguration = createCasualConfiguration;
35
+ function createConfiguration() {
36
+ const configuration = configurations_1.includeCommonConfiguration({
37
+ parsers: [
38
+ new ZHHansDateParser_1.default(),
39
+ new ZHHansRelationWeekdayParser_1.default(),
40
+ new ZHHansWeekdayParser_1.default(),
41
+ new ZHHansTimeExpressionParser_1.default(),
42
+ new ZHHansDeadlineFormatParser_1.default(),
43
+ ],
44
+ refiners: [new ZHHansMergeDateRangeRefiner_1.default(), new ZHHansMergeDateTimeRefiner_1.default()],
45
+ });
46
+ configuration.refiners = configuration.refiners.filter((refiner) => !(refiner instanceof ExtractTimezoneOffsetRefiner_1.default));
47
+ return configuration;
48
+ }
49
+ exports.createConfiguration = createConfiguration;
@@ -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 ZHHansCasualDateParser 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 ZHHansCasualDateParser 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 == "明") {
38
+ if (refMoment.hour() > 1) {
39
+ startMoment = startMoment.add(1, "day");
40
+ }
41
+ }
42
+ else if (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 == "早") {
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 == "上") {
69
+ result.start.imply("hour", 6);
70
+ }
71
+ else if (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 == "明") {
90
+ if (refMoment.hour() > 1) {
91
+ startMoment = startMoment.add(1, "day");
92
+ }
93
+ }
94
+ else if (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 == "上") {
113
+ result.start.imply("hour", 6);
114
+ }
115
+ else if (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 = ZHHansCasualDateParser;
@@ -0,0 +1,6 @@
1
+ import { ParsingContext } from "../../../../chrono";
2
+ import { AbstractParserWithWordBoundaryChecking } from "../../../../common/parsers/AbstractParserWithWordBoundary";
3
+ export default class ZHHansDateParser extends AbstractParserWithWordBoundaryChecking {
4
+ innerPattern(): RegExp;
5
+ innerExtract(context: ParsingContext, match: RegExpMatchArray): import("../../../../results").ParsingResult;
6
+ }
@@ -0,0 +1,72 @@
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 ZHHansDateParser extends AbstractParserWithWordBoundary_1.AbstractParserWithWordBoundaryChecking {
13
+ innerPattern() {
14
+ return new RegExp("(" +
15
+ "\\d{2,4}|" +
16
+ "[" +
17
+ Object.keys(constants_1.NUMBER).join("") +
18
+ "]{4}|" +
19
+ "[" +
20
+ Object.keys(constants_1.NUMBER).join("") +
21
+ "]{2}" +
22
+ ")?" +
23
+ "(?:\\s*)" +
24
+ "(?:年)?" +
25
+ "(?:[\\s|,|,]*)" +
26
+ "(" +
27
+ "\\d{1,2}|" +
28
+ "[" +
29
+ Object.keys(constants_1.NUMBER).join("") +
30
+ "]{1,3}" +
31
+ ")" +
32
+ "(?:\\s*)" +
33
+ "(?:月)" +
34
+ "(?:\\s*)" +
35
+ "(" +
36
+ "\\d{1,2}|" +
37
+ "[" +
38
+ Object.keys(constants_1.NUMBER).join("") +
39
+ "]{1,3}" +
40
+ ")?" +
41
+ "(?:\\s*)" +
42
+ "(?:日|号)?");
43
+ }
44
+ innerExtract(context, match) {
45
+ const startMoment = dayjs_1.default(context.refDate);
46
+ const result = context.createParsingResult(match.index, match[0]);
47
+ let month = parseInt(match[MONTH_GROUP]);
48
+ if (isNaN(month))
49
+ month = constants_1.zhStringToNumber(match[MONTH_GROUP]);
50
+ result.start.assign("month", month);
51
+ if (match[DAY_GROUP]) {
52
+ let day = parseInt(match[DAY_GROUP]);
53
+ if (isNaN(day))
54
+ day = constants_1.zhStringToNumber(match[DAY_GROUP]);
55
+ result.start.assign("day", day);
56
+ }
57
+ else {
58
+ result.start.imply("day", startMoment.date());
59
+ }
60
+ if (match[YEAR_GROUP]) {
61
+ let year = parseInt(match[YEAR_GROUP]);
62
+ if (isNaN(year))
63
+ year = constants_1.zhStringToYear(match[YEAR_GROUP]);
64
+ result.start.assign("year", year);
65
+ }
66
+ else {
67
+ result.start.imply("year", startMoment.year());
68
+ }
69
+ return result;
70
+ }
71
+ }
72
+ exports.default = ZHHansDateParser;
@@ -0,0 +1,6 @@
1
+ import { ParsingContext } from "../../../../chrono";
2
+ import { AbstractParserWithWordBoundaryChecking } from "../../../../common/parsers/AbstractParserWithWordBoundary";
3
+ export default class ZHHansDeadlineFormatParser 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 ZHHansDeadlineFormatParser 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 = ZHHansDeadlineFormatParser;
@@ -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 ZHHansRelationWeekdayParser extends AbstractParserWithWordBoundaryChecking {
5
+ innerPattern(): RegExp;
6
+ innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingResult;
7
+ }