feat(utils): add getTagComparator that generate comparator, only numFirst supported

This commit is contained in:
Joxit
2023-05-10 05:16:57 +02:00
parent a135c00866
commit 34d1ed90ad
2 changed files with 67 additions and 0 deletions

View File

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