dtg-js 1.1.2 → 1.2.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.
- package/index.js +38 -29
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,38 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
|
8
|
-
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'
|
|
9
|
-
);
|
|
1
|
+
const TABLE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx";
|
|
2
|
+
|
|
3
|
+
const LOOKUP = new Uint8Array(128);
|
|
4
|
+
for (let i = 0; i < TABLE.length; i++) {
|
|
5
|
+
LOOKUP[TABLE.charCodeAt(i)] = i;
|
|
6
|
+
}
|
|
10
7
|
|
|
11
8
|
export const toJSDate = (base60) => {
|
|
12
|
-
if (
|
|
13
|
-
throw new Error("
|
|
9
|
+
if (base60.length !== 7) {
|
|
10
|
+
throw new Error("Invalid base60 length");
|
|
14
11
|
}
|
|
15
12
|
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
const v = new Array(7);
|
|
14
|
+
|
|
15
|
+
for (let i = 0; i < 7; i++) {
|
|
16
|
+
const code = base60.charCodeAt(i);
|
|
17
|
+
const val = LOOKUP[code];
|
|
18
|
+
|
|
19
|
+
if (val === 0 && base60[i] !== "0") {
|
|
20
|
+
throw new Error("Invalid character");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
v[i] = val;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return new Date(Date.UTC(v[0] * 60 + v[1], v[2], v[3] + 1, v[4], v[5], v[6]));
|
|
20
27
|
};
|
|
21
28
|
|
|
22
|
-
export const toBase60 = (
|
|
23
|
-
if (!(
|
|
24
|
-
throw new Error('"
|
|
29
|
+
export const toBase60 = (date) => {
|
|
30
|
+
if (!(date instanceof Date)) {
|
|
31
|
+
throw new Error('"date" is not a valid Date object');
|
|
25
32
|
}
|
|
26
33
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
]
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
const year = date.getUTCFullYear();
|
|
35
|
+
|
|
36
|
+
const chars = new Array(7);
|
|
37
|
+
|
|
38
|
+
chars[0] = TABLE[(year / 60) | 0];
|
|
39
|
+
chars[1] = TABLE[year % 60];
|
|
40
|
+
chars[2] = TABLE[date.getUTCMonth()];
|
|
41
|
+
chars[3] = TABLE[date.getUTCDate() - 1];
|
|
42
|
+
chars[4] = TABLE[date.getUTCHours()];
|
|
43
|
+
chars[5] = TABLE[date.getUTCMinutes()];
|
|
44
|
+
chars[6] = TABLE[date.getUTCSeconds()];
|
|
45
|
+
|
|
46
|
+
return chars.join("");
|
|
38
47
|
};
|