mirror of
https://github.com/Joxit/docker-registry-ui.git
synced 2026-05-23 04:42:47 +00:00
feat(utils): add getTagComparator that generate comparator, only numFirst supported
This commit is contained in:
@@ -56,3 +56,35 @@ export const splitTagToArray = (tag) =>
|
||||
.split('')
|
||||
.reduce(tagReduce, [])
|
||||
.map((e) => (isDigit(e.charAt(0)) ? parseInt(e) : e));
|
||||
|
||||
const applyOrder = (order, e1, e2) => {
|
||||
if (e1 === e2) {
|
||||
return 0;
|
||||
}
|
||||
if (order.numFirst) {
|
||||
if (typeof e1 === 'number') {
|
||||
const factor = order.numAsc ? 1 : -1;
|
||||
return typeof e2 === 'number' ? (e1 - e2) * factor : -1;
|
||||
} else if (typeof e2 === 'number') {
|
||||
return 1;
|
||||
} else {
|
||||
const factor = order.alphaAsc ? 1 : -1;
|
||||
return e1.localeCompare(e2) * factor
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const getTagComparator = (order) => {
|
||||
return (e1, e2) => {
|
||||
const tag1 = splitTagToArray(e1.tag || e1);
|
||||
const tag2 = splitTagToArray(e2.tag || e2);
|
||||
|
||||
for (var i = 0; i < tag1.length && i < tag2.length; i++) {
|
||||
const compare = applyOrder(order, tag1[i], tag2[i]);
|
||||
if (compare != 0) {
|
||||
return compare;
|
||||
}
|
||||
}
|
||||
return (e1.tag || e1).length - (e2.tag || e2).length;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user