fix(utils): taglistOrderVariants improved format

This commit is contained in:
Joxit 2023-05-02 01:26:11 +02:00
parent 2b63fb725c
commit a3e987482e
No known key found for this signature in database
GPG key ID: F526592B8E012263
2 changed files with 25 additions and 16 deletions

View file

@ -1,3 +1,4 @@
import { DockerRegistryUIError } from './error.js';
const LOCAL_STORAGE_KEY = 'registryServer'; const LOCAL_STORAGE_KEY = 'registryServer';
export function bytesToSize(bytes) { export function bytesToSize(bytes) {
@ -221,21 +222,25 @@ export function stringToArray(value) {
return value && typeof value === 'string' ? value.split(',') : []; 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) => { export const taglistOrderVariants = (taglistOrder) => {
switch (taglistOrder) { switch (taglistOrder) {
case 'desc': case 'desc':
case 'alpha-desc':
return 'alpha-desc;num-desc'; return 'alpha-desc;num-desc';
case 'asc': case 'asc':
case 'num-asc':
return 'num-asc;alpha-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: default:
if (!taglistOrder) { if (!taglistOrder) {
return 'num-asc;alpha-asc'; return 'num-asc;alpha-asc';
} else if (taglistOrder.indexOf(';') === -1) { } else if (TAGLIST_ORDER_REGEX.test(taglistOrder)) {
return taglistOrder.startsWith('num-') ? `${taglistOrder};alpha-asc` : `${taglistOrder};num-asc`;
} else {
return taglistOrder; return taglistOrder;
} }
throw new DockerRegistryUIError(`The order \`${taglistOrder}\` is not recognized.`);
} }
}; };

View file

@ -1,33 +1,37 @@
import { taglistOrderVariants } from '../src/scripts/utils.js'; import { taglistOrderVariants } from '../src/scripts/utils.js';
import { DockerRegistryUIError } from '../src/scripts/error.js';
import assert from 'assert'; import assert from 'assert';
describe('utils tests', () => { describe('utils tests', () => {
describe('taglistOrderVariants', () => { describe('taglistOrderVariants', () => {
it(`should return the input when it's well formed and num first`, () => { 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']; const expected = ['num-asc;alpha-asc', 'num-asc;alpha-desc', 'num-desc;alpha-asc', 'num-desc;alpha-asc'];
expected.forEach( expected.forEach((e) => assert.deepEqual(taglistOrderVariants(e), e));
(e) => assert.deepEqual(taglistOrderVariants(e), e)
);
}); });
it(`should return the input when it's well formed and alpha first`, () => { 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']; const expected = ['alpha-asc;num-asc', 'alpha-asc;num-desc', 'alpha-desc;num-asc', 'alpha-desc;num-asc'];
expected.forEach( expected.forEach((e) => assert.deepEqual(taglistOrderVariants(e), e));
(e) => assert.deepEqual(taglistOrderVariants(e), e)
);
}); });
it('should return correct variant of `num-asc;alpha-asc`', () => { it('should return correct variant of `num-asc;alpha-asc`', () => {
const expected = 'num-asc;alpha-asc'; const expected = 'num-asc;alpha-asc';
['asc', 'num-asc'].forEach( [undefined, '', 'asc', 'num-asc'].forEach((e) => assert.deepEqual(taglistOrderVariants(e), expected));
(e) => assert.deepEqual(taglistOrderVariants(e), expected)
);
}); });
it('should return correct variant of `alpha-desc;num-desc`', () => { it('should return correct variant of `alpha-desc;num-desc`', () => {
const expected = 'alpha-desc;num-desc'; const expected = 'alpha-desc;num-desc';
['desc', 'alpha-desc'].forEach( ['desc'].forEach((e) => assert.deepEqual(taglistOrderVariants(e), expected));
(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}`)
); );
}); });
}); });