mdtg 1.3.1 → 2.0.1

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 (2) hide show
  1. package/index.js +245 -240
  2. package/package.json +27 -26
package/index.js CHANGED
@@ -1,244 +1,249 @@
1
1
  const normalizeNumber = (number) => String(number).padStart(2, "0");
2
+ const parseInteger = (input) => Number.parseInt(input, 10);
2
3
 
3
4
  export class MDTG {
4
- #currentDate = null;
5
- static months = [
6
- "jan",
7
- "feb",
8
- "mar",
9
- "apr",
10
- "may",
11
- "jun",
12
- "jul",
13
- "aug",
14
- "sep",
15
- "oct",
16
- "nov",
17
- "dec",
18
- ];
19
-
20
- static offset = {
21
- Y: 12,
22
- X: 11,
23
- W: 10,
24
- V: 9,
25
- U: 8,
26
- T: 7,
27
- S: 6,
28
- R: 5,
29
- Q: 4,
30
- P: 3,
31
- O: 2,
32
- N: 1,
33
- Z: 0,
34
- A: -1,
35
- B: -2,
36
- C: -3,
37
- D: -4,
38
- E: -5,
39
- F: -6,
40
- G: -7,
41
- H: -8,
42
- I: -9,
43
- J: -10,
44
- K: -11,
45
- L: -12,
46
- M: -13,
47
- };
48
-
49
- constructor(date) {
50
- const now = new Date();
51
- this.#currentDate =
52
- date ??
53
- new Date(
54
- Date.UTC(
55
- now.getUTCFullYear(),
56
- now.getUTCMonth(),
57
- now.getUTCDate(),
58
- now.getUTCHours(),
59
- now.getUTCMinutes(),
60
- now.getUTCSeconds(),
61
- now.getUTCMilliseconds(),
62
- ),
63
- );
64
- }
65
-
66
- static isShortFormat(str) {
67
- return /^[0-9]{6}[A-Z]$/.test(str);
68
- }
69
-
70
- static isShortenedFormat(str) {
71
- return /^[0-9]{6}[A-Z](jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i.test(
72
- str,
73
- );
74
- }
75
-
76
- static isLongFormat(str) {
77
- return /^[0-9]{8}[A-Z](jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i.test(
78
- str,
79
- );
80
- }
81
-
82
- #toShortMDT(timezone = "Z") {
83
- if (!/^[A-Z]$/.test(timezone.toUpperCase())) {
84
- timezone = "Z";
85
- }
86
- const newDate = new Date(
87
- Date.UTC(
88
- this.#currentDate.getUTCFullYear(),
89
- this.#currentDate.getUTCMonth(),
90
- this.#currentDate.getUTCDate(),
91
- this.#currentDate.getUTCHours() - MDTG.offset[timezone.toUpperCase()],
92
- this.#currentDate.getUTCMinutes(),
93
- ),
94
- );
95
- const shortMDT = [
96
- newDate.getUTCDate(),
97
- newDate.getUTCHours(),
98
- newDate.getUTCMinutes(),
99
- ]
100
- .map(normalizeNumber)
101
- .concat(timezone.toUpperCase())
102
- .join("");
103
- if (!MDTG.isShortFormat(shortMDT)) {
104
- throw new Error("There was an error while building short MDT");
105
- }
106
- return shortMDT;
107
- }
108
-
109
- #toLongMDT(timezone = "Z") {
110
- if (!/^[A-Z]$/.test(timezone.toUpperCase())) {
111
- timezone = "Z";
112
- }
113
- const newDate = new Date(
114
- Date.UTC(
115
- this.#currentDate.getUTCFullYear(),
116
- this.#currentDate.getUTCMonth(),
117
- this.#currentDate.getUTCDate(),
118
- this.#currentDate.getUTCHours() - MDTG.offset[timezone.toUpperCase()],
119
- this.#currentDate.getUTCMinutes(),
120
- this.#currentDate.getUTCSeconds(),
121
- ),
122
- );
123
- const longMDT = [
124
- newDate.getUTCDate(),
125
- newDate.getUTCHours(),
126
- newDate.getUTCMinutes(),
127
- newDate.getUTCSeconds(),
128
- ]
129
- .map(normalizeNumber)
130
- .concat(
131
- timezone.toUpperCase(),
132
- MDTG.months[newDate.getUTCMonth()],
133
- normalizeNumber(newDate.getUTCFullYear() % 2000),
134
- )
135
- .join("");
136
- if (!MDTG.isLongFormat(longMDT)) {
137
- throw new Error("There was an error while building long MDT");
138
- }
139
- return longMDT;
140
- }
141
-
142
- #toShortenedMDT(timezone = "Z") {
143
- if (!/^[A-Z]$/.test(timezone.toUpperCase())) {
144
- timezone = "Z";
145
- }
146
- const newDate = new Date(
147
- Date.UTC(
148
- this.#currentDate.getUTCFullYear(),
149
- this.#currentDate.getUTCMonth(),
150
- this.#currentDate.getUTCDate(),
151
- this.#currentDate.getUTCHours() - MDTG.offset[timezone.toUpperCase()],
152
- this.#currentDate.getUTCMinutes(),
153
- this.#currentDate.getUTCSeconds(),
154
- ),
155
- );
156
- const shortenedMDT = [
157
- this.#toShortMDT(timezone.toUpperCase()),
158
- MDTG.months[newDate.getUTCMonth()],
159
- normalizeNumber(newDate.getUTCFullYear() % 2000),
160
- ].join("");
161
- if (!MDTG.isShortenedFormat(shortenedMDT)) {
162
- throw new Error("There was an error while building shortened MDT");
163
- }
164
- return shortenedMDT;
165
- }
166
-
167
- toMDT(options) {
168
- if (options?.form === "short") {
169
- return this.#toShortMDT(options?.timezone);
170
- } else if (options?.form === "shortened") {
171
- return this.#toShortenedMDT(options?.timezone);
172
- } else {
173
- return this.#toLongMDT(options?.timezone);
174
- }
175
- }
176
-
177
- static parse(str) {
178
- if (MDTG.isShortFormat(str)) {
179
- const day = Number.parseInt(str.slice(0, 2), 10);
180
- const hours = Number.parseInt(str.slice(2, 4), 10);
181
- const minutes = Number.parseInt(str.slice(4, 6), 10);
182
- const timezoneOffset = str.slice(-1).toUpperCase();
183
-
184
- const today = new Date();
185
- const date = new Date(
186
- Date.UTC(
187
- today.getUTCFullYear(),
188
- today.getUTCMonth(),
189
- day,
190
- hours + MDTG.offset[timezoneOffset],
191
- minutes,
192
- 0,
193
- 0,
194
- ),
195
- );
196
- return date;
197
- } else if (MDTG.isShortenedFormat(str)) {
198
- const day = Number.parseInt(str.slice(0, 2), 10);
199
- const hours = Number.parseInt(str.slice(2, 4), 10);
200
- const minutes = Number.parseInt(str.slice(4, 6), 10);
201
- const timezoneOffset = str.slice(6, 7).toUpperCase();
202
- const month = MDTG.months.indexOf(str.slice(7, 10).toLowerCase());
203
- const year = 2000 + Number.parseInt(str.slice(10, 12), 10);
204
- const date = new Date(
205
- Date.UTC(
206
- year,
207
- month,
208
- day,
209
- hours + MDTG.offset[timezoneOffset],
210
- minutes,
211
- 0,
212
- 0,
213
- ),
214
- );
215
- return date;
216
- } else if (MDTG.isLongFormat(str)) {
217
- const day = Number.parseInt(str.slice(0, 2), 10);
218
- const hours = Number.parseInt(str.slice(2, 4), 10);
219
- const minutes = Number.parseInt(str.slice(4, 6), 10);
220
- const seconds = Number.parseInt(str.slice(6, 8), 10);
221
- const timezoneOffset = str.slice(8, 9).toUpperCase();
222
- const month = MDTG.months.indexOf(str.slice(9, 12).toLowerCase());
223
- const year = 2000 + Number.parseInt(str.slice(12, 14), 10);
224
- const date = new Date(
225
- Date.UTC(
226
- year,
227
- month,
228
- day,
229
- hours + MDTG.offset[timezoneOffset],
230
- minutes,
231
- seconds,
232
- 0,
233
- ),
234
- );
235
- return date;
236
- } else {
237
- throw Error(`There was an error while parsing the string "${str}"`);
238
- }
239
- }
240
-
241
- toDate() {
242
- return this.#currentDate;
243
- }
5
+ #currentDate;
6
+
7
+ static FORM = {
8
+ SHORT: "short",
9
+ SHORTENED: "shortened",
10
+ LONG: "long",
11
+ };
12
+
13
+ static #months = [
14
+ "jan",
15
+ "feb",
16
+ "mar",
17
+ "apr",
18
+ "may",
19
+ "jun",
20
+ "jul",
21
+ "aug",
22
+ "sep",
23
+ "oct",
24
+ "nov",
25
+ "dec",
26
+ ];
27
+
28
+ static #shortFormatRegex = /^[0-9]{6}[A-Z]$/;
29
+ static #shortenedFormatRegex =
30
+ /^[0-9]{6}[A-Z](jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i;
31
+ static #longFormatRegex =
32
+ /^[0-9]{8}[A-Z](jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i;
33
+
34
+ static #offset = {
35
+ Y: 12,
36
+ X: 11,
37
+ W: 10,
38
+ V: 9,
39
+ U: 8,
40
+ T: 7,
41
+ S: 6,
42
+ R: 5,
43
+ Q: 4,
44
+ P: 3,
45
+ O: 2,
46
+ N: 1,
47
+ Z: 0,
48
+ A: -1,
49
+ B: -2,
50
+ C: -3,
51
+ D: -4,
52
+ E: -5,
53
+ F: -6,
54
+ G: -7,
55
+ H: -8,
56
+ I: -9,
57
+ J: -10,
58
+ K: -11,
59
+ L: -12,
60
+ M: -13,
61
+ };
62
+
63
+ constructor(date) {
64
+ if (date && !(date instanceof Date)) {
65
+ throw new TypeError("Expected Date object");
66
+ }
67
+
68
+ const now = new Date();
69
+
70
+ this.#currentDate =
71
+ date ??
72
+ new Date(
73
+ Date.UTC(
74
+ now.getUTCFullYear(),
75
+ now.getUTCMonth(),
76
+ now.getUTCDate(),
77
+ now.getUTCHours(),
78
+ now.getUTCMinutes(),
79
+ now.getUTCSeconds(),
80
+ now.getUTCMilliseconds(),
81
+ ),
82
+ );
83
+ }
84
+
85
+ static #validateTimezone(tz) {
86
+ const key = tz?.toUpperCase();
87
+ return key in MDTG.#offset ? key : "Z";
88
+ }
89
+
90
+ #createDateWithOffset(offset) {
91
+ const d = this.#currentDate;
92
+
93
+ return new Date(
94
+ Date.UTC(
95
+ d.getUTCFullYear(),
96
+ d.getUTCMonth(),
97
+ d.getUTCDate(),
98
+ d.getUTCHours() - offset,
99
+ d.getUTCMinutes(),
100
+ d.getUTCSeconds(),
101
+ ),
102
+ );
103
+ }
104
+
105
+ static isShortFormat(str) {
106
+ return MDTG.#shortFormatRegex.test(str);
107
+ }
108
+
109
+ static isShortenedFormat(str) {
110
+ return MDTG.#shortenedFormatRegex.test(str);
111
+ }
112
+
113
+ static isLongFormat(str) {
114
+ return MDTG.#longFormatRegex.test(str);
115
+ }
116
+
117
+ #toShortMDT(timezone = "Z") {
118
+ const tz = MDTG.#validateTimezone(timezone);
119
+ const date = this.#createDateWithOffset(MDTG.#offset[tz]);
120
+
121
+ const result = [date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes()]
122
+ .map(normalizeNumber)
123
+ .concat(tz)
124
+ .join("");
125
+
126
+ if (!MDTG.isShortFormat(result)) {
127
+ throw new Error("Failed to build short MDTG");
128
+ }
129
+
130
+ return result;
131
+ }
132
+
133
+ #toLongMDT(timezone = "Z") {
134
+ const tz = MDTG.#validateTimezone(timezone);
135
+ const date = this.#createDateWithOffset(MDTG.#offset[tz]);
136
+
137
+ const result = [
138
+ date.getUTCDate(),
139
+ date.getUTCHours(),
140
+ date.getUTCMinutes(),
141
+ date.getUTCSeconds(),
142
+ ]
143
+ .map(normalizeNumber)
144
+ .concat(
145
+ tz,
146
+ MDTG.#months[date.getUTCMonth()],
147
+ normalizeNumber(date.getUTCFullYear() % 100),
148
+ )
149
+ .join("");
150
+
151
+ if (!MDTG.isLongFormat(result)) {
152
+ throw new Error("Failed to build long MDTG");
153
+ }
154
+
155
+ return result;
156
+ }
157
+
158
+ #toShortenedMDT(timezone = "Z") {
159
+ const tz = MDTG.#validateTimezone(timezone);
160
+ const date = this.#createDateWithOffset(MDTG.#offset[tz]);
161
+
162
+ const result = [
163
+ this.#toShortMDT(tz),
164
+ MDTG.#months[date.getUTCMonth()],
165
+ normalizeNumber(date.getUTCFullYear() % 100),
166
+ ].join("");
167
+
168
+ if (!MDTG.isShortenedFormat(result)) {
169
+ throw new Error("Failed to build shortened MDTG");
170
+ }
171
+
172
+ return result;
173
+ }
174
+
175
+ toMDT({ form = MDTG.FORM.LONG, timezone = "Z" } = {}) {
176
+ switch (form) {
177
+ case MDTG.FORM.SHORT:
178
+ return this.#toShortMDT(timezone);
179
+ case MDTG.FORM.SHORTENED:
180
+ return this.#toShortenedMDT(timezone);
181
+ default:
182
+ return this.#toLongMDT(timezone);
183
+ }
184
+ }
185
+
186
+ static #parseShort(str) {
187
+ const day = parseInteger(str.slice(0, 2));
188
+ const hours = parseInteger(str.slice(2, 4));
189
+ const minutes = parseInteger(str.slice(4, 6));
190
+ const tz = str.slice(6, 7).toUpperCase();
191
+
192
+ const today = new Date();
193
+
194
+ return new Date(
195
+ Date.UTC(
196
+ today.getUTCFullYear(),
197
+ today.getUTCMonth(),
198
+ day,
199
+ hours + MDTG.#offset[tz],
200
+ minutes,
201
+ ),
202
+ );
203
+ }
204
+
205
+ static #parseShortened(str) {
206
+ const day = parseInteger(str.slice(0, 2));
207
+ const hours = parseInteger(str.slice(2, 4));
208
+ const minutes = parseInteger(str.slice(4, 6));
209
+ const tz = str.slice(6, 7).toUpperCase();
210
+
211
+ const month = MDTG.#months.indexOf(str.slice(7, 10).toLowerCase());
212
+ if (month === -1) throw new Error("Invalid month");
213
+
214
+ const year = 2000 + parseInteger(str.slice(10, 12));
215
+
216
+ return new Date(
217
+ Date.UTC(year, month, day, hours + MDTG.#offset[tz], minutes),
218
+ );
219
+ }
220
+
221
+ static #parseLong(str) {
222
+ const day = parseInteger(str.slice(0, 2));
223
+ const hours = parseInteger(str.slice(2, 4));
224
+ const minutes = parseInteger(str.slice(4, 6));
225
+ const seconds = parseInteger(str.slice(6, 8));
226
+ const tz = str.slice(8, 9).toUpperCase();
227
+
228
+ const month = MDTG.#months.indexOf(str.slice(9, 12).toLowerCase());
229
+ if (month === -1) throw new Error("Invalid month");
230
+
231
+ const year = 2000 + parseInteger(str.slice(12, 14));
232
+
233
+ return new Date(
234
+ Date.UTC(year, month, day, hours + MDTG.#offset[tz], minutes, seconds),
235
+ );
236
+ }
237
+
238
+ static parse(str) {
239
+ if (MDTG.isShortFormat(str)) return MDTG.#parseShort(str);
240
+ if (MDTG.isShortenedFormat(str)) return MDTG.#parseShortened(str);
241
+ if (MDTG.isLongFormat(str)) return MDTG.#parseLong(str);
242
+
243
+ throw new Error(`Invalid MDTG string "${str}"`);
244
+ }
245
+
246
+ toDate() {
247
+ return new Date(this.#currentDate);
248
+ }
244
249
  }
package/package.json CHANGED
@@ -1,28 +1,29 @@
1
1
  {
2
- "name": "mdtg",
3
- "version": "1.3.1",
4
- "main": "index.js",
5
- "type": "module",
6
- "engines": {
7
- "node": ">=18.0.0",
8
- "npm": ">=8.6.0"
9
- },
10
- "scripts": {
11
- "format": "biome format .",
12
- "prelint": "npm run format",
13
- "lint": "biome lint .",
14
- "test": "vitest run"
15
- },
16
- "keywords": [
17
- "military",
18
- "date",
19
- "time"
20
- ],
21
- "author": "0xflotus",
22
- "license": "MIT",
23
- "description": "This package offers you methods to convert Date Objects to military date time and vice versa",
24
- "devDependencies": {
25
- "@biomejs/biome": "2.3.13",
26
- "vitest": "4.0.18"
27
- }
2
+ "name": "mdtg",
3
+ "version": "2.0.1",
4
+ "main": "index.js",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=24.0.0"
8
+ },
9
+ "scripts": {
10
+ "coverage": "vitest run --coverage",
11
+ "format": "biome format .",
12
+ "prelint": "npm run format",
13
+ "lint": "biome lint .",
14
+ "test": "vitest run"
15
+ },
16
+ "keywords": [
17
+ "military",
18
+ "date",
19
+ "time"
20
+ ],
21
+ "author": "0xflotus",
22
+ "license": "MIT",
23
+ "description": "This package offers you methods to convert Date Objects to military date time and vice versa",
24
+ "devDependencies": {
25
+ "@biomejs/biome": "2.4.7",
26
+ "@vitest/coverage-v8": "4.1.0",
27
+ "vitest": "4.1.0"
28
+ }
28
29
  }