UNPKG

4.14 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ParsingContext = exports.Chrono = void 0;
4const results_1 = require("./results");
5const en_1 = require("./locales/en");
6class Chrono {
7 constructor(configuration) {
8 configuration = configuration || en_1.createCasualConfiguration();
9 this.parsers = [...configuration.parsers];
10 this.refiners = [...configuration.refiners];
11 }
12 clone() {
13 return new Chrono({
14 parsers: [...this.parsers],
15 refiners: [...this.refiners],
16 });
17 }
18 parseDate(text, referenceDate, option) {
19 const results = this.parse(text, referenceDate, option);
20 return results.length > 0 ? results[0].start.date() : null;
21 }
22 parse(text, referenceDate, option) {
23 const context = new ParsingContext(text, referenceDate, option);
24 let results = [];
25 this.parsers.forEach((parser) => {
26 const parsedResults = Chrono.executeParser(context, parser);
27 results = results.concat(parsedResults);
28 });
29 results.sort((a, b) => {
30 return a.index - b.index;
31 });
32 this.refiners.forEach(function (refiner) {
33 results = refiner.refine(context, results);
34 });
35 return results;
36 }
37 static executeParser(context, parser) {
38 const results = [];
39 const pattern = parser.pattern(context);
40 const originalText = context.text;
41 let remainingText = context.text;
42 let match = pattern.exec(remainingText);
43 while (match) {
44 const index = match.index + originalText.length - remainingText.length;
45 match.index = index;
46 const result = parser.extract(context, match);
47 if (!result) {
48 remainingText = originalText.substring(match.index + 1);
49 match = pattern.exec(remainingText);
50 continue;
51 }
52 let parsedResult = null;
53 if (result instanceof results_1.ParsingResult) {
54 parsedResult = result;
55 }
56 else if (result instanceof results_1.ParsingComponents) {
57 parsedResult = context.createParsingResult(match.index, match[0]);
58 parsedResult.start = result;
59 }
60 else {
61 parsedResult = context.createParsingResult(match.index, match[0], result);
62 }
63 context.debug(() => console.log(`${parser.constructor.name} extracted result ${parsedResult}`));
64 results.push(parsedResult);
65 remainingText = originalText.substring(index + parsedResult.text.length);
66 match = pattern.exec(remainingText);
67 }
68 return results;
69 }
70}
71exports.Chrono = Chrono;
72class ParsingContext {
73 constructor(text, refDate, option) {
74 this.text = text;
75 this.reference = new results_1.ReferenceWithTimezone(refDate);
76 this.option = option !== null && option !== void 0 ? option : {};
77 this.refDate = this.reference.instant;
78 }
79 createParsingComponents(components) {
80 if (components instanceof results_1.ParsingComponents) {
81 return components;
82 }
83 return new results_1.ParsingComponents(this.reference, components);
84 }
85 createParsingResult(index, textOrEndIndex, startComponents, endComponents) {
86 const text = typeof textOrEndIndex === "string" ? textOrEndIndex : this.text.substring(index, textOrEndIndex);
87 const start = startComponents ? this.createParsingComponents(startComponents) : null;
88 const end = endComponents ? this.createParsingComponents(endComponents) : null;
89 return new results_1.ParsingResult(this.reference, index, text, start, end);
90 }
91 debug(block) {
92 if (this.option.debug) {
93 if (this.option.debug instanceof Function) {
94 this.option.debug(block);
95 }
96 else {
97 const handler = this.option.debug;
98 handler.debug(block);
99 }
100 }
101 }
102}
103exports.ParsingContext = ParsingContext;