Skip to content

Commit

Permalink
updated general function for tag comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
matej-vavrek committed Sep 5, 2024
1 parent 5db8ab0 commit c7c010d
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions js/components/preview/tags/utils/tagUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ export const createMoleculeTagObject = (
};
};

export const compareTagsByCategoryAndNameAsc = (a, b) => {
/**
* Compare tags, first by category and then by name
*
* @param {TagObject} a
* @param {TagObject} b
* @param {boolean} asc true for asc, false for desc
* @returns
*/
export const compareTagsByCategoryAndName = (a, b, asc = true) => {
// by category first
if (a.category < b.category) {
return -1;
Expand All @@ -51,33 +59,20 @@ export const compareTagsByCategoryAndNameAsc = (a, b) => {
// then by name
const aName = a.tag_prefix ? `${a.tag_prefix} - ${a.tag}` : a.tag;
const bName = b.tag_prefix ? `${b.tag_prefix} - ${b.tag}` : b.tag;
return aName.localeCompare(bName, undefined, { numeric: true, sensitivity: 'base' });
return asc ? aName.localeCompare(bName, undefined, { numeric: true, sensitivity: 'base' })
: bName.localeCompare(aName, undefined, { numeric: true, sensitivity: 'base' });
};

export const compareTagsAsc = (a, b) => {
const aName = a.tag_prefix ? `${a.tag_prefix} - ${a.tag}` : a.tag;
const bName = b.tag_prefix ? `${b.tag_prefix} - ${b.tag}` : b.tag;
export const compareTagsByCategoryAndNameAsc = (a, b) => {
compareTagsByCategoryAndName(a, b, true);
}

if (aName < bName) {
return -1;
}
if (aName > bName) {
return 1;
}
return 0;
export const compareTagsAsc = (a, b) => {
return compareTagsByCategoryAndName(a, b, true);
};

export const compareTagsDesc = (a, b) => {
const aName = a.tag_prefix ? `${a.tag_prefix} - ${a.tag}` : a.tag;
const bName = b.tag_prefix ? `${b.tag_prefix} - ${b.tag}` : b.tag;

if (aName > bName) {
return -1;
}
if (aName < bName) {
return 1;
}
return 0;
return compareTagsByCategoryAndName(a, b, false);
};

export const compareTagsByCategoryAsc = (a, b) => {
Expand Down

0 comments on commit c7c010d

Please sign in to comment.