Skip to content

Commit

Permalink
fix(utils): taglistOrderVariants improved format
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed May 1, 2023
1 parent 2b63fb7 commit a3e9874
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
15 changes: 10 additions & 5 deletions src/scripts/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DockerRegistryUIError } from './error.js';
const LOCAL_STORAGE_KEY = 'registryServer';

export function bytesToSize(bytes) {
Expand Down Expand Up @@ -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.`);
}
};
26 changes: 15 additions & 11 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -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}`)
);
});
});
Expand Down

0 comments on commit a3e9874

Please sign in to comment.