]>
Commit | Line | Data |
---|---|---|
1 | const internals = { | |
2 | kDehyphenRegex: /[-]/g, | |
3 | kSplitterRegex: /.{1,8}/g, | |
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 | } | |
26 | }; | |
27 | ||
28 | // Return a glyph with color based on a 4 byte fragment of a UUIDv4 | |
29 | const getGlyphHashFragment = function (uuidFragment) { | |
30 | ||
31 | const glyphIndex = parseInt(uuidFragment.substring(0, 2), 16) % 16; | |
32 | return { | |
33 | glyph: internals.kGlyphs[glyphIndex], | |
34 | color: `#${uuidFragment.substring(2, 8)}` | |
35 | }; | |
36 | }; | |
37 | ||
38 | // Return an array of glyphs based on a UUIDv4 | |
39 | export const getGlyphHash = function (uuid) { | |
40 | ||
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); | |
48 | return hashFragments.map(getGlyphHashFragment); | |
49 | }; |