mirror of
https://github.com/Joxit/docker-registry-ui.git
synced 2025-04-26 15:09:53 +03:00
feat(utils): add talgistOrderParser will parse the order string into object
This commit is contained in:
parent
a3e987482e
commit
edb5aa97e8
3 changed files with 56 additions and 1 deletions
5
src/scripts/error.js
Normal file
5
src/scripts/error.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
export class DockerRegistryUIError extends Error {
|
||||
constructor(msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
|
@ -244,3 +244,21 @@ export const taglistOrderVariants = (taglistOrder) => {
|
|||
throw new DockerRegistryUIError(`The order \`${taglistOrder}\` is not recognized.`);
|
||||
}
|
||||
};
|
||||
|
||||
export function talgistOrderParser(taglistOrder) {
|
||||
const orders = taglistOrderVariants(taglistOrder)
|
||||
.split(';')
|
||||
.filter((e) => e)
|
||||
.map((e) => e.split('-').filter((e) => e))
|
||||
.reduce((acc, e, idx) => {
|
||||
if (e.length > 1) {
|
||||
acc[e[0] + 'Asc'] = e[1] === 'asc';
|
||||
}
|
||||
if (idx === 0) {
|
||||
acc.numFirst = e[0] === 'num';
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
return orders;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { taglistOrderVariants } from '../src/scripts/utils.js';
|
||||
import { taglistOrderVariants, talgistOrderParser } from '../src/scripts/utils.js';
|
||||
import { DockerRegistryUIError } from '../src/scripts/error.js';
|
||||
import assert from 'assert';
|
||||
|
||||
|
@ -35,4 +35,36 @@ describe('utils tests', () => {
|
|||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('talgistOrderParser', () => {
|
||||
it('should have default configuration when empty or undefined', () => {
|
||||
const expected = { numAsc: true, alphaAsc: true, numFirst: true };
|
||||
assert.deepEqual(talgistOrderParser(), expected);
|
||||
assert.deepEqual(talgistOrderParser(''), expected);
|
||||
});
|
||||
|
||||
it('should parse correctly `num-asc;alpha-asc` and variants', () => {
|
||||
const expected = { numAsc: true, alphaAsc: true, numFirst: true };
|
||||
['asc', 'num-asc;alpha-asc', 'num-asc'].forEach((e) =>
|
||||
assert.deepEqual(talgistOrderParser(e), expected, `wrong result for ${e}`)
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse correctly `alpha-desc;num-desc` and variants', () => {
|
||||
const expected = { numAsc: false, alphaAsc: false, numFirst: false };
|
||||
['desc', 'alpha-desc;num-desc'].forEach((e) =>
|
||||
assert.deepEqual(talgistOrderParser(e), expected, `wrong result for ${e}`)
|
||||
);
|
||||
});
|
||||
|
||||
it('should parse correctly `alpha-asc;num-desc` and variants', () => {
|
||||
const expected = { numAsc: false, alphaAsc: true, numFirst: false };
|
||||
assert.deepEqual(talgistOrderParser('alpha-asc;num-desc'), expected)
|
||||
});
|
||||
|
||||
it('should parse correctly `num-desc;alpha-desc` and variants', () => {
|
||||
const expected = { numAsc: false, alphaAsc: false, numFirst: true };
|
||||
assert.deepEqual(talgistOrderParser('num-desc;alpha-desc'), expected)
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue