Skip to content

Commit

Permalink
🐛 Fix incorrect sorting in some edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
priestine committed Jul 6, 2020
1 parent b61b581 commit 2fc024e
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/pure/getters/get-all-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const getAllTags = ({ execEither }: IDeps) => () => ({
allTags: execEither('git show-ref --tags')
.map((string) => string.split('\n'))
.map((refs) => refs.map((ref) => ref.replace(/.*refs\/tags\//, '')))
.map((tags) => tags.filter((tag) => !/(\d+)\.?(\d+)?\.?(\d+)?\+(.*)/.test(tag)))
.map((tags) => tags.sort(bySemVer))
.fold(
() => [],
Expand All @@ -19,15 +20,13 @@ export const getAllTags = ({ execEither }: IDeps) => () => ({
// ------------------------------------------------------------------------------------------------

const bySemVer = (a: string, b: string): -1 | 0 | 1 => {
const aTuple: [number, number, number, string] = /(\d+)\.?(\d+)?\.?(\d+)?(.*)/
.exec(a)
?.slice(1, 5)
.map((n, i) => (i < 3 ? Number(n) : n)) as any
const aTuple: [number, number, number, string] = (/(\d+)\.?(\d+)?\.?(\d+)?(.*)/.exec(a) as any)
.slice(1, 5)
.map((n: string, i: number) => (i < 3 ? Number(n) : n))

const bTuple: [number, number, number, string] = /(\d+)\.?(\d+)?\.?(\d+)?(.*)/
.exec(b)
?.slice(1, 5)
.map((n, i) => (i < 3 ? Number(n) : n)) as any
const bTuple: [number, number, number, string] = (/(\d+)\.?(\d+)?\.?(\d+)?(.*)/.exec(b) as any)
.slice(1, 5)
.map((n: string, i: number) => (i < 3 ? Number(n) : n))

if (aTuple[0] > bTuple[0]) {
return -1
Expand Down Expand Up @@ -56,21 +55,25 @@ const bySemVer = (a: string, b: string): -1 | 0 | 1 => {
const [aRc, aRcVersion] = aTuple[3].split('.')
const [bRc, bRcVersion] = bTuple[3].split('.')

if (!aRc || aRc < bRc) {
return 1
if (!aRc) {
return -1
}

if (!bRc || aRc > bRc) {
return -1
if (!bRc) {
return 1
}

if (!aRcVersion || aRcVersion < bRcVersion) {
if (aRc > bRc) {
return 1
}

if (!bRcVersion || aRcVersion > bRcVersion) {
if (aRc < bRc) {
return -1
}

return 0
if (Number(aRcVersion) < Number(bRcVersion)) {
return 1
}

return -1
}

0 comments on commit 2fc024e

Please sign in to comment.