diff --git a/src/scripts/utils.js b/src/scripts/utils.js index adf3314..778226d 100644 --- a/src/scripts/utils.js +++ b/src/scripts/utils.js @@ -1,3 +1,4 @@ +import { DockerRegistryUIError } from './error.js'; const LOCAL_STORAGE_KEY = 'registryServer'; export function bytesToSize(bytes) { @@ -221,21 +222,25 @@ export function stringToArray(value) { return value && typeof value === 'string' ? value.split(',') : []; } +const TAGLIST_ORDER_REGEX = /(alpha-(asc|desc);num-(asc|desc))|(num-(asc|desc);alpha-(asc|desc))/; + export const taglistOrderVariants = (taglistOrder) => { switch (taglistOrder) { case 'desc': - case 'alpha-desc': return 'alpha-desc;num-desc'; case 'asc': - case 'num-asc': return 'num-asc;alpha-asc'; + case 'alpha-desc': + case 'alpha-asc': + case 'num-desc': + case 'num-asc': + return `${taglistOrder};${taglistOrder.startsWith('num') ? 'alpha' : 'num'}-asc`; default: if (!taglistOrder) { return 'num-asc;alpha-asc'; - } else if (taglistOrder.indexOf(';') === -1) { - return taglistOrder.startsWith('num-') ? `${taglistOrder};alpha-asc` : `${taglistOrder};num-asc`; - } else { + } else if (TAGLIST_ORDER_REGEX.test(taglistOrder)) { return taglistOrder; } + throw new DockerRegistryUIError(`The order \`${taglistOrder}\` is not recognized.`); } }; diff --git a/test/utils.test.js b/test/utils.test.js index 8ff84d1..c87c8d3 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -1,33 +1,37 @@ import { taglistOrderVariants } from '../src/scripts/utils.js'; +import { DockerRegistryUIError } from '../src/scripts/error.js'; import assert from 'assert'; describe('utils tests', () => { describe('taglistOrderVariants', () => { it(`should return the input when it's well formed and num first`, () => { const expected = ['num-asc;alpha-asc', 'num-asc;alpha-desc', 'num-desc;alpha-asc', 'num-desc;alpha-asc']; - expected.forEach( - (e) => assert.deepEqual(taglistOrderVariants(e), e) - ); + expected.forEach((e) => assert.deepEqual(taglistOrderVariants(e), e)); }); it(`should return the input when it's well formed and alpha first`, () => { const expected = ['alpha-asc;num-asc', 'alpha-asc;num-desc', 'alpha-desc;num-asc', 'alpha-desc;num-asc']; - expected.forEach( - (e) => assert.deepEqual(taglistOrderVariants(e), e) - ); + expected.forEach((e) => assert.deepEqual(taglistOrderVariants(e), e)); }); it('should return correct variant of `num-asc;alpha-asc`', () => { const expected = 'num-asc;alpha-asc'; - ['asc', 'num-asc'].forEach( - (e) => assert.deepEqual(taglistOrderVariants(e), expected) - ); + [undefined, '', 'asc', 'num-asc'].forEach((e) => assert.deepEqual(taglistOrderVariants(e), expected)); }); it('should return correct variant of `alpha-desc;num-desc`', () => { const expected = 'alpha-desc;num-desc'; - ['desc', 'alpha-desc'].forEach( - (e) => assert.deepEqual(taglistOrderVariants(e), expected) + ['desc'].forEach((e) => assert.deepEqual(taglistOrderVariants(e), expected)); + }); + + it('should extend correctly orders', () => { + ['alpha-desc', 'alpha-asc'].forEach((e) => assert.deepEqual(taglistOrderVariants(e), `${e};num-asc`)); + ['num-desc', 'num-asc'].forEach((e) => assert.deepEqual(taglistOrderVariants(e), `${e};alpha-asc`)); + }); + + it('should throw error on incorrect values', () => { + ['alpha-desc;alpha-asc', 'foobar'].forEach((e) => + assert.throws(() => taglistOrderVariants(e), DockerRegistryUIError, `Did not throw on ${e}`) ); }); });