From d5129832b9683c5c0e05127dba0152cfb9c92f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1n=20D=C3=A9nes?= Date: Sun, 23 Jan 2022 22:02:05 +0100 Subject: [PATCH] More flexible semver handling --- src/utils.js | 10 ++++++++-- test/unit/utils.js | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/utils.js b/src/utils.js index 7e6b94b..2acf1b0 100644 --- a/src/utils.js +++ b/src/utils.js @@ -111,9 +111,15 @@ exports.stack = (a, b) => { } 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)) { - 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]} } else throw new Error(`Invalid semver "${str}" at ${name}!`) } diff --git a/test/unit/utils.js b/test/unit/utils.js index b131ccb..6ea2bdb 100644 --- a/test/unit/utils.js +++ b/test/unit/utils.js @@ -133,8 +133,13 @@ describe('Utils', function() { }) it('semver', function() { - u.semver('1.2.3').should.deep.equal({major: 1, minor: 2, patch: 3}) - u.semver('1.2.3-develop').should.deep.equal({major: 1, minor: 2, patch: 3}) + const expected = {major: 1, minor: 0, patch: 0} + 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() }) @@ -145,6 +150,7 @@ describe('Utils', function() { 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', '2.0.0').should.be.false + u.satisfies({major: 1, minor: 2, patch: 3}, {major: 1, minor: 2, patch: 3}).should.be.true }) }) \ No newline at end of file