add ulid timestamp extraction script
This commit is contained in:
parent
4524623697
commit
8c91bb3d34
32
static/js/ulid.js
Normal file
32
static/js/ulid.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
let canonicalBase32 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
||||
let canonicalBase32Rev = {};
|
||||
for (let i = 0; i < canonicalBase32.length; ++i) {
|
||||
canonicalBase32Rev[canonicalBase32.charAt(i)] = i;
|
||||
}
|
||||
|
||||
// Works on BigInt to support numbers larger than (1 << 31).
|
||||
function decodeCanonicalBase32(string) {
|
||||
let result = 0n;
|
||||
let cursor = 0b1n;
|
||||
for (let i = string.length - 1; i >= 0; --i) {
|
||||
result += cursor * BigInt(canonicalBase32Rev[string[i]]);
|
||||
cursor <<= 5n;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function getTimestamp(ulid) {
|
||||
return new Date(Number(decodeCanonicalBase32(ulid.substring(0, 10))));
|
||||
}
|
||||
|
||||
export function isCanonicalUlid(id) {
|
||||
if (id.length != 26) {
|
||||
return false;
|
||||
}
|
||||
for (let i = 0; i < id.length; ++i) {
|
||||
if (!canonicalBase32.includes(id.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
<script>const TREEHOUSE_SITE = `{{ config.site }}`;</script>
|
||||
<script type="module" src="{{ config.site }}/navmap.js"></script>
|
||||
<script type="module" src="{{ config.site }}/static/js/ulid.js"></script>
|
||||
<script type="module" src="{{ config.site }}/static/js/usability.js"></script>
|
||||
<script type="module" src="{{ config.site }}/static/js/tree.js"></script>
|
||||
<script type="module" src="{{ config.site }}/static/js/emoji.js"></script>
|
||||
|
|
Loading…
Reference in a new issue