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