Skip to content

Commit

Permalink
Fix sortReleases for non-semver tags
Browse files Browse the repository at this point in the history
Fixes #81
  • Loading branch information
cookpete committed Jan 14, 2019
1 parent 4877910 commit 9da4b36
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { niceDate } from './utils'

const MERGE_COMMIT_PATTERN = /^Merge (remote-tracking )?branch '.+'/
const COMMIT_MESSAGE_PATTERN = /\n+([\S\s]+)/
const NUMERIC_PATTERN = /^\d+(\.\d+)?$/

export function parseReleases (commits, remote, latestVersion, options) {
let release = newRelease(latestVersion)
Expand Down Expand Up @@ -43,7 +44,18 @@ export function parseReleases (commits, remote, latestVersion, options) {
}

export function sortReleases (a, b) {
if (a.tag && b.tag) return semver.rcompare(a.tag, b.tag)
if (a.tag && b.tag) {
if (semver.valid(a.tag) && semver.valid(b.tag)) {
return semver.rcompare(a.tag, b.tag)
}
if (NUMERIC_PATTERN.test(a.tag) && NUMERIC_PATTERN.test(b.tag)) {
return parseFloat(a.tag) < parseFloat(b.tag) ? 1 : -1
}
if (a.tag === b.tag) {
return 0
}
return a.tag < b.tag ? 1 : -1
}
if (a.tag) return 1
if (b.tag) return -1
return 0
Expand Down
13 changes: 13 additions & 0 deletions test/releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,17 @@ describe('sortReleases', () => {
expect(sortReleases({ tag: null }, { tag: '0.1.0' })).to.equal(-1)
expect(sortReleases({ tag: null }, { tag: null })).to.equal(-0)
})

it('supports non-semver tags', () => {
expect(sortReleases({ tag: 'abc' }, { tag: 'def' })).to.equal(1)
expect(sortReleases({ tag: 'def' }, { tag: 'abc' })).to.equal(-1)
expect(sortReleases({ tag: 'abc' }, { tag: 'abc' })).to.equal(0)
})

it('supports non-semver numeric tags', () => {
expect(sortReleases({ tag: '22.1' }, { tag: '22.0' })).to.equal(-1)
expect(sortReleases({ tag: '22.0' }, { tag: '22.1' })).to.equal(1)
expect(sortReleases({ tag: '123.0' }, { tag: '22.1' })).to.equal(-1)
expect(sortReleases({ tag: '0.1' }, { tag: '0.01' })).to.equal(-1)
})
})

0 comments on commit 9da4b36

Please sign in to comment.