Skip to content

Commit

Permalink
feat(utils): add support to alpha first order
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed May 11, 2023
1 parent 34d1ed9 commit fbab517
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/scripts/taglist-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ 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
}
const numFirst = order.numFirst ? 1 : -1;
if (typeof e1 === 'number') {
const factor = order.numAsc ? 1 : -1;
return typeof e2 === 'number' ? (e1 - e2) * factor : -1 * numFirst;
} else if (typeof e2 === 'number') {
return 1 * numFirst;
} else {
const factor = order.alphaAsc ? 1 : -1;
return e1.localeCompare(e2) * factor;
}
};

Expand Down
36 changes: 36 additions & 0 deletions test/taglist-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,41 @@ describe('utils tests', () => {
assert.deepEqual(['latest', '0.2.4', 'main'].sort(comparator), ['0.2.4', 'main', 'latest']);
assert.deepEqual(['latest', '1.0.0-SNAPSHOT', '1.0.0'].sort(comparator), ['1.0.0', '1.0.0-SNAPSHOT', 'latest']);
});

it('should sort tags with `alpha-asc;num-asc`', () => {
const comparator = getTagComparator(taglistOrderParser('alpha-asc;num-asc'));

assert.deepEqual(['0.2.4', '1.2.5', '0.2.5'].sort(comparator), ['0.2.4', '0.2.5', '1.2.5']);
assert.deepEqual(['latest', '0.2.4', 'main'].sort(comparator), ['latest', 'main', '0.2.4']);
assert.deepEqual(['latest', '1.0.0-SNAPSHOT', '1.0.0'].sort(comparator), ['latest', '1.0.0', '1.0.0-SNAPSHOT']);
assert.deepEqual(['latest', 'main', 'edge'].sort(comparator), ['edge', 'latest', 'main']);
});

it('should sort tags with `alpha-asc;num-desc`', () => {
const comparator = getTagComparator(taglistOrderParser('alpha-asc;num-desc'));

assert.deepEqual(['0.2.4', '1.2.5', '0.2.5'].sort(comparator), ['1.2.5', '0.2.5', '0.2.4']);
assert.deepEqual(['latest', '0.2.4', 'main'].sort(comparator), ['latest', 'main', '0.2.4']);
assert.deepEqual(['latest', '1.0.0-SNAPSHOT', '1.0.0'].sort(comparator), ['latest', '1.0.0', '1.0.0-SNAPSHOT']);
assert.deepEqual(['latest', 'main', 'edge'].sort(comparator), ['edge', 'latest', 'main']);
});

it('should sort tags with `alpha-desc;num-asc`', () => {
const comparator = getTagComparator(taglistOrderParser('alpha-desc;num-asc'));

assert.deepEqual(['0.2.4', '1.2.5', '0.2.5'].sort(comparator), ['0.2.4', '0.2.5', '1.2.5']);
assert.deepEqual(['latest', '0.2.4', 'main'].sort(comparator), ['main', 'latest', '0.2.4']);
assert.deepEqual(['latest', '1.0.0-SNAPSHOT', '1.0.0'].sort(comparator), ['latest', '1.0.0', '1.0.0-SNAPSHOT']);
assert.deepEqual(['latest', 'main', 'edge'].sort(comparator), ['main', 'latest', 'edge']);
});

it('should sort tags with `alpha-desc;num-desc`', () => {
const comparator = getTagComparator(taglistOrderParser('alpha-desc;num-desc'));

assert.deepEqual(['0.2.4', '1.2.5', '0.2.5'].sort(comparator), ['1.2.5', '0.2.5', '0.2.4']);
assert.deepEqual(['latest', '0.2.4', 'main'].sort(comparator), ['main', 'latest', '0.2.4']);
assert.deepEqual(['latest', '1.0.0-SNAPSHOT', '1.0.0'].sort(comparator), ['latest', '1.0.0', '1.0.0-SNAPSHOT']);
assert.deepEqual(['latest', 'main', 'edge'].sort(comparator), ['main', 'latest', 'edge']);
});
});
});

0 comments on commit fbab517

Please sign in to comment.