mdtg 0.0.2 → 0.0.4
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.
- package/README.md +4 -4
- package/eslint.config.js +21 -0
- package/index.js +125 -83
- package/package.json +8 -2
package/README.md
CHANGED
|
@@ -15,13 +15,13 @@ npm install mdtg
|
|
|
15
15
|
### Usage
|
|
16
16
|
|
|
17
17
|
```js
|
|
18
|
-
import { MDTG } from
|
|
18
|
+
import { MDTG } from "mdtg";
|
|
19
19
|
|
|
20
20
|
const mdtg = new MDTG();
|
|
21
21
|
|
|
22
|
-
console.log(mdtg.toMDT({form: "short"}));
|
|
23
|
-
console.log(mdtg.toMDT({form: "shortened"}));
|
|
24
|
-
console.log(mdtg.toMDT({form: "long"}));
|
|
22
|
+
console.log(mdtg.toMDT({ form: "short" }));
|
|
23
|
+
console.log(mdtg.toMDT({ form: "shortened" }));
|
|
24
|
+
console.log(mdtg.toMDT({ form: "long" }));
|
|
25
25
|
|
|
26
26
|
console.log(MDTG.parse("12135530Zaug24"));
|
|
27
27
|
console.log(MDTG.parse("121355Zaug24"));
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import pluginJs from "@eslint/js";
|
|
2
|
+
|
|
3
|
+
export default [
|
|
4
|
+
pluginJs.configs.recommended,
|
|
5
|
+
{
|
|
6
|
+
rules: {
|
|
7
|
+
"no-unused-vars": [
|
|
8
|
+
"error",
|
|
9
|
+
{
|
|
10
|
+
varsIgnorePattern: "^_",
|
|
11
|
+
},
|
|
12
|
+
],
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
files: ["**/*.test.js"],
|
|
17
|
+
rules: {
|
|
18
|
+
"no-undef": "off",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
];
|
package/index.js
CHANGED
|
@@ -1,100 +1,142 @@
|
|
|
1
|
+
const normalizeNumber = (number) => String(number).padStart(2, "0");
|
|
2
|
+
|
|
1
3
|
export class MDTG {
|
|
2
|
-
|
|
3
|
-
|
|
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
|
+
];
|
|
4
19
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
20
|
+
constructor(date) {
|
|
21
|
+
this.#currentDate = date ?? new Date();
|
|
22
|
+
}
|
|
8
23
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
24
|
+
static isShortFormat(str) {
|
|
25
|
+
return /^[0-9]{6}Z$/.test(str);
|
|
26
|
+
}
|
|
12
27
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
28
|
+
static isShortenedFormat(str) {
|
|
29
|
+
return /^[0-9]{6}Z(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i.test(
|
|
30
|
+
str,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
16
33
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
34
|
+
static isLongFormat(str) {
|
|
35
|
+
return /^[0-9]{8}Z(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[0-9]{2}$/i.test(
|
|
36
|
+
str,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
20
39
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
40
|
+
#toShortMDT() {
|
|
41
|
+
const shortMDT = [
|
|
42
|
+
this.#currentDate.getDate(),
|
|
43
|
+
this.#currentDate.getHours(),
|
|
44
|
+
this.#currentDate.getMinutes(),
|
|
45
|
+
]
|
|
46
|
+
.map(normalizeNumber)
|
|
47
|
+
.concat("Z")
|
|
48
|
+
.join("");
|
|
49
|
+
if (!MDTG.isShortFormat(shortMDT)) {
|
|
50
|
+
throw new Error("There was an error while building short MDT");
|
|
29
51
|
}
|
|
52
|
+
return shortMDT;
|
|
53
|
+
}
|
|
30
54
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
55
|
+
#toLongMDT() {
|
|
56
|
+
const longMDT = [
|
|
57
|
+
this.#currentDate.getDate(),
|
|
58
|
+
this.#currentDate.getHours(),
|
|
59
|
+
this.#currentDate.getMinutes(),
|
|
60
|
+
this.#currentDate.getSeconds(),
|
|
61
|
+
]
|
|
62
|
+
.map(normalizeNumber)
|
|
63
|
+
.concat(
|
|
64
|
+
"Z",
|
|
65
|
+
MDTG.months[this.#currentDate.getMonth()],
|
|
66
|
+
normalizeNumber(this.#currentDate.getFullYear() % 2000),
|
|
67
|
+
)
|
|
68
|
+
.join("");
|
|
69
|
+
if (!MDTG.isLongFormat(longMDT)) {
|
|
70
|
+
throw new Error("There was an error while building long MDT");
|
|
42
71
|
}
|
|
72
|
+
return longMDT;
|
|
73
|
+
}
|
|
43
74
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
75
|
+
#toShortenedMDT() {
|
|
76
|
+
const shortenedMDT = [
|
|
77
|
+
this.#toShortMDT(),
|
|
78
|
+
MDTG.months[this.#currentDate.getMonth()],
|
|
79
|
+
normalizeNumber(this.#currentDate.getFullYear() % 2000),
|
|
80
|
+
].join("");
|
|
81
|
+
if (!MDTG.isShortenedFormat(shortenedMDT)) {
|
|
82
|
+
throw new Error("There was an error while building shortened MDT");
|
|
51
83
|
}
|
|
84
|
+
return shortenedMDT;
|
|
85
|
+
}
|
|
52
86
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
87
|
+
toMDT(options) {
|
|
88
|
+
if (options?.form === "short") {
|
|
89
|
+
return this.#toShortMDT();
|
|
90
|
+
} else if (options?.form === "shortened") {
|
|
91
|
+
return this.#toShortenedMDT();
|
|
92
|
+
} else {
|
|
93
|
+
return this.#toLongMDT();
|
|
61
94
|
}
|
|
95
|
+
}
|
|
62
96
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
97
|
+
static parse(str) {
|
|
98
|
+
if (MDTG.isShortFormat(str)) {
|
|
99
|
+
const day = Number.parseInt(str.slice(0, 2));
|
|
100
|
+
const hours = Number.parseInt(str.slice(2, 4));
|
|
101
|
+
const minutes = Number.parseInt(str.slice(4, 6));
|
|
102
|
+
const _timezone = str.slice(-1);
|
|
69
103
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
104
|
+
const today = new Date();
|
|
105
|
+
const date = new Date(
|
|
106
|
+
today.getFullYear(),
|
|
107
|
+
today.getMonth(),
|
|
108
|
+
day,
|
|
109
|
+
hours,
|
|
110
|
+
minutes,
|
|
111
|
+
0,
|
|
112
|
+
0,
|
|
113
|
+
);
|
|
114
|
+
return date;
|
|
115
|
+
} else if (MDTG.isShortenedFormat(str)) {
|
|
116
|
+
const day = Number.parseInt(str.slice(0, 2));
|
|
117
|
+
const hours = Number.parseInt(str.slice(2, 4));
|
|
118
|
+
const minutes = Number.parseInt(str.slice(4, 6));
|
|
119
|
+
const _timezone = str.slice(6, 7);
|
|
120
|
+
const month = MDTG.months.indexOf(str.slice(7, 10).toLowerCase());
|
|
121
|
+
const year = 2000 + Number.parseInt(str.slice(10, 12));
|
|
122
|
+
const date = new Date(year, month, day, hours, minutes, 0, 0);
|
|
123
|
+
return date;
|
|
124
|
+
} else if (MDTG.isLongFormat(str)) {
|
|
125
|
+
const day = Number.parseInt(str.slice(0, 2));
|
|
126
|
+
const hours = Number.parseInt(str.slice(2, 4));
|
|
127
|
+
const minutes = Number.parseInt(str.slice(4, 6));
|
|
128
|
+
const seconds = Number.parseInt(str.slice(6, 8));
|
|
129
|
+
const _timezone = str.slice(8, 9);
|
|
130
|
+
const month = MDTG.months.indexOf(str.slice(9, 12).toLowerCase());
|
|
131
|
+
const year = 2000 + Number.parseInt(str.slice(12, 14));
|
|
132
|
+
const date = new Date(year, month, day, hours, minutes, seconds, 0);
|
|
133
|
+
return date;
|
|
134
|
+
} else {
|
|
135
|
+
throw Error("There was an error while parsing the string " + str);
|
|
95
136
|
}
|
|
137
|
+
}
|
|
96
138
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
139
|
+
toDate() {
|
|
140
|
+
return this.#currentDate;
|
|
141
|
+
}
|
|
142
|
+
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mdtg",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
7
|
+
"format": "prettier --check .",
|
|
8
|
+
"lint": "eslint .",
|
|
7
9
|
"test": "node index.test.js"
|
|
8
10
|
},
|
|
9
11
|
"keywords": [
|
|
@@ -13,5 +15,9 @@
|
|
|
13
15
|
],
|
|
14
16
|
"author": "0xflotus",
|
|
15
17
|
"license": "MIT",
|
|
16
|
-
"description": "This package offers you methods to convert Date Objects to military date time and vice versa"
|
|
18
|
+
"description": "This package offers you methods to convert Date Objects to military date time and vice versa",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"eslint": "8.57.1",
|
|
21
|
+
"prettier": "3.5.2"
|
|
22
|
+
}
|
|
17
23
|
}
|