1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
const SHORT_CHARS =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
/**
* Transforms a number into a custom 62 char expression of that number,
* effectively making a "short" version of that number (0-9a-zA-Z). This does
* NOT convert to a base-62 number.
*
* There are vast gaps in the effective translation of numbers, which is why
* this function is mostly for concisely translating dates prior to the year
* 2063. Since all other values associated with dates (month, day, hours,
* minutes, seconds) effectively fall within the range of a single character,
* this is an accepted shortcoming.
*
* Examples:
*
* 0 = 0
*
* 48 = M
*
* 2022 = km (in base-62 this would be wC)
*
* @deprecated {@link encodeBase62} is now used to generate the timestamp instead
*/
export function toShort(valu: number): string {
return (
valu
.toString()
.match(/.{1,2}/g)
?.map((s) => SHORT_CHARS[parseInt(s)])
.join("") ?? ""
);
}
/**
* Encodes a number as a base-62 value string.
*/
export function encodeBase62(valu: number): string {
let res = "";
const mod = 62;
do {
res = SHORT_CHARS[valu % mod] + res;
valu = Math.floor(valu / mod);
} while (valu > 0);
return res;
}
/**
* Translates a Unix EPOCH timestamp to a 62-char expression of the date. See
* the `toShort()` method for more details on the meaning of the final output.
*/
export function getTimestamp(seconds: number): string {
const date = new Date(seconds * 1000);
const dateArr = [
date.getUTCFullYear(),
date.getUTCMonth() + 1, // UTC month starts at 0
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
];
return dateArr.map(encodeBase62).join(".") + "-0";
}
/**
* Returns a date in the ISO-8601 format
*/
export function formatDate(date: Date): string {
return (
date.getUTCFullYear() +
"-" +
(date.getUTCMonth() + 1) +
"-" +
date.getUTCDate()
);
}
|