chrono-node 2.6.6 → 2.7.0

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 (48) hide show
  1. package/dist/cjs/calculation/mergingCalculation.js +2 -0
  2. package/dist/cjs/calculation/mergingCalculation.js.map +1 -1
  3. package/dist/cjs/common/casualReferences.js +13 -3
  4. package/dist/cjs/common/casualReferences.js.map +1 -1
  5. package/dist/cjs/locales/en/parsers/ENCasualDateParser.js +12 -6
  6. package/dist/cjs/locales/en/parsers/ENCasualDateParser.js.map +1 -1
  7. package/dist/cjs/locales/en/parsers/ENCasualTimeParser.d.ts +1 -1
  8. package/dist/cjs/locales/en/parsers/ENCasualTimeParser.js +15 -6
  9. package/dist/cjs/locales/en/parsers/ENCasualTimeParser.js.map +1 -1
  10. package/dist/cjs/locales/en/parsers/ENTimeExpressionParser.js +23 -22
  11. package/dist/cjs/locales/en/parsers/ENTimeExpressionParser.js.map +1 -1
  12. package/dist/cjs/locales/en/parsers/ENTimeUnitWithinFormatParser.js +3 -2
  13. package/dist/cjs/locales/en/parsers/ENTimeUnitWithinFormatParser.js.map +1 -1
  14. package/dist/cjs/results.d.ts +5 -0
  15. package/dist/cjs/results.js +30 -2
  16. package/dist/cjs/results.js.map +1 -1
  17. package/dist/cjs/types.d.ts +2 -0
  18. package/dist/cjs/types.js.map +1 -1
  19. package/dist/esm/calculation/mergingCalculation.js +2 -0
  20. package/dist/esm/calculation/mergingCalculation.js.map +1 -1
  21. package/dist/esm/common/casualReferences.js +13 -3
  22. package/dist/esm/common/casualReferences.js.map +1 -1
  23. package/dist/esm/locales/en/parsers/ENCasualDateParser.js +12 -6
  24. package/dist/esm/locales/en/parsers/ENCasualDateParser.js.map +1 -1
  25. package/dist/esm/locales/en/parsers/ENCasualTimeParser.d.ts +1 -1
  26. package/dist/esm/locales/en/parsers/ENCasualTimeParser.js +15 -6
  27. package/dist/esm/locales/en/parsers/ENCasualTimeParser.js.map +1 -1
  28. package/dist/esm/locales/en/parsers/ENTimeExpressionParser.js +23 -22
  29. package/dist/esm/locales/en/parsers/ENTimeExpressionParser.js.map +1 -1
  30. package/dist/esm/locales/en/parsers/ENTimeUnitWithinFormatParser.js +3 -2
  31. package/dist/esm/locales/en/parsers/ENTimeUnitWithinFormatParser.js.map +1 -1
  32. package/dist/esm/results.d.ts +5 -0
  33. package/dist/esm/results.js +30 -2
  34. package/dist/esm/results.js.map +1 -1
  35. package/dist/esm/types.d.ts +2 -0
  36. package/dist/esm/types.js.map +1 -1
  37. package/package.json +1 -1
  38. package/src/calculation/mergingCalculation.ts +2 -0
  39. package/src/common/casualReferences.ts +13 -3
  40. package/src/locales/en/parsers/ENCasualDateParser.ts +12 -7
  41. package/src/locales/en/parsers/ENCasualTimeParser.ts +15 -6
  42. package/src/locales/en/parsers/ENTimeExpressionParser.ts +24 -22
  43. package/src/locales/en/parsers/ENTimeUnitWithinFormatParser.ts +4 -3
  44. package/src/results.ts +34 -4
  45. package/src/types.ts +11 -1
  46. package/test/en/en_time_units_within.test.ts +22 -1
  47. package/test/result.test.ts +36 -3
  48. package/test/system.test.ts +37 -0
@@ -3,8 +3,9 @@ import { ParsingContext } from "../../../chrono";
3
3
  import { ParsingComponents } from "../../../results";
4
4
  import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
5
5
 
6
- const PATTERN_WITHOUT_PREFIX = new RegExp(
7
- `(?:(?:about|around|roughly|approximately|just)\\s*(?:~\\s*)?)?(${TIME_UNITS_PATTERN})(?=\\W|$)`,
6
+ const PATTERN_WITH_OPTIONAL_PREFIX = new RegExp(
7
+ `(?:(?:within|in|for)\\s*)?` +
8
+ `(?:(?:about|around|roughly|approximately|just)\\s*(?:~\\s*)?)?(${TIME_UNITS_PATTERN})(?=\\W|$)`,
8
9
  "i"
9
10
  );
10
11
 
@@ -29,7 +30,7 @@ export default class ENTimeUnitWithinFormatParser extends AbstractParserWithWord
29
30
  if (this.strictMode) {
30
31
  return PATTERN_WITH_PREFIX_STRICT;
31
32
  }
32
- return context.option.forwardDate ? PATTERN_WITHOUT_PREFIX : PATTERN_WITH_PREFIX;
33
+ return context.option.forwardDate ? PATTERN_WITH_OPTIONAL_PREFIX : PATTERN_WITH_PREFIX;
33
34
  }
34
35
 
35
36
  innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents {
package/src/results.ts CHANGED
@@ -50,6 +50,7 @@ export class ParsingComponents implements ParsedComponents {
50
50
  private knownValues: { [c in Component]?: number };
51
51
  private impliedValues: { [c in Component]?: number };
52
52
  private reference: ReferenceWithTimezone;
53
+ private _tags = new Set<string>();
53
54
 
54
55
  constructor(reference: ReferenceWithTimezone, knownComponents?: { [c in Component]?: number }) {
55
56
  this.reference = reference;
@@ -155,9 +156,11 @@ export class ParsingComponents implements ParsedComponents {
155
156
  }
156
157
 
157
158
  toString() {
158
- return `[ParsingComponents {knownValues: ${JSON.stringify(this.knownValues)}, impliedValues: ${JSON.stringify(
159
- this.impliedValues
160
- )}}, reference: ${JSON.stringify(this.reference)}]`;
159
+ return `[ParsingComponents {
160
+ tags: ${JSON.stringify(Array.from(this._tags).sort())},
161
+ knownValues: ${JSON.stringify(this.knownValues)},
162
+ impliedValues: ${JSON.stringify(this.impliedValues)}},
163
+ reference: ${JSON.stringify(this.reference)}]`;
161
164
  }
162
165
 
163
166
  dayjs() {
@@ -170,6 +173,22 @@ export class ParsingComponents implements ParsedComponents {
170
173
  return new Date(date.getTime() + timezoneAdjustment * 60000);
171
174
  }
172
175
 
176
+ addTag(tag: string): ParsingComponents {
177
+ this._tags.add(tag);
178
+ return this;
179
+ }
180
+
181
+ addTags(tags: string[] | Set<string>): ParsingComponents {
182
+ for (const tag of tags) {
183
+ this._tags.add(tag);
184
+ }
185
+ return this;
186
+ }
187
+
188
+ tags(): Set<string> {
189
+ return new Set(this._tags);
190
+ }
191
+
173
192
  private dateWithoutTimezoneAdjustment() {
174
193
  const date = new Date(
175
194
  this.get("year"),
@@ -271,7 +290,18 @@ export class ParsingResult implements ParsedResult {
271
290
  return this.start.date();
272
291
  }
273
292
 
293
+ tags(): Set<string> {
294
+ const combinedTags: Set<string> = new Set(this.start.tags());
295
+ if (this.end) {
296
+ for (const tag of this.end.tags()) {
297
+ combinedTags.add(tag);
298
+ }
299
+ }
300
+ return combinedTags;
301
+ }
302
+
274
303
  toString() {
275
- return `[ParsingResult {index: ${this.index}, text: '${this.text}', ...}]`;
304
+ const tags = Array.from(this.tags()).sort();
305
+ return `[ParsingResult {index: ${this.index}, text: '${this.text}', tags: ${JSON.stringify(tags)} ...}]`;
276
306
  }
277
307
  }
package/src/types.ts CHANGED
@@ -77,9 +77,14 @@ export interface ParsedResult {
77
77
  readonly end?: ParsedComponents;
78
78
 
79
79
  /**
80
- * Create a javascript date object (from the result.start).
80
+ * @return a javascript date object created from the `result.start`.
81
81
  */
82
82
  date(): Date;
83
+
84
+ /**
85
+ * @return debugging tags combined of the `result.start` and `result.end`.
86
+ */
87
+ tags(): Set<string>;
83
88
  }
84
89
 
85
90
  /**
@@ -105,6 +110,11 @@ export interface ParsedComponents {
105
110
  * @return a javascript date object.
106
111
  */
107
112
  date(): Date;
113
+
114
+ /**
115
+ * @return debugging tags of the parsed component.
116
+ */
117
+ tags(): Set<string>;
108
118
  }
109
119
 
110
120
  export type Component =
@@ -336,7 +336,7 @@ test("Test - Time units' certainty", () => {
336
336
  });
337
337
  });
338
338
 
339
- test("Test - Strict mode", function () {
339
+ test("Test - Strict mode", () => {
340
340
  testSingleCase(chrono, "in 2hour", new Date(2016, 10 - 1, 1, 14, 52), (result, text) => {
341
341
  expect(result.start.get("hour")).toBe(16);
342
342
  expect(result.start.get("minute")).toBe(52);
@@ -345,3 +345,24 @@ test("Test - Strict mode", function () {
345
345
  testUnexpectedResult(chrono.strict, "in 15m");
346
346
  testUnexpectedResult(chrono.strict, "within 5hr");
347
347
  });
348
+
349
+ test("Test - Forward date option", () => {
350
+ testSingleCase(chrono, "1 hour", new Date(2012, 7, 10, 12, 14), { forwardDate: true }, (result) => {
351
+ expect(result.text).toBe("1 hour");
352
+ expect(result.start).toBeDate(new Date(2012, 7, 10, 13, 14));
353
+ });
354
+
355
+ testSingleCase(chrono, "1 month", new Date(2016, 10 - 1, 1, 14, 52), { forwardDate: true }, (result, text) => {
356
+ expect(result.text).toBe("1 month");
357
+ expect(result.start.get("year")).toBe(2016);
358
+ expect(result.start.get("month")).toBe(11);
359
+ expect(result.start.get("day")).toBe(1);
360
+ });
361
+
362
+ testSingleCase(chrono, "in 1 month", new Date(2016, 10 - 1, 1, 14, 52), { forwardDate: true }, (result, text) => {
363
+ expect(result.text).toBe("in 1 month");
364
+ expect(result.start.get("year")).toBe(2016);
365
+ expect(result.start.get("month")).toBe(11);
366
+ expect(result.start.get("day")).toBe(1);
367
+ });
368
+ });
@@ -1,6 +1,6 @@
1
- import { ParsingComponents, ReferenceWithTimezone } from "../src/results";
1
+ import { ParsingComponents, ParsingResult, ReferenceWithTimezone } from "../src/results";
2
2
 
3
- test("Test - Create & manipulate date results", () => {
3
+ test("Test - Create & manipulate parsing components", () => {
4
4
  const reference = new ReferenceWithTimezone(new Date());
5
5
  const components = new ParsingComponents(reference, { year: 2014, month: 11, day: 24 });
6
6
 
@@ -8,6 +8,7 @@ test("Test - Create & manipulate date results", () => {
8
8
  expect(components.get("month")).toBe(11);
9
9
  expect(components.get("day")).toBe(24);
10
10
  expect(components.date()).toBeDefined();
11
+ expect(components.tags().size).toBe(0);
11
12
 
12
13
  // null
13
14
  expect(components.get("weekday")).toBeNull();
@@ -23,13 +24,45 @@ test("Test - Create & manipulate date results", () => {
23
24
  expect(components.get("weekday")).toBe(2);
24
25
  expect(components.isCertain("weekday")).toBe(true);
25
26
 
26
- // "imply" doesn't overrides "assign"
27
+ // "imply" doesn't override "assign"
27
28
  components.imply("year", 2013);
28
29
  expect(components.get("year")).toBe(2014);
29
30
 
30
31
  // "assign" overrides "assign"
31
32
  components.assign("year", 2013);
32
33
  expect(components.get("year")).toBe(2013);
34
+
35
+ components.addTag("custom/testing_component_tag");
36
+ expect(components.tags().size).toBe(1);
37
+ expect(components.tags()).toContain("custom/testing_component_tag");
38
+ expect(components.toString()).toContain("custom/testing_component_tag");
39
+ });
40
+
41
+ test("Test - Create & manipulate parsing results", () => {
42
+ const reference = new ReferenceWithTimezone(new Date());
43
+ const text = "1 - 2 hour later";
44
+
45
+ const startComponents = ParsingComponents.createRelativeFromReference(reference, { "hour": 1 }).addTag(
46
+ "custom/testing_start_component_tag"
47
+ );
48
+
49
+ const endComponents = ParsingComponents.createRelativeFromReference(reference, { "hour": 2 }).addTag(
50
+ "custom/testing_end_component_tag"
51
+ );
52
+
53
+ const result = new ParsingResult(reference, 0, text, startComponents, endComponents);
54
+
55
+ // The result's date() should be the same as the start components' date()
56
+ expect(result.date()).toStrictEqual(startComponents.date());
57
+
58
+ // The result's tags should include both the start and end components' tags
59
+ expect(result.tags()).toContain("custom/testing_start_component_tag");
60
+ expect(result.tags()).toContain("custom/testing_end_component_tag");
61
+
62
+ // The result's toString() should include the text and tags
63
+ expect(result.toString()).toContain(text);
64
+ expect(result.toString()).toContain("custom/testing_start_component_tag");
65
+ expect(result.toString()).toContain("custom/testing_end_component_tag");
33
66
  });
34
67
 
35
68
  test("Test - Calendar checking with implied components", () => {
@@ -5,6 +5,7 @@ import UnlikelyFormatFilter from "../src/common/refiners/UnlikelyFormatFilter";
5
5
  import SlashDateFormatParser from "../src/common/parsers/SlashDateFormatParser";
6
6
  import ENWeekdayParser from "../src/locales/en/parsers/ENWeekdayParser";
7
7
  import ENTimeUnitCasualRelativeFormatParser from "../src/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser";
8
+ import { ParsingComponents } from "../src/";
8
9
 
9
10
  //-------------------------------------
10
11
 
@@ -124,6 +125,42 @@ test("Test - Add custom refiner example", () => {
124
125
  });
125
126
  });
126
127
 
128
+ test("Test - Add custom parser with tags example", () => {
129
+ const custom = chrono.casual.clone();
130
+ custom.parsers.push({
131
+ pattern: () => {
132
+ return /\bChristmas\b/i;
133
+ },
134
+ extract: (context) => {
135
+ return context
136
+ .createParsingComponents({
137
+ day: 25,
138
+ month: 12,
139
+ })
140
+ .addTag("parser/ChristmasDayParser");
141
+ },
142
+ });
143
+
144
+ testSingleCase(custom, "Doing something tomorrow", (result) => {
145
+ expect(result.text).toBe("tomorrow");
146
+ expect(result.tags()).toContain("parser/ENCasualDateParser");
147
+ });
148
+
149
+ testSingleCase(custom, "I'll arrive at 2.30AM on Christmas", (result) => {
150
+ expect(result.text).toBe("at 2.30AM on Christmas");
151
+ expect(result.tags()).toContain("parser/ChristmasDayParser");
152
+ expect(result.tags()).toContain("parser/ENTimeExpressionParser");
153
+ });
154
+
155
+ testSingleCase(custom, "I'll arrive at Christmas night", (result) => {
156
+ expect(result.text).toBe("Christmas night");
157
+ expect(result.tags()).toContain("parser/ChristmasDayParser");
158
+ expect(result.tags()).toContain("parser/ENCasualTimeParser");
159
+ });
160
+
161
+ // TODO: Check if the merge date range combine tags correctly
162
+ });
163
+
127
164
  test("Test - Remove a parser example", () => {
128
165
  const custom = chrono.en.strict.clone();
129
166
  custom.parsers = custom.parsers.filter((r) => !(r instanceof SlashDateFormatParser));