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