Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Feb 3, 2022
1 parent 9dbf882 commit 1f594b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
20 changes: 6 additions & 14 deletions src/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,15 @@ const getEndIndex = (tags, { unreleasedOnly, startingVersion, startingDate, tagP
return 1
}
if (startingVersion) {
// check for literal match first, this allows matching non-semver tags
let index = tags.findIndex(({ tag }) => tag === startingVersion)
const semverStartingVersion = inferSemver(startingVersion.replace(tagPrefix, ''))
const index = tags.findIndex(({ tag }) => {
return tag === startingVersion || tag === semverStartingVersion
})
if (index !== -1) {
return index + 1
} else {
// infer semver from startingVersion & compare with other tag's inferred versions
startingVersion = inferSemver(startingVersion.replace(tagPrefix, ''))
index = tags.findIndex(({ version }) => version === startingVersion)
// check for literal match first
if (index !== -1) {
return index + 1
// find nearest version that is lower than startingVersion
} else {
index = tags.findIndex(({ version }) => semver.gt(startingVersion, version))
return index
}
}
// Fall back to nearest version lower than startingVersion
return tags.findIndex(({ version }) => semver.lt(version, semverStartingVersion))
}
if (startingDate) {
return tags.filter(t => t.isoDate >= startingDate).length
Expand Down
9 changes: 3 additions & 6 deletions test/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,9 @@ describe('fetchTags', () => {

it('supports --starting-version', async () => {
expect(await fetchTags({ ...options, startingVersion: 'v0.3' })).to.have.lengthOf(2)
// abreviated tag inferred via semver
expect(await fetchTags({ ...options, startingVersion: 'v1' })).to.have.lengthOf(1)
// inexistant tag in the past
expect(await fetchTags({ ...options, startingVersion: 'v0.2.8' })).to.have.lengthOf(2)
// inexistant tag in the future
expect(await fetchTags({ ...options, startingVersion: 'v2.0.0' })).to.have.lengthOf(0)
expect(await fetchTags({ ...options, startingVersion: 'v1' })).to.have.lengthOf(1) // Inferred semver
expect(await fetchTags({ ...options, startingVersion: 'v0.2.8' })).to.have.lengthOf(2) // Non-existent tag from the past
expect(await fetchTags({ ...options, startingVersion: 'v2.0.0' })).to.have.lengthOf(0) // Non-existent tag from the future
})

it('supports --ending-version', async () => {
Expand Down

0 comments on commit 1f594b1

Please sign in to comment.