Skip to content

Commit

Permalink
fix: semver.diff prerelease to release recognition (#533)
Browse files Browse the repository at this point in the history
Co-authored-by: deefactorial <[email protected]>
  • Loading branch information
wraithgar and dominique-blockchain authored Apr 10, 2023
1 parent 3f222b1 commit 796cbe2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
26 changes: 21 additions & 5 deletions functions/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,27 @@ const diff = (version1, version2) => {
const hasPre = v1.prerelease.length || v2.prerelease.length
const prefix = hasPre ? 'pre' : ''
const defaultResult = hasPre ? 'prerelease' : ''
for (const key in v1) {
if (key === 'major' || key === 'minor' || key === 'patch') {
if (v1[key] !== v2[key]) {
return prefix + key
}

if (v1.major !== v2.major) {
return prefix + 'major'
}
if (v1.minor !== v2.minor) {
return prefix + 'minor'
}

if (v1.patch !== v2.patch) {
return prefix + 'patch'
}

if (!v1.prerelease.length || !v2.prerelease.length) {
if (v1.patch) {
return 'patch'
}
if (v1.minor) {
return 'minor'
}
if (v1.major) {
return 'major'
}
}
return defaultResult // may be undefined
Expand Down
16 changes: 11 additions & 5 deletions test/functions/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ const diff = require('../../functions/diff')
test('diff versions test', (t) => {
// [version1, version2, result]
// diff(version1, version2) -> result
[['1.2.3', '0.2.3', 'major'],
[
['1.2.3', '0.2.3', 'major'],
['0.2.3', '1.2.3', 'major'],
['1.4.5', '0.2.3', 'major'],
['1.2.3', '2.0.0-pre', 'premajor'],
['2.0.0-pre', '1.2.3', 'premajor'],
['1.2.3', '1.3.3', 'minor'],
['1.0.1', '1.1.0-pre', 'preminor'],
['1.2.3', '1.2.4', 'patch'],
['1.2.3', '1.2.4-pre', 'prepatch'],
['0.0.1', '0.0.1-pre', 'prerelease'],
['0.0.1', '0.0.1-pre-2', 'prerelease'],
['1.1.0', '1.1.0-pre', 'prerelease'],
['0.0.1', '0.0.1-pre', 'patch'],
['0.0.1', '0.0.1-pre-2', 'patch'],
['1.1.0', '1.1.0-pre', 'minor'],
['1.1.0-pre-1', '1.1.0-pre-2', 'prerelease'],
['1.0.0', '1.0.0', null],

['0.0.2-1', '0.0.2', 'patch'],
['0.1.0-1', '0.1.0', 'minor'],
['1.0.0-1', '1.0.0', 'major'],
['0.0.0-1', '0.0.0', 'prerelease'],
].forEach((v) => {
const version1 = v[0]
const version2 = v[1]
Expand Down

0 comments on commit 796cbe2

Please sign in to comment.