More flexible semver handling
This commit is contained in:
parent
e0f5c910eb
commit
d5129832b9
2 changed files with 16 additions and 4 deletions
10
src/utils.js
10
src/utils.js
|
@ -111,9 +111,15 @@ exports.stack = (a, b) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const semver = exports.semver = (str, name='') => {
|
const semver = exports.semver = (str, name='') => {
|
||||||
const main = str.split('-')[0]
|
let main = str.split('-')[0]
|
||||||
|
if (main.startsWith('v')) {
|
||||||
|
main = main.substring(1)
|
||||||
|
}
|
||||||
|
while (main.split('.').length < 3) {
|
||||||
|
main += '.0'
|
||||||
|
}
|
||||||
if (/^\d+\.\d+\.\d+$/.test(main)) {
|
if (/^\d+\.\d+\.\d+$/.test(main)) {
|
||||||
const parts = main.split('.').map(v => parseInt(v))
|
const parts = main.split('.').map(part => parseInt(part, 10))
|
||||||
return {major: parts[0], minor: parts[1], patch: parts[2]}
|
return {major: parts[0], minor: parts[1], patch: parts[2]}
|
||||||
} else throw new Error(`Invalid semver "${str}" at ${name}!`)
|
} else throw new Error(`Invalid semver "${str}" at ${name}!`)
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,8 +133,13 @@ describe('Utils', function() {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('semver', function() {
|
it('semver', function() {
|
||||||
u.semver('1.2.3').should.deep.equal({major: 1, minor: 2, patch: 3})
|
const expected = {major: 1, minor: 0, patch: 0}
|
||||||
u.semver('1.2.3-develop').should.deep.equal({major: 1, minor: 2, patch: 3})
|
u.semver('1.0.0').should.deep.equal(expected)
|
||||||
|
u.semver('1.0.0-develop').should.deep.equal(expected)
|
||||||
|
u.semver('v1.0.0').should.deep.equal(expected)
|
||||||
|
u.semver('1').should.deep.equal(expected)
|
||||||
|
u.semver('1.0').should.deep.equal(expected)
|
||||||
|
u.semver.bind(this, '1.', 'name').should.throw()
|
||||||
u.semver.bind(this, 'invalid', 'name').should.throw()
|
u.semver.bind(this, 'invalid', 'name').should.throw()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -145,6 +150,7 @@ describe('Utils', function() {
|
||||||
u.satisfies('1.2.3', '1.2.4').should.be.false
|
u.satisfies('1.2.3', '1.2.4').should.be.false
|
||||||
u.satisfies('1.2.3', '1.3.0').should.be.false
|
u.satisfies('1.2.3', '1.3.0').should.be.false
|
||||||
u.satisfies('1.2.3', '2.0.0').should.be.false
|
u.satisfies('1.2.3', '2.0.0').should.be.false
|
||||||
|
u.satisfies({major: 1, minor: 2, patch: 3}, {major: 1, minor: 2, patch: 3}).should.be.true
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
Loading…
Add table
Add a link
Reference in a new issue