diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/.glyph_hash.js.icloud | bin | 0 -> 161 bytes | |||
| -rw-r--r-- | src/utils/.glyph_hash.test.js.icloud | bin | 0 -> 168 bytes | |||
| -rw-r--r-- | src/utils/.readable_time.js.icloud | bin | 0 -> 166 bytes | |||
| -rw-r--r-- | src/utils/.readable_time.test.js.icloud | bin | 0 -> 171 bytes | |||
| -rw-r--r-- | src/utils/.resolve_after.js.icloud | bin | 0 -> 166 bytes | |||
| -rw-r--r-- | src/utils/.resolve_after.test.js.icloud | bin | 0 -> 171 bytes | |||
| -rw-r--r-- | src/utils/glyph_hash.js | 49 | ||||
| -rw-r--r-- | src/utils/glyph_hash.test.js | 63 | ||||
| -rw-r--r-- | src/utils/readable_time.js | 38 | ||||
| -rw-r--r-- | src/utils/readable_time.test.js | 83 | ||||
| -rw-r--r-- | src/utils/resolve_after.js | 25 | ||||
| -rw-r--r-- | src/utils/resolve_after.test.js | 45 |
12 files changed, 0 insertions, 303 deletions
diff --git a/src/utils/.glyph_hash.js.icloud b/src/utils/.glyph_hash.js.icloud Binary files differnew file mode 100644 index 0000000..838924c --- /dev/null +++ b/src/utils/.glyph_hash.js.icloud diff --git a/src/utils/.glyph_hash.test.js.icloud b/src/utils/.glyph_hash.test.js.icloud Binary files differnew file mode 100644 index 0000000..7297edf --- /dev/null +++ b/src/utils/.glyph_hash.test.js.icloud diff --git a/src/utils/.readable_time.js.icloud b/src/utils/.readable_time.js.icloud Binary files differnew file mode 100644 index 0000000..3979901 --- /dev/null +++ b/src/utils/.readable_time.js.icloud diff --git a/src/utils/.readable_time.test.js.icloud b/src/utils/.readable_time.test.js.icloud Binary files differnew file mode 100644 index 0000000..76061bb --- /dev/null +++ b/src/utils/.readable_time.test.js.icloud diff --git a/src/utils/.resolve_after.js.icloud b/src/utils/.resolve_after.js.icloud Binary files differnew file mode 100644 index 0000000..0e74aa9 --- /dev/null +++ b/src/utils/.resolve_after.js.icloud diff --git a/src/utils/.resolve_after.test.js.icloud b/src/utils/.resolve_after.test.js.icloud Binary files differnew file mode 100644 index 0000000..9be721f --- /dev/null +++ b/src/utils/.resolve_after.test.js.icloud diff --git a/src/utils/glyph_hash.js b/src/utils/glyph_hash.js deleted file mode 100644 index 3758601..0000000 --- a/src/utils/glyph_hash.js +++ /dev/null @@ -1,49 +0,0 @@ -const internals = { - kDehyphenRegex: /[-]/g, - kSplitterRegex: /.{1,8}/g, - 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 -const getGlyphHashFragment = function (uuidFragment) { - - const glyphIndex = parseInt(uuidFragment.substring(0, 2), 16) % 16; - return { - glyph: internals.kGlyphs[glyphIndex], - color: `#${uuidFragment.substring(2, 8)}` - }; -}; - -// Return an array of glyphs based on a UUIDv4 -export const getGlyphHash = function (uuid) { - - const dehyphenedUuid = uuid.replace(/[-]/g, ''); - - if (dehyphenedUuid.length !== 32) { - throw new internals.unexpectedUUIDLength(); - } - - const hashFragments = dehyphenedUuid.match(internals.kSplitterRegex); - return hashFragments.map(getGlyphHashFragment); -}; diff --git a/src/utils/glyph_hash.test.js b/src/utils/glyph_hash.test.js deleted file mode 100644 index eada78a..0000000 --- a/src/utils/glyph_hash.test.js +++ /dev/null @@ -1,63 +0,0 @@ -import { getGlyphHash } from './glyph_hash'; - - -describe('Glyph Hash utility', () => { - - test('it throws an exception if the string is too short', () => { - - expect(() => { - - getGlyphHash('short'); - }).toThrow(); - - expect(() => { - - getGlyphHash('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); - }).toThrow(); - - expect(() => { - - getGlyphHash('abcdefghijklmnopqrstuvwxyzABCDEF'); - }).not.toThrow(); - }); - - test('it treats UUIDs with hyphens the same as those without', () => { - - const uuidWithHyphens = 'f7722355-2285-46c0-a55f-3483a826f3a6'; - const uuidWithoutHyphens = 'f7722355228546c0a55f3483a826f3a6'; - - expect(getGlyphHash(uuidWithHyphens)).toEqual(getGlyphHash(uuidWithoutHyphens)); - }); - - describe('it generates four sets of glyphs and colors', () => { - - const state = {}; - - beforeEach(() => { - - state.glyphHash = getGlyphHash('f7722355-2285-46c0-a55f-3483a826f3a6'); - }); - - test('there should be four glyph fragments', () => { - - expect(state.glyphHash.length).toBe(4); - }); - - test('each fragment should have a single character glyph', () => { - - for (const glyphHashFragment of state.glyphHash) { - expect(typeof glyphHashFragment.glyph).toBe('string'); - expect(glyphHashFragment.glyph.length).toBe(1); - } - }); - - test('each fragment should have a hexadecimal color', () => { - - for (const glyphHashFragment of state.glyphHash) { - expect(typeof glyphHashFragment.color).toBe('string'); - expect(glyphHashFragment.color.length).toBe(7); - expect(glyphHashFragment.color).toEqual(expect.stringMatching(/#[0-9a-f]{6}/)); - } - }); - }); -}); diff --git a/src/utils/readable_time.js b/src/utils/readable_time.js deleted file mode 100644 index 4476952..0000000 --- a/src/utils/readable_time.js +++ /dev/null @@ -1,38 +0,0 @@ -const internals = { - magnitudes: { - day: 86400000, - hour: 3600000, - minute: 60000, - second: 1000 - }, - labels: { - day: 'time.days', - hour: 'time.hours', - minute: 'time.minutes', - second: 'time.seconds' - }, - - makeTimeReadable(time, magnitude) { - - return { - count: Math.floor(time / internals.magnitudes[magnitude]), - label: internals.labels[magnitude] - }; - } -}; - -export const readableTime = function readableTime(time) { - - switch (true) { - case time >= internals.magnitudes.day: - return internals.makeTimeReadable(time, 'day'); - case time >= internals.magnitudes.hour: - return internals.makeTimeReadable(time, 'hour'); - case time >= internals.magnitudes.minute: - return internals.makeTimeReadable(time, 'minute'); - case time < 0: - return internals.makeTimeReadable(0, 'second'); - default: - return internals.makeTimeReadable(time, 'second'); - } -}; diff --git a/src/utils/readable_time.test.js b/src/utils/readable_time.test.js deleted file mode 100644 index 5d8ba27..0000000 --- a/src/utils/readable_time.test.js +++ /dev/null @@ -1,83 +0,0 @@ -import { readableTime } from './readable_time'; - -describe('readableTime', () => { - - test('it shows negative time as 0', () => { - - const response = readableTime(-1000); - - expect(response.count).toBe(0); - expect(response.label).toContain('seconds'); - }); - - test('uses seconds as the smallest unit', () => { - - const response = readableTime(10); - - expect(response.count).toBe(0); - expect(response.label).toContain('seconds'); - }); - - test('correctly divides miliseconds into seconds', () => { - - const response = readableTime(4000); - - expect(response.count).toBe(4); - }); - - test('uses seconds if the time is < 1 minute', () => { - - const response = readableTime(59 * 1000); - - expect(response.label).toContain('seconds'); - }); - - test('correctly divides miliseconds into minutes', () => { - - const response = readableTime(2 * 60 * 1000); - - expect(response.count).toBe(2); - }); - - test('uses minutes if the time is < 1 hour', () => { - - const response = readableTime(59 * 60 * 1000); - - expect(response.label).toContain('minutes'); - }); - - test('correctly divides miliseconds into hours', () => { - - const response = readableTime(2 * 60 * 60 * 1000); - - expect(response.count).toBe(2); - }); - - test('uses hours if the time is < 1 days', () => { - - const response = readableTime(23 * 60 * 60 * 1000); - - expect(response.label).toContain('hours'); - }); - - test('correctly divides miliseconds into days', () => { - - const response = readableTime(2 * 24 * 60 * 60 * 1000); - - expect(response.count).toBe(2); - }); - - test('uses days if the time is >= 1 day', () => { - - const response = readableTime(364 * 24 * 60 * 60 * 1000); - - expect(response.label).toContain('days'); - }); - - test('uses days as the maximum unit', () => { - - const response = readableTime(Number.MAX_VALUE); - - expect(response.label).toContain('days'); - }); -}); diff --git a/src/utils/resolve_after.js b/src/utils/resolve_after.js deleted file mode 100644 index 0884401..0000000 --- a/src/utils/resolve_after.js +++ /dev/null @@ -1,25 +0,0 @@ -export const resolveAfter = function (timesUntilResolve) { - - let counter = null; - let currentValue = 0; - - if (typeof timesUntilResolve !== 'number' || timesUntilResolve <= 0) { - throw new Error('Resolve after requires a positive integer'); - } - - const promise = new Promise((resolvePromise) => { - - counter = () => { - - if (++currentValue === timesUntilResolve) { - resolvePromise(); - } - }; - }); - - return { - counter, - promise - }; - -}; diff --git a/src/utils/resolve_after.test.js b/src/utils/resolve_after.test.js deleted file mode 100644 index f7fc753..0000000 --- a/src/utils/resolve_after.test.js +++ /dev/null @@ -1,45 +0,0 @@ -import { resolveAfter } from './resolve_after'; - -describe('Resolve After', () => { - - test('it should throw if given 0', () => { - - expect(() => { - - resolveAfter(0); - }).toThrow(); - }); - - test('it should throw if given a negative number', () => { - - expect(() => { - - resolveAfter(-1); - }).toThrow(); - }); - - test('it should throw if given a negative number', () => { - - expect(() => { - - resolveAfter('lol'); - }).toThrow(); - }); - - test('it should resolve after the specified number of times', () => { - - expect(() => { - - const { counter, resolveAfterThree } = resolveAfter(3); - let resolved = false; - - resolveAfterThree.then(() => (resolved = true)); - counter(); - expect(resolved).toBe(false); - counter(); - expect(resolved).toBe(false); - counter(); - expect(resolved).toBe(true); - }).toThrow(); - }); -}); |