aboutsummaryrefslogtreecommitdiff
path: root/src/utils/glyph_hash.js
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2021-04-17 12:35:28 +0200
committerRuben Beltran del Rio <ruben@unlimited.pizza>2021-04-17 12:35:28 +0200
commite7f6de3d196ec1bf392056c504c2bc7b86e40ca0 (patch)
treec6a37283a72edb9d0c1465e73ddfd20fb5a95901 /src/utils/glyph_hash.js
parentf088a6594c15951b85a56129944afa51a62dc765 (diff)
Add tests for utils
Diffstat (limited to 'src/utils/glyph_hash.js')
-rw-r--r--src/utils/glyph_hash.js32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/utils/glyph_hash.js b/src/utils/glyph_hash.js
index 13a81a8..3758601 100644
--- a/src/utils/glyph_hash.js
+++ b/src/utils/glyph_hash.js
@@ -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);
};