@snyk/ruby-semver
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -13,3 +13,3 @@ const GemVersion = require('./generated').GemVersion; | ||
rcompare, | ||
diff: () => { throw new Error('Not implemented'); }, | ||
diff, | ||
}; | ||
@@ -79,1 +79,28 @@ | ||
} | ||
function diff(v1, v2) { | ||
if (eq(v1, v2)) { return null; } | ||
const version1 = GemVersion.$create(v1); | ||
const version2 = GemVersion.$create(v2); | ||
let hasPrerelease; | ||
const segments = [version1.$segments(), version2.$segments()] | ||
.map(seg => { | ||
const prereleaseIndex = seg.findIndex(v => String(v).match(/[a-zA-Z]/)); | ||
if (prereleaseIndex === -1) { return seg; } | ||
hasPrerelease = true; | ||
return seg.slice(0, prereleaseIndex); | ||
}) | ||
.sort((a,b) => b.length - a.length) | ||
const diffPosition = segments[0].findIndex((v, i) => v !== segments[1][i]); | ||
if (diffPosition === -1 && hasPrerelease) { | ||
return 'prerelease'; | ||
} | ||
const diffType = ['major', 'minor'][diffPosition] || 'patch'; | ||
return (hasPrerelease ? 'pre' : '') + diffType; | ||
} |
@@ -19,3 +19,3 @@ { | ||
}, | ||
"version": "1.2.0" | ||
"version": "1.3.0" | ||
} |
import test from 'ava'; | ||
import { diff } from '../../'; | ||
const semver = require('../../'); | ||
const diff = semver.diff; | ||
@@ -11,4 +12,62 @@ // Not implemented as we don't use it. | ||
test('diff(v1, v2): not implemented', t => { | ||
t.throws(() => diff('1.1.2', '1.2.0'), 'Not implemented'); | ||
test('diff(v1, v2): same versions', t => { | ||
t.is(diff('1', '1'), null); | ||
t.is(diff('1.1', '1.1'), null); | ||
t.is(diff('1.1.2', '1.1.2'), null); | ||
t.is(diff('1.1.1.1', '1.1.1.1'), null); | ||
t.is(diff('1.0.0.alpha.1', '1.0.0.alpha.1'), null); | ||
t.is(diff('1.1.2-1', '1.1.2.pre.1'), null); | ||
t.is(diff('1.1.2.pre.1', '1.1.2-1'), null); | ||
t.is(diff('2', '2.0'), null); | ||
t.is(diff('2.0', '2'), null); | ||
t.is(diff('2', '2.0.0'), null); | ||
t.is(diff('2.0.0', '2'), null); | ||
t.is(diff('2', '2.0.0.0'), null); | ||
t.is(diff('2.0.0.0', '2'), null); | ||
}); | ||
test('diff(v1, v2): major versions', t => { | ||
t.is(diff('1', '3'), 'major'); | ||
t.is(diff('1.1', '3.1'), 'major'); | ||
t.is(diff('1.1.2', '3.0.0'), 'major'); | ||
t.is(diff('1.1.2', '2.0.0'), 'major'); | ||
t.is(diff('1.1.1.1', '2.0.0'), 'major'); | ||
}); | ||
test('diff(v1, v2): minor versions', t => { | ||
t.is(diff('1.1', '1.2'), 'minor'); | ||
t.is(diff('1.1.2', '1.2.1.1'), 'minor'); | ||
t.is(diff('1.1.2', '1.2.0'), 'minor'); | ||
t.is(diff('1.1.2.1', '1.2.0'), 'minor'); | ||
t.is(diff('1.1.2.1', '1.2.0.1'), 'minor'); | ||
}); | ||
test('diff(v1, v2): patch versions', t => { | ||
t.is(diff('1.1.2', '1.1.3'), 'patch'); | ||
t.is(diff('1.1.2', '1.1.2.1'), 'patch'); | ||
t.is(diff('1.1.2.1', '1.1.3'), 'patch'); | ||
t.is(diff('1.1.2.1', '1.1.3.2.1'), 'patch'); | ||
t.is(diff('1.1.2.1', '1.1.2.1.1.1.2'), 'patch'); | ||
t.is(diff('1.1.2.1.1.1.1', '1.1.2.1.1.1.2'), 'patch'); | ||
}); | ||
test('diff(v1, v2): premajor versions', t => { | ||
t.is(diff('1.0.0.alpha.1', '2.0.0'), 'premajor'); | ||
}); | ||
test('diff(v1, v2): preminor versions', t => { | ||
t.is(diff('1.1.2.alpha.1', '1.2.0'), 'preminor'); | ||
}); | ||
test('diff(v1, v2): prepatch versions', t => { | ||
t.is(diff('1.1.2.alpha.1', '1.1.3'), 'prepatch'); | ||
t.is(diff('1.1.2.3.alpha.1', '1.1.2.alpha.2'), 'prepatch'); | ||
t.is(diff('1.1.2.3.alpha.1', '1.1.2.4.alpha.2'), 'prepatch'); | ||
t.is(diff('1.1.2.alpha.1', '1.1.2.1'), 'prepatch'); | ||
}); | ||
test('diff(v1, v2): prerelease versions', t => { | ||
t.is(diff('1.1.2.alpha.1', '1.1.2.alpha.2'), 'prerelease'); | ||
t.is(diff('1.1.2.3.alpha.1', '1.1.2.3.alpha.2'), 'prerelease'); | ||
t.is(diff('1.alpha.1', '1'), 'prerelease'); | ||
}); |
718070
16381