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.ParsingResult = exports.ParsingComponents = exports.ReferenceWithTimezone = void 0;
|
7 | const quarterOfYear_1 = __importDefault(require("dayjs/plugin/quarterOfYear"));
|
8 | const dayjs_1 = __importDefault(require("dayjs"));
|
9 | const dayjs_2 = require("./utils/dayjs");
|
10 | const timezone_1 = require("./timezone");
|
11 | dayjs_1.default.extend(quarterOfYear_1.default);
|
12 | class ReferenceWithTimezone {
|
13 | constructor(input) {
|
14 | var _a, _b;
|
15 | input = input !== null && input !== void 0 ? input : new Date();
|
16 | if (input instanceof Date) {
|
17 | this.instant = input;
|
18 | this.timezoneOffset = -input.getTimezoneOffset();
|
19 | }
|
20 | else {
|
21 | this.instant = (_a = input.instant) !== null && _a !== void 0 ? _a : new Date();
|
22 | this.timezoneOffset = timezone_1.toTimezoneOffset((_b = input.timezone) !== null && _b !== void 0 ? _b : -this.instant.getTimezoneOffset());
|
23 | }
|
24 | }
|
25 | }
|
26 | exports.ReferenceWithTimezone = ReferenceWithTimezone;
|
27 | class ParsingComponents {
|
28 | constructor(reference, knownComponents) {
|
29 | this.reference = reference;
|
30 | this.knownValues = {};
|
31 | this.impliedValues = {};
|
32 | if (knownComponents) {
|
33 | for (const key in knownComponents) {
|
34 | this.knownValues[key] = knownComponents[key];
|
35 | }
|
36 | }
|
37 | const refDayJs = dayjs_1.default(reference.instant);
|
38 | this.imply("day", refDayJs.date());
|
39 | this.imply("month", refDayJs.month() + 1);
|
40 | this.imply("year", refDayJs.year());
|
41 | this.imply("hour", 12);
|
42 | this.imply("minute", 0);
|
43 | this.imply("second", 0);
|
44 | this.imply("millisecond", 0);
|
45 | }
|
46 | get(component) {
|
47 | if (component in this.knownValues) {
|
48 | return this.knownValues[component];
|
49 | }
|
50 | if (component in this.impliedValues) {
|
51 | return this.impliedValues[component];
|
52 | }
|
53 | return null;
|
54 | }
|
55 | isCertain(component) {
|
56 | return component in this.knownValues;
|
57 | }
|
58 | getCertainComponents() {
|
59 | return Object.keys(this.knownValues);
|
60 | }
|
61 | imply(component, value) {
|
62 | if (component in this.knownValues) {
|
63 | return this;
|
64 | }
|
65 | this.impliedValues[component] = value;
|
66 | return this;
|
67 | }
|
68 | assign(component, value) {
|
69 | this.knownValues[component] = value;
|
70 | delete this.impliedValues[component];
|
71 | return this;
|
72 | }
|
73 | delete(component) {
|
74 | delete this.knownValues[component];
|
75 | delete this.impliedValues[component];
|
76 | }
|
77 | clone() {
|
78 | const component = new ParsingComponents(this.reference);
|
79 | component.knownValues = {};
|
80 | component.impliedValues = {};
|
81 | for (const key in this.knownValues) {
|
82 | component.knownValues[key] = this.knownValues[key];
|
83 | }
|
84 | for (const key in this.impliedValues) {
|
85 | component.impliedValues[key] = this.impliedValues[key];
|
86 | }
|
87 | return component;
|
88 | }
|
89 | isOnlyDate() {
|
90 | return !this.isCertain("hour") && !this.isCertain("minute") && !this.isCertain("second");
|
91 | }
|
92 | isOnlyTime() {
|
93 | return !this.isCertain("weekday") && !this.isCertain("day") && !this.isCertain("month");
|
94 | }
|
95 | isOnlyWeekdayComponent() {
|
96 | return this.isCertain("weekday") && !this.isCertain("day") && !this.isCertain("month");
|
97 | }
|
98 | isOnlyDayMonthComponent() {
|
99 | return this.isCertain("day") && this.isCertain("month") && !this.isCertain("year");
|
100 | }
|
101 | isValidDate() {
|
102 | const date = this.dateWithoutTimezoneAdjustment();
|
103 | if (date.getFullYear() !== this.get("year"))
|
104 | return false;
|
105 | if (date.getMonth() !== this.get("month") - 1)
|
106 | return false;
|
107 | if (date.getDate() !== this.get("day"))
|
108 | return false;
|
109 | if (this.get("hour") != null && date.getHours() != this.get("hour"))
|
110 | return false;
|
111 | if (this.get("minute") != null && date.getMinutes() != this.get("minute"))
|
112 | return false;
|
113 | return true;
|
114 | }
|
115 | toString() {
|
116 | return `[ParsingComponents {knownValues: ${JSON.stringify(this.knownValues)}, impliedValues: ${JSON.stringify(this.impliedValues)}}]`;
|
117 | }
|
118 | dayjs() {
|
119 | return dayjs_1.default(this.date());
|
120 | }
|
121 | date() {
|
122 | const date = this.dateWithoutTimezoneAdjustment();
|
123 | return new Date(date.getTime() + this.getSystemTimezoneAdjustmentMinute() * 60000);
|
124 | }
|
125 | dateWithoutTimezoneAdjustment() {
|
126 | const date = new Date(this.get("year"), this.get("month") - 1, this.get("day"), this.get("hour"), this.get("minute"), this.get("second"), this.get("millisecond"));
|
127 | date.setFullYear(this.get("year"));
|
128 | return date;
|
129 | }
|
130 | getSystemTimezoneAdjustmentMinute() {
|
131 | var _a;
|
132 | const currentTimezoneOffset = -new Date().getTimezoneOffset();
|
133 | const targetTimezoneOffset = (_a = this.get("timezoneOffset")) !== null && _a !== void 0 ? _a : this.reference.timezoneOffset;
|
134 | return currentTimezoneOffset - targetTimezoneOffset;
|
135 | }
|
136 | static createRelativeFromRefInstant(refInstant, fragments) {
|
137 | let date = dayjs_1.default(refInstant);
|
138 | for (const key in fragments) {
|
139 | date = date.add(fragments[key], key);
|
140 | }
|
141 | const reference = new ReferenceWithTimezone(refInstant);
|
142 | const components = new ParsingComponents(reference);
|
143 | if (fragments["hour"] || fragments["minute"] || fragments["second"]) {
|
144 | dayjs_2.assignSimilarTime(components, date);
|
145 | dayjs_2.assignSimilarDate(components, date);
|
146 | components.assign("timezoneOffset", -refInstant.getTimezoneOffset());
|
147 | }
|
148 | else {
|
149 | dayjs_2.implySimilarTime(components, date);
|
150 | components.imply("timezoneOffset", -refInstant.getTimezoneOffset());
|
151 | if (fragments["d"]) {
|
152 | components.assign("day", date.date());
|
153 | components.assign("month", date.month() + 1);
|
154 | components.assign("year", date.year());
|
155 | }
|
156 | else {
|
157 | if (fragments["week"]) {
|
158 | components.imply("weekday", date.day());
|
159 | }
|
160 | components.imply("day", date.date());
|
161 | if (fragments["month"]) {
|
162 | components.assign("month", date.month() + 1);
|
163 | components.assign("year", date.year());
|
164 | }
|
165 | else {
|
166 | components.imply("month", date.month() + 1);
|
167 | if (fragments["year"]) {
|
168 | components.assign("year", date.year());
|
169 | }
|
170 | else {
|
171 | components.imply("year", date.year());
|
172 | }
|
173 | }
|
174 | }
|
175 | }
|
176 | return components;
|
177 | }
|
178 | }
|
179 | exports.ParsingComponents = ParsingComponents;
|
180 | class ParsingResult {
|
181 | constructor(reference, index, text, start, end) {
|
182 | this.reference = reference;
|
183 | this.refDate = reference.instant;
|
184 | this.index = index;
|
185 | this.text = text;
|
186 | this.start = start || new ParsingComponents(reference);
|
187 | this.end = end;
|
188 | }
|
189 | clone() {
|
190 | const result = new ParsingResult(this.reference, this.index, this.text);
|
191 | result.start = this.start ? this.start.clone() : null;
|
192 | result.end = this.end ? this.end.clone() : null;
|
193 | return result;
|
194 | }
|
195 | date() {
|
196 | return this.start.date();
|
197 | }
|
198 | toString() {
|
199 | return `[ParsingResult {index: ${this.index}, text: '${this.text}', ...}]`;
|
200 | }
|
201 | }
|
202 | exports.ParsingResult = ParsingResult;
|