feat(utils): add splitTagToArray that will transform a tag into an array alpha and num splited

This commit is contained in:
Joxit
2023-05-09 22:34:59 +02:00
parent b0ea4e5fb8
commit a135c00866
3 changed files with 78 additions and 45 deletions

View 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));

View File

@@ -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;
};