]> git.r.bdr.sh - rbdr/forum/blobdiff - src/utils/glyph_hash.js
Add tests for utils
[rbdr/forum] / src / utils / glyph_hash.js
index 13a81a8286af15555a980be11ce54ce39661ef66..3758601dd9d43031b36fb65d9725118cb3aedb60 100644 (file)
@@ -1,6 +1,28 @@
 const internals = {
+  kDehyphenRegex: /[-]/g,
   kSplitterRegex: /.{1,8}/g,
-  kGlyphs: ['☽', '☆', '♢', '♡', '╱', '╲', '╳', '〰', '▷', '⏊', '〒', '▢', '◯', '⏃', '⏀', '⏆']
+  kGlyphs: [
+    '☽',
+    '☆',
+    '♢',
+    '♡',
+    '╱',
+    '╲',
+    '╳',
+    '〰',
+    '▷',
+    '⏊',
+    '〒',
+    '▢',
+    '◯',
+    '⏃',
+    '⏀',
+    '⏆'
+  ],
+  unexpectedUUIDLength: class UnexpectedUUIDLength extends Error {
+    name = 'UnexpectedUUIDLength';
+    message = 'The provided string was not a valid UUIDv4, please provide a 32 character long string'
+  }
 };
 
 // Return a glyph with color based on a 4 byte fragment of a UUIDv4
@@ -16,6 +38,12 @@ const getGlyphHashFragment = function (uuidFragment) {
 // Return an array of glyphs based on a UUIDv4
 export const getGlyphHash = function (uuid) {
 
-  const hashFragments = uuid.replace(/[-]/g, '').match(internals.kSplitterRegex);
+  const dehyphenedUuid = uuid.replace(/[-]/g, '');
+
+  if (dehyphenedUuid.length !== 32) {
+    throw new internals.unexpectedUUIDLength();
+  }
+
+  const hashFragments = dehyphenedUuid.match(internals.kSplitterRegex);
   return hashFragments.map(getGlyphHashFragment);
 };