mirror of
https://github.com/Joxit/docker-registry-ui.git
synced 2026-05-23 04:42:47 +00:00
feat(utils): add splitTagToArray that will transform a tag into an array alpha and num splited
This commit is contained in:
58
src/scripts/taglist-order.js
Normal file
58
src/scripts/taglist-order.js
Normal file
@@ -0,0 +1,58 @@
|
||||
import { DockerRegistryUIError } from './error.js';
|
||||
import { isDigit } from './utils.js';
|
||||
|
||||
const TAGLIST_ORDER_REGEX = /(alpha-(asc|desc);num-(asc|desc))|(num-(asc|desc);alpha-(asc|desc))/;
|
||||
|
||||
export const taglistOrderVariants = (taglistOrder) => {
|
||||
switch (taglistOrder) {
|
||||
case 'desc':
|
||||
return 'alpha-desc;num-desc';
|
||||
case '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 (TAGLIST_ORDER_REGEX.test(taglistOrder)) {
|
||||
return taglistOrder;
|
||||
}
|
||||
throw new DockerRegistryUIError(`The order \`${taglistOrder}\` is not recognized.`);
|
||||
}
|
||||
};
|
||||
|
||||
export const taglistOrderParser = (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;
|
||||
};
|
||||
|
||||
export const tagReduce = (acc, e) => {
|
||||
if (acc.length > 0 && isDigit(acc[acc.length - 1].charAt(0)) == isDigit(e)) {
|
||||
acc[acc.length - 1] += e;
|
||||
} else {
|
||||
acc.push(e);
|
||||
}
|
||||
return acc;
|
||||
};
|
||||
|
||||
export const splitTagToArray = (tag) =>
|
||||
tag
|
||||
.split('')
|
||||
.reduce(tagReduce, [])
|
||||
.map((e) => (isDigit(e.charAt(0)) ? parseInt(e) : e));
|
||||
@@ -1,4 +1,3 @@
|
||||
import { DockerRegistryUIError } from './error.js';
|
||||
const LOCAL_STORAGE_KEY = 'registryServer';
|
||||
|
||||
export function bytesToSize(bytes) {
|
||||
@@ -221,44 +220,3 @@ export function truthy(value) {
|
||||
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':
|
||||
return 'alpha-desc;num-desc';
|
||||
case '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 (TAGLIST_ORDER_REGEX.test(taglistOrder)) {
|
||||
return taglistOrder;
|
||||
}
|
||||
throw new DockerRegistryUIError(`The order \`${taglistOrder}\` is not recognized.`);
|
||||
}
|
||||
};
|
||||
|
||||
export const taglistOrderParser = (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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user