From 1e698cdfa2f87dcea12c89de574c974f3f272ea7 Mon Sep 17 00:00:00 2001 From: Kevin Hoerr Date: Wed, 28 Dec 2022 12:12:02 -0500 Subject: Use base-62 for timestamp (#52) --- src/util/timestamp.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/util/timestamp.ts b/src/util/timestamp.ts index f53f5ad..96c5064 100644 --- a/src/util/timestamp.ts +++ b/src/util/timestamp.ts @@ -19,6 +19,8 @@ const SHORT_CHARS = * 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 ( @@ -30,6 +32,21 @@ export function toShort(valu: number): string { ); } +/** + * Encodes a number as a base-62 value string. + */ +export function encodeBase62(valu: number): string { + let res = ""; + const mod = 62; + + while (valu > 0) { + res = SHORT_CHARS[valu % mod] + res; + valu = Math.floor(valu / mod); + } + + 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. @@ -44,5 +61,5 @@ export function getTimestamp(seconds: number): string { date.getUTCMinutes(), ]; - return dateArr.map(toShort).join(".") + "-0"; + return dateArr.map(encodeBase62).join(".") + "-0"; } -- cgit