]> git.r.bdr.sh - rbdr/forum/blame - src/lib/utils/glyph_hash.ts
Use tailwind, reference types
[rbdr/forum] / src / lib / utils / glyph_hash.ts
CommitLineData
a7cf03c1
RBR
1export type GlyphHash = GlyphHashFragment[];
2type GlyphHashFragment = {
cac85db0
RBR
3 glyph: string;
4 color: string;
a7cf03c1
RBR
5};
6
66dc4cae 7const internals = {
cac85db0
RBR
8 kDehyphenRegex: /[-]/g,
9 kSplitterRegex: /.{1,8}/g,
10 kGlyphs: ['☽', '☆', '♢', '♡', '╱', '╲', '╳', '〰', '▷', '⏊', '〒', '▢', '◯', '⏃', '⏀', '⏆'],
11 unexpectedUUIDLength: class UnexpectedUUIDLength extends Error {
12 name = 'UnexpectedUUIDLength';
13 message =
14 'The provided string was not a valid UUIDv4, please provide a 32 character long string';
15 }
66dc4cae
BB
16};
17
18// Return a glyph with color based on a 4 byte fragment of a UUIDv4
a7cf03c1 19const getGlyphHashFragment = function (uuidFragment: string): GlyphHashFragment {
cac85db0
RBR
20 const glyphIndex = parseInt(uuidFragment.substring(0, 2), 16) % 16;
21 return {
22 glyph: internals.kGlyphs[glyphIndex],
23 color: `#${uuidFragment.substring(2, 8)}`
24 };
66dc4cae
BB
25};
26
27// Return an array of glyphs based on a UUIDv4
a7cf03c1 28export const getGlyphHash = function (uuid: string): GlyphHash {
cac85db0 29 const dehyphenedUuid = uuid.replace(/[-]/g, '');
66dc4cae 30
cac85db0
RBR
31 if (dehyphenedUuid.length !== 32) {
32 throw new internals.unexpectedUUIDLength();
33 }
e7f6de3d 34
cac85db0
RBR
35 const hashFragments = dehyphenedUuid.match(internals.kSplitterRegex);
36 return hashFragments.map(getGlyphHashFragment);
66dc4cae 37};