X-Git-Url: https://git.r.bdr.sh/rbdr/forum/blobdiff_plain/bd8e98d7e24c4dbaee7db6ec7955f7c2f6d396a6..010f307346e525ac2e4239a0549d2c1a4d6d102b:/src/utils/glyph_hash.js diff --git a/src/utils/glyph_hash.js b/src/utils/glyph_hash.js index 120c02a..3758601 100644 --- a/src/utils/glyph_hash.js +++ b/src/utils/glyph_hash.js @@ -1,4 +1,5 @@ const internals = { + kDehyphenRegex: /[-]/g, kSplitterRegex: /.{1,8}/g, kGlyphs: [ '☽', @@ -17,22 +18,32 @@ const internals = { '⏃', '⏀', '⏆' - ] + ], + 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 const getGlyphHashFragment = function (uuidFragment) { - const glyphIndex = parseInt(uuidFragment.substring(0,2), 16) % 16; + const glyphIndex = parseInt(uuidFragment.substring(0, 2), 16) % 16; return { glyph: internals.kGlyphs[glyphIndex], - color: `#${uuidFragment.substring(2,8)}` + color: `#${uuidFragment.substring(2, 8)}` }; }; // Return an array of glyphs based on a UUIDv4 export const getGlyphHash = function (uuid) { - const hashFragments = uuid.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); };